您似乎正在尝试将整个 $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