【问题标题】:How to receive Webhook and send message through Twillio with it's data如何通过 Twilio 接收 Webhook 并发送消息及其数据
【发布时间】:2014-11-15 22:49:25
【问题描述】:

我正在为我的一个客户做一个附带项目,当他们收到新的实时聊天时,将向他们发送一条文本(通过 Twilio)。此网络钩子 (docs here) 发送有关访问者的信息,以及他们对聊天前调查的回复。我可以发送一条基本消息,例如“您网站上的新聊天!”但不是任何可变数据。这是我的代码的开始:

<?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();
}
?>

谢谢!

【问题讨论】:

    标签: php webhooks livechat


    【解决方案1】:

    您似乎正在尝试将整个 $data 作为邮件正文发送。

    根据 Twilio 的 REST docs,Body 变量只能是 160 个字符(正常 SMS 长度)。

    查看您为 LiveChat 网络挂钩提供的链接时,您可能会获得一些不同的数据对象:

    聊天开始时的简单网络挂钩示例:

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

    从访客信息开始聊天:

    {
      "event_type": "chat_started",
      "token": "27f41c8da685c81a890f9e5f8ce48387",
      "license_id": "1025707",
      "visitor": {
        "id": "S1354547427.0c151b0e1b",
        "name": "John",
        "email": "john.smith@gmail.com"
      }
    }
    

    聊天消息的网络挂钩:

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

    除了第一个之外,所有这些都超过 160 个字符。因此,如果您发送的是原始请求正文,Twilio 将不会接受它。

    相反,您可以做的只是返回一个自定义正文,具体取决于您要发送给 Twilio 的信息。

    例如,你可以这样做:

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

    或者您实际上可以发送第一条消息:

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

    那么您应该做的最后一件事是将消息修剪为 160 个字符或对它们进行分页,这样您的消息就不会丢失。

    一个简单的修剪是:

    if (strlen($body) > 160) {
        $body = substr($body, 0, 156) + '...';
    }
    
    // Then send the $body off to Twilio
    

    【讨论】:

      猜你喜欢
      • 2020-12-22
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多