1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #include <boost/program_options.hpp> #include <string> #include <thread> #include "../include/aricpp/client.h" #include "../include/aricpp/urlencode.h"
using namespace std; using namespace aricpp;
int main( int argc, char* argv[] ) { try { string host = "172.16.30.49"; string port = "8088"; string username = "asterisk"; string password = "passwd"; string application = "zarniwoop";
namespace po = boost::program_options; po::options_description desc("Allowed options"); desc.add_options() ("help,h", "produce help message") ("version,V", "print version string")
("host,H", po::value(&host), "ip address of the ARI server [localhost]") ("port,P", po::value(&port), "port of the ARI server [8088]") ("username,u", po::value(&username), "username of the ARI account on the server [asterisk]") ("password,p", po::value(&password), "password of the ARI account on the server [asterisk]") ("application,a", po::value(&application), "stasis application to use [attendant]") ;
po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm);
if (vm.count("help")) { std::cout << desc << "\n"; return 0; }
if (vm.count("version")) { cout << "This is push application v1.0.\n"; return 0; }
#if BOOST_VERSION < 106600 using IoContext = boost::asio::io_service; #else using IoContext = boost::asio::io_context; #endif IoContext ios;
Client client( ios, host, port, username, password, application ); client.Connect( [&](boost::system::error_code e){ if (e) cerr << "Error connecting: " << e.message() << '\n'; else { cout << "Connected" << '\n'; } });
auto inputReader = [&]() { string line; while (true) { cout << "> Enter called and the voice you need to push:\n"; getline( std::cin, line );
std::istringstream iss(line); std::string called, voice, caller;
if (!(iss >> called >> voice)) { std::cerr << "Error: Not enough input." << std::endl; continue; }
auto url = "/ari/channels?endpoint=PJSIP%2F"+called+"&extension=2839&context=ims&timeout=30&api_key=asterisk:passwd"; if (iss >> caller) { url += "&callerId=" + caller; } if (iss.rdbuf()->in_avail() != 0) { std::cerr << "Warning: Extra input detected." << std::endl; }
auto sendRequest = [&client,url,voice]() { client.RawCmd(Method::post, url, [](auto,auto,auto,auto){}, "{\"variables\": {\"push_sound\": \""+voice+"\"}}"); };
#if BOOST_VERSION < 106600 ios.post(sendRequest); #else boost::asio::post(ios.get_executor(), sendRequest); #endif }
cout << "Exiting application\n"; ios.stop(); }; thread readerThread( inputReader );
ios.run();
readerThread.join();
} catch ( const exception& e ) { cerr << "Exception in app: " << e.what() << ". Aborting\n"; return -1; } return 0; }
|