twilio 短信关键字自动回复搜索传入正文 [英] twilio sms keyword autoresponder search incoming body

查看:28
本文介绍了twilio 短信关键字自动回复搜索传入正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助修改来自 Twilio 的建议代码以搜索传入的短信正文以发送不同的响应吗?https://www.twilio.com/help/faq/sms/how-do-i-build-a-sms-keyword-response-application

Can someone help to revise the suggested code from Twilio to search through the incoming sms body to send different responses? https://www.twilio.com/help/faq/sms/how-do-i-build-a-sms-keyword-response-application

需要更改代码,以便它在收到的短信中搜索关键字日志记录",例如需要帮助登录",然后将发送不同的响应.

Need to alter code so that it searches the incoming SMS for keyword "logging" e.g. "Need help logging in", then the a different response will send.

/* Controller: Match the keyword with the customized SMS reply. */
function index(){
$response = new Services_Twilio_Twiml();
$response->sms("Hi. Received your message. We will contact you via email on file.");
echo $response;
}
function password(){
$response = new Services_Twilio_Twiml();
$response->sms("Hi. Received your message. We will contact you via email on file. #Password");
echo $response;
}

function logging(){
$response = new Services_Twilio_Twiml();
$response->sms("Hi. Received your message. We will contact you via email on file. #Logging");
echo $response;
}

/* Read the contents of the 'Body' field of the Request. */
$body = $_REQUEST['Body'];
/* Remove formatting from $body until it is just lowercase 
characters without punctuation or spaces. */
$result = preg_replace("/[^A-Za-z0-9]/u", " ", $body);
$result = trim($result);
$result = strtolower($result);

/* Router: Match the ‘Body’ field with index of keywords */
switch ($result) {
case 'password’':
    password();
    break;
case 'logging':
    logging();
    break;

/* Optional: Add new routing logic above this line. */
default:
    index();

}

推荐答案

来自 Twilio 的 Ricky 再次来到这里.

Ricky from Twilio here again.

很高兴您在主机上运行正常!如您所见,当前的代码示例仅在有人将确切的单词logging"作为消息正文发送时才会匹配.如果您想匹配字符串中的文本(例如:需要帮助登录"),我会使用 PHP 的 stripos 函数.有了它,你可以做这样的事情:

Glad you got things working on your host! As you've seen, the current code sample only will match if someone sends the exact word "logging" as their message body. If you want to match the text in a string (ex: "need help logging in") I would use PHP's stripos function. With that you could do something like this:

/* Read the contents of the 'Body' field of the Request. */
$body = $_REQUEST['Body'];
// Check to see if contains the word "logging"
if(stripos($body, "logging") !== FALSE) {
  // message contains the word "logging"
} else {
  // message does not contain the word "logging"
}

这篇关于twilio 短信关键字自动回复搜索传入正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆