我正在为我的一个客户开展一个侧面项目,当他们收到新的实时聊天时,会向他们发送文本(通过Twilio)。此webhook(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();
}
?>
谢谢!
答案 0 :(得分:1)
看起来您正在尝试将整个$data
作为邮件正文发送。
根据Twilio的REST docs,Body
变量只能是160个字符(正常的短信长度)。
在查看您为LiveChat Web挂钩提供的链接时,您可能会获得一些不同的数据对象:
聊天开始时的简单Web挂钩示例:
{
"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"
}
}
聊天消息的Web钩子:
"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