0

I have searched high and low for an answer to this, and cannot find it. I am trying trying to integrate a Twilio powered sms responder into my real estate site. So far I have it able to respond to single keywords and provide an appropriate response.

However, the way this will be used will require that the app look through an sms text string to find any keyword(s) in the string and respond appropriately.

For example, I can get the following to work perfectly fine: Single keyword 'listing' is texted in, and then it provides a response. But, if someone texts 'what can you tell me about the listing at 123 any street?' the responder will not be able to respond.

I need a way to have it find 'listing' (or any other keyword) in that string and respond to that keyword rather than ignore the whole thing.

This is what I'm working off of (obviously my keywords would be different):

<?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();
}

Found here: https://www.twilio.com/help/faq/sms/how-do-i-build-a-sms-keyword-response-application

I found 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"
}

Found here:twilio sms keyword autoresponder search incoming body

But it doesn't seem to work, or I can't figure out how to properly implement it.

FWIW, I am not a programmer, but have a limited understanding of this. I'm just trying to build my own tools because out of the box solutions don't work for me. Thanks for any help, and feel free to ask any questions you need to.

Community
  • 1
  • 1
Sam
  • 9
  • 2
  • try to pass message by `urlencode`. – urfusion Apr 06 '16 at 14:17
  • 1
    How have you tried to implement the `stripos` suggestion? That looks like the right way to me. Also, I'm more than happy to call you a programmer when you're building something like this for yourself. I think that's awesome. – philnash Apr 06 '16 at 17:59
  • 1
    Thanks! I'm looking into strpos, as I also think that it's the way to go. I guess you could say I'm a part-time programmer than lol. – Sam Apr 06 '16 at 18:38

0 Answers0