I am working on a side project for one of my clients that will send them a text (through Twilio) when they receive a new livechat chat. This webhook (docs here) sends information about the visitor, along with their responses to a pre-chat survey. I am able to send a basic message like "New chat on your site!" but not any variable data. Here's the start to my code:
<?php
$data = file_get_contents('php://input');
$data = json_decode($data);
// this line loads the library
require './twilio/Services/Twilio.php';
$account_sid = $sid;
$auth_token = $token;
$client = new Services_Twilio($account_sid, $auth_token);
try {
$message = $client->account->messages->create(array(
"From" => "+12677932737",
"To" => "5555555555",
"Body" => $data,
));
} catch (Services_Twilio_RestException $e) {
$error = $e->getMessage();
}
?>
Thanks!
It looks like you are trying to send the entire
$data
as the body of the message.According to the REST docs for Twilio, the
Body
variable can only be 160 characters (normal SMS length).When looking at the link you provided for the LiveChat web hooks, there are a few different data objects that you might be getting:
Example of a simple web hook when a chat starts:
Chat start with visitor info:
Web hook of a chat message:
All of them except for the first one are over 160 characters. So if you are sending the raw request body, Twilio won't accept it.
Instead, what you could do is then just return a custom body depending on what information you want to send off to Twilio.
For example, you could do:
Or you could actually send the first message:
Then the last thing you should do is to either trim the message to be 160 characters or paginate them so your message isn't lost.
A simple trim would be: