#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;
}