1

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

需要更改代码,以便它在传入的 SMS 中搜索关键字“logging”,例如“需要帮助登录”,然后将发送不同的响应。

/* 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();

}

4

1 回答 1

0

来自 Twilio 的 Ricky 又来了。

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

/* 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"
}
于 2015-10-26T18:12:28.417 回答