Automatically Send SMS to customers after finishing a call
Use Toky webhooks to send an SMS after a customer finishes an inbound or outbound call
What you can get
This is what you can get with this sample:
- After an inbound or outbound call finish in your Toky call center, our system will automatically report the event to a registered service for the new_call event.
- Your registered service will receive the new_call data and it will send a custom SMS to the caller's phone number.
This sample can be used to automatically send surveys, marketing information, coupon codes etc.
Server code in PHP
You need to create a REST web service that you can't register later as a Toky webhook for the new_call event. This is a typical request for this event:
{
"callid": "6a3c8sd112ochev3ru9b",
"direction": "Outbound",
"web_call": "no",
"agent": "[email protected]",
"duration": "12",
"state": "completed",
"contact_name": "Jhon Doe",
"contact_email": "[email protected]",
"contact_id": "247578",
"to_number": "+1-202-5550174",
"from_number": "+16175550115",
"date": "2019-11-26 14:24",
"record_url": "https://app.toky.co/business/recording/outbound/6a3c8sd112ochev3ru9b",
"record_url_raw": "https://tokystorage.s3.amazonaws.com/recordings/8d6dc35eaaa1345bbbb23/6a3c8saaa123k2k2k3k3k",
"caller_name": "",
"caller_email": "",
"caller_country": "",
"caller_phone_number": "",
"reply_code": "200",
"error_message": "",
"ivr_option_pressed": null,
"answered_time": 17,
"in_queue_duration": 0
}
API Endpoint used
Parameters you need to replace in this web service:
- [[TOKY_API_KEY][TOKY\_API\_KEY]]: You can get the Toky API on this link: https://app.toky.co/business/my_account#api-key
- [[TOKY_SMS_PHONE_NUMBER][TOKY\_SMS\_PHONE\_NUMBER]]: This is the phone number in Toky, enabled to send SMS text messages. It should be entered in the international format, for example:
+ 18443326433
- [[AGENT_EMAIL][AGENT_EMAIL]]: It is the email of the agent to whom you want to be assigned SMS sent in Toky.
<?php
//header("Content-Type: application/json; charset=UTF-8");
//get request data
$data =file_get_contents('php://input');
//Convert data to object
$obj = json_decode($data,true);
//Ignore web calls
if ($obj["web_call"]=="no"){
//Define destination phone number
if($obj["direction"]=="Inbound"){
//If Inbound the SMS is sent to the source phone number
$destPhoneNumber =$obj["from_number"];
}else{
//If Outbound the SMS is sent to the destination phone number
$destPhoneNumber =$obj["to_number"];
}
// Message to send to the customer, it can be customized with the webhook data
$message ="Hello, thank you for contacting Toky, visit https://toky.co to learn more about us.";
sendSMS($destPhoneNumber,$message);
}
//function to send SMS
function sendSMS($phoneNumber,$textMessage)
{
// create a new cURL resource
$ch = curl_init();
$smsPhoneNumber = "[[TOKY_SMS_PHONE_NUMBER]]";
$api_key = '[[TOKY_API_KEY]]';
$headers = array();
$headers[] = "X-Toky-Key: {$api_key}";
$data = array("from" => $smsPhoneNumber, "email" => "[[AGENT_EMAIL]]",
"to" => $phoneNumber,
"text" => $textMessage);
$json_data = json_encode($data);
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://api.toky.co/v1/sms/send");
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS, $json_data);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
$curl_response = curl_exec($ch); // Send request
curl_close($ch); // close cURL resource
$decoded = json_decode($curl_response,true);
//Return error message if the message can't be sent
if (!$decoded["success"]){
$result= "Error sending SMS '$textMessage' to $phoneNumber: ".$decoded["error_message"];
}else{
$result='SMS sent successfully';
}
return $result;
}
?>
Now that you have a web service you can register it in Toky to start receiving events after every call is finished. To register your webhook you can follow the steps on this article
Updated 3 months ago