Hi guys, I am trying to create an sms auto responder for my business based on the Twilio responder :
[php]<?php
/* Include twilio-php, the official Twilio PHP Helper Library,
- which can be found at
-
http://www.twilio.com/docs/libraries
*/
include(‘Services/Twilio.php’);
/* Controller: Match the keyword with the customized SMS reply. */
function index(){
$response = new Services_Twilio_Twiml();
$response->sms(“Reply with one of the following keywords:
monkey, dog, pigeon, owl.”);
echo $response;
}
function monkey(){
$response = new Services_Twilio_Twiml();
$response->sms(“Monkey. A small to medium-sized primate that
typically has a long tail, most kinds of which live in trees in
tropical countries.”);
echo $response;
}
function dog(){
$response = new Services_Twilio_Twiml();
$response->sms(“Dog. A domesticated carnivorous mammal that
typically has a long snout, an acute sense of smell, and a barking,
howling, or whining voice.”);
echo $response;
}
function pigeon(){
$response = new Services_Twilio_Twiml();
$response->sms(“Pigeon. A stout seed- or fruit-eating bird with
a small head, short legs, and a cooing voice, typically having gray and
white plumage.”);
echo $response;
}
function owl(){
$response = new Services_Twilio_Twiml();
$response->sms(“Owl. A nocturnal bird of prey with large
forward-facing eyes surrounded by facial disks, a hooked beak,
and typically a loud call.”);
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 ‘monkey’:
monkey();
break;
case ‘dog’:
dog();
break;
case ‘pigeon’:
pigeon();
break;
case ‘owl’:
owl();
break;
/* Optional: Add new routing logic above this line. */
default:
index();
}[/php]
I can get this to work just fine, but I’d like it to be able to pick keywords out of a sentence and respond, instead of needing to process single words at a time. Obviously, I will be changing the keywords and the appropriate responses, but I just want to get it working first.
As of right now, if you text any one of those words (owl, monkey, dog, pigeon) by itself it will respond accordingly. If you send one of those words in a sentence (“How much does that pigeon weigh?” for example), it will just respond with the default index. I’d like to be able to set an array of words, have the app check an incoming text against the array of words and if any keyword is in the sentence, it will activate the appropriate response just like it would for a single word. I’ve done a ton of reading on this, and think I may have narrowed down to ‘in_array’ being the best option, but cannot seem to get it to work. I admit that I’m a total newbie to this, and am trying to teach myself code as I go in order to solve my own problem. Can anyone help me make this work? Thanks, and feel free to ask any questions if you need clarification.