How to receive Webhook and send message through Twillio with it's data

848 views Asked by At

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!

1

There are 1 answers

0
Josh Davis On BEST ANSWER

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:

{
  "event_type": "chat_started",
  "token": "27f41c8da685c81a890f9e5f8ce48387",
  "license_id": "1025707"
}

Chat start with visitor info:

{
  "event_type": "chat_started",
  "token": "27f41c8da685c81a890f9e5f8ce48387",
  "license_id": "1025707",
  "visitor": {
    "id": "S1354547427.0c151b0e1b",
    "name": "John",
    "email": "[email protected]"
  }
}

Web hook of a chat message:

"chat": {
   "id":"MH022RD0K5",
   "started_timestamp":1358937653,
   "ended_timestamp":1358939109,
   "messages":[
      {
         "user_type":"agent",
         "author_name":"John Doe",
         "agent_id":"[email protected]",
         "text":"Hello",
         "timestamp":1358937653
      },
      {
         "user_type":"supervisor",
         "author_name":"James Doe",
         "agent_id":"[email protected]",
         "text":"This is whispered message.",
         "timestamp":1358937658
      },
      {
         "user_type":"visitor",
         "author_name":"Mary Brown",
         "text":"How are you?",
         "timestamp":1358937661
      },
      tags:[
         "sales",
         "support",
         "feedback"
      ]
   ]
}

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:

$body = "Chat started with: {$data->visitor->name}";

Or you could actually send the first message:

$body = "Chat message: {$data->chat->messages[0]->text}";

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:

if (strlen($body) > 160) {
    $body = substr($body, 0, 156) + '...';
}

// Then send the $body off to Twilio