📜 ⬆️ ⬇️

We make automated calls for notifications or polls.

In some cases, the company may need to call some of its customers and inform them about something, or, for example, conduct a survey about the quality of service. This task can be solved in different ways, but we will proceed from the fact that in this case it needs to be solved by a web developer who is used to dealing with Javascript / web services and doesn’t really want to delve into the specifics of IP telephony systems. Fortunately for him there is a suitable tool, which is more detailed under the cut.

So, for the implementation of automated dialing, we will use the VoxImplant platform, as well as its built-in call- list subsystem (CallList). It should be immediately noted that automated calls can only be made by people who have agreed to this . Telephone spam is no better than SMS spam or email spam, do not annoy people once again.
Let's look at several scenarios — the usual notification of an event (for example, an activation code message or a reminder about a visit to a medical institution) and a survey on the quality of service.

Account setup


In the case of VoxImplant, a CSV file with a list of numbers and additional information is supplied to each number that can be used in the script running on each line of the file. The first line is a required header with the name of the parameters, so that later these parameters can be used in the script. There are also system parameters that affect how the system will process this string. Let's consider the following example:
')
user_id;first_name;last_name;phone_number;activation_code 0;;;74951234567;101102 


This is the content of the CSV file that we will send to the call. When calling a VoxImplant script, this data will come in the form of JSON, and we will be able to use it for its intended purpose. Create the following VoxEngine script:

 require(Modules.CallList); //   CallList require(Modules.AI); var call, first_name, last_name, phone_number, activation_code, playbackCounter = 0; //     VoxEngine.addEventListener(AppEvents.Started, function (e) { var data = VoxEngine.customData(); // <--      CSV  JSON- data = JSON.parse(data); user_id = data.user_id; first_name = data.first_name; last_name = data.last_name; phone_number = data.phone_number; activation_code = data.activation_code; Logger.write(" " + first_name + " " + last_name + "   " + phone_number); //  call = VoxEngine.callPSTN(phone_number, "+1234567890"); // <--     callerid,      CallerIDs     Voximplant //     call.addEventListener(CallEvents.AudioStarted, function(){AI.detectVoicemail(call)}); //    call.addEventListener(CallEvents.Connected, handleCallConnected); call.addEventListener(CallEvents.Failed, handleCallFailed); call.addEventListener(CallEvents.Disconnected, handleCallDisconnected); call.addEventListener(AI.Events.VoicemailDetected, voicemailDetected); }); function voicemailDetected(e) { //  ? if (e.confidence >= 75) { VoxEngine.CallList.reportError("Voicemail", VoxEngine.terminate); return; } } //  function handleCallConnected(e) { connected = true; setTimeout(function () { e.call.say(", " + first_name + "!   : " + activation_code, Language.RU_RUSSIAN_FEMALE); }, 500); e.call.addEventListener(CallEvents.PlaybackFinished, handlePlaybackFinished); } //   function handlePlaybackFinished(e) { e.call.removeEventListener(CallEvents.PlaybackFinished, handlePlaybackFinished); playbackCounter++; //        if (playbackCounter == 4) { e.call.hangup(); } else { //          setTimeout(function () { e.call.say("  : " + activation_code, Language.RU_RUSSIAN_FEMALE); e.call.addEventListener(CallEvents.PlaybackFinished, handlePlaybackFinished); }, 2000); } } function handleCallFailed(e) { //  ,     //               //   ,    ,    result_data // CSV-      result, msg, code  JSON- CallList.reportError({ result: false, msg: "Failed", code: e.code }, VoxEngine.terminate); } function handleCallDisconnected(e) { //   ,  ,   // result  duration     result_data // CSV-     JSON- CallList.reportResult({ result: true, duration: e.duration }, VoxEngine.terminate); } 

We save the script, create an application and the application rule to which we cling to the script. Pattern can not be changed, it does not matter when run through an HTTP request.

Calling through HTTP API


The API method to start dialing http://voximplant.com/docs/references/httpapi/#toc-createcalllist , let's analyze the request parameters so that everything is clear:



Let's make the following PHP file with a couple of functions that start dialing:

 <?php define("API_URL", "https://api.voximplant.com/platform_api/"); define("API_KEY", " api key"); define("ACCOUNT_NAME", " account name"); define("RULE_ID", id-); function httpRequest($url,$params) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); if (isset($params["post"])) curl_setopt($ch, CURLOPT_POST, 1); if (isset($params["post_data"])) curl_setopt($ch, CURLOPT_POSTFIELDS, $params["post_data"]); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/csv')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); curl_close ($ch); return $server_output; } function createCallList($file) { $url = API_URL . "CreateCallList/?" . "account_name=" . ACCOUNT_NAME . "&api_key=" . API_KEY . "&rule_id=" . RULE_ID . "&max_simultaneous=10" . "&num_attempts=2" . "&interval_seconds=60" . "&priority=1" . "&name=CallList"; $data = file_get_contents($file); $params = array('post' => true, 'post_data' => $data); $result = httpRequest($url, $params); echo $result; } function getCallListDetails($list_id, $output = "json") { $url = API_URL . "GetCallListDetails/?" . "account_name=" . ACCOUNT_NAME . "&api_key=" . API_KEY . "&list_id=" . $list_id . "&output=" . $output; $params = array(); $result = httpRequest($url, $params); echo $result; } createCallList(URL-CSV-); //getCallListDetails($list_id, "csv"); ?> 

The result of the execution of getCallListDetails will look something like this (if you have successfully dialed):
 "activation_code";"user_id";"last_name";"phone_number";"first_name";"__end_execution_time";"__start_execution_time";"result_data";"last_attempt";"attmepts_left";"status_id";status 101102;0;;74951234567;;;;"{""result"":true,""duration"":27}";"2014-11-24 19:21:39";1;2;Processed 

Naturally, you can get a list of all telephone calls http://voximplant.com/docs/references/httpapi/GetCallLists.html or stop the telephone call http://voximplant.com/docs/references/httpapi/StopCallListProcessing.html .

Sample Survey Script


Let's slightly complicate the task - besides the call, we will also be asked to enter an assessment of the quality of service and save it in the results of the call. To do this, you will need to modify the functions handleCallConnected, handleCallDisconnected and add a new handleToneReceived:
 function handleCallConnected(e) { connected = true; e.call.handleTones(true); // <--    setTimeout(function () { e.call.say(", " + first_name + "!      , "+ ",       .", Language.RU_RUSSIAN_FEMALE); }, 500); e.call.addEventListener(CallEvents.PlaybackFinished, handlePlaybackFinished); e.call.addEventListener(CallEvents.ToneReceived, handleToneReceived); } var rating; function handleToneReceived(e) { e.call.removeEventListener(CallEvents.PlaybackFinished, handlePlaybackFinished); e.call.stopPlayback(); rating = e.tone; e.call.say("    !", Language.RU_RUSSIAN_FEMALE); e.call.addEventListener(CallEvents.PlaybackFinished, function(e) { e.call.hangup(); }); } function handleCallDisconnected(e) { CallList.reportResult({ result: true, duration: e.duration, rating: rating, }, VoxEngine.terminate); } //   function handlePlaybackFinished(e) { e.call.removeEventListener(CallEvents.PlaybackFinished, handlePlaybackFinished); playbackCounter++; //        if (playbackCounter == 4) { e.call.hangup(); } else { //          setTimeout(function () { e.call.say(",       .", Language.RU_RUSSIAN_FEMALE); e.call.addEventListener(CallEvents.PlaybackFinished, handlePlaybackFinished); }, 2000); } } 

The result will look something like this:
 "user_id";"last_name";"phone_number";"first_name";"__end_execution_time";"__start_execution_time";"result_data";"last_attempt";"attmepts_left";"status_id";status 0;;74951234567;;;;"{""result"":true,""duration"":27,""rating"":""3""}";"2014-11-24 20:17:13";1;2;Processed 

Well, we reviewed a few dialing scenarios using VoxImplant. If you wish, you can make HTTP requests to the outside world directly from the script and transfer data somewhere in real time or connect the ASR module with speech recognition and allow people to answer the question directly with voice. Calls, if desired, can be sent through your SIP-infrastructure - we change callPSTN to callSIP in the script and transfer the settings there. In general, we hope that this post will be useful to you and help you solve your problem, useful calls to you!

Source: https://habr.com/ru/post/243809/


All Articles