2015-10-25 93 views
1

有人可以幫助修改Twilio建議的代碼來搜索傳入的短信主體發送不同的響應? https://www.twilio.com/help/faq/sms/how-do-i-build-a-sms-keyword-response-applicationtwilio短信關鍵字自動回覆搜索傳入正文

需要更改代碼,以便它搜索關鍵字「日誌」的傳入SMS,例如「需要幫助登錄」,然後發送不同的響應。

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

}

回答

0

瑞奇從Twilio這裏一次。

很高興你的東西在你的主機上工作!正如你所看到的,當前的代碼示例只有在有人發送確切的單詞「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" 
}