0

I am trying to use Twilio (still in testing phase / trial account) to ask questions like a survey. I found the tutorial on how to create context base answers but I can not figure out how to get it to ask a second question.

The following code is PHP from their tutorial.

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

Could I wrap this code in a function, call the function immediately, then call another function inside one of the functions? I tried this and it never gets to the second questions. For example:

Someone sends a text to the number. They get the animal question, they answer "dog", get the response and then I want it to go to function question2().

function question1(){

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

switch code...
}

function question2(){
#another question answer set here leading to more questions based on answers.
}


question();

Any help is appreciated. I know there are platforms for this but I want to see how manually building something like this works out.

Thanks, Eric

EDIT: Clarification. I want to have a flow of questions based on responses. When someone answers a question, I want to provide a response and then ask a second, third, fourth question.

Eric.18
  • 463
  • 4
  • 20
  • I am pretty sure `monkey()`, `dog()`, `pigeon()`, `owl()` can be combined into a single function: `animal(animal_name)` (with animal name as the argument). – Maximus2012 Feb 25 '16 at 19:25
  • I agree that they could. This is their code, probably simplified for tutorial purposes. This is not really my issue / question but thank you. – Eric.18 Feb 25 '16 at 19:33
  • I think if you start by simplifying the function like that, then you can possibly use the same or a similar function for the 2nd part of your problem which is what you need. – Maximus2012 Feb 25 '16 at 19:49
  • You can possibly extend the function to add additional argument: `animal(animal_type, response_level)`. See if that approach might work. – Maximus2012 Feb 25 '16 at 19:51

1 Answers1

0

While you can use nested functions in PHP, I don't think it provides any help. Their use seems a bit debatable (What are PHP nested functions for?) unless for the purpose of using anonymous functions.

Doesn't this work?:

//user responds with 'dog'.

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;
    question2();

}

function question2(){
    $response = new Services_Twilio_Twiml();
    $response->sms("Reply with one of the following keywords: 
       cat, mouse, rat, worm.");
    echo $response;
}

/* Router: Match the ‘Body’ field with index of keywords */
/* This can be extended to use as many keywords as you want, regardless of which question you are on.*/
switch ($result) {
    case 'monkey':
        monkey();
        break;
    case 'dog':
        dog();
        break;
    case 'pigeon':
        pigeon();
        break;
    case 'owl':
        owl();
        break;
    case 'rat':
         rat();
         break;
    case 'cat':
          cat();
          break;
    case 'worm':
          worm();
          break;
    /*Etc, ad infinitum*/
    default:
        index();
}

As others have said - using a switch apparatus with a different function for each animal is a bit laborious in real life. You'd probably want to replace that with something more sensible. However, I'm just trying to provide something that does what you want.

Community
  • 1
  • 1
fred2
  • 1,015
  • 2
  • 9
  • 29
  • Thanks for your input but this does not work. I will have to look into the documentation a bit deeper. It will never get to question 2 even after dog is chosen. I assume this has to do with Twilio's services and response calls. I also assume there is an issue with you code around question 2, with no function declaration. – Eric.18 Feb 26 '16 at 12:24
  • I'd investigate the responses from Twilio and also any PHP errors. If function 'dog()' fires, then 'question2()' must also fire (unless a syntax error prevents it). The question then is why SMS messages are not being received as a result of $response->sms("Reply with one of the following keywords: cat, mouse, rat, worm."); – fred2 Feb 26 '16 at 16:13
  • Thanks. I will look into it more. When I do respond with cat (any animal) it will send the corresponding switch function but it will never fire question 2 afterwards. – Eric.18 Feb 26 '16 at 16:15
  • I didn't explicitly state this, but you understand that you need to combine my code with their tutorial, right? – fred2 Feb 26 '16 at 16:20
  • Yes, I understood that but it has to be something with Twilio services or API. The part that basically kills the flow (stops responses) is having the call to question 2 in the dog() function. Without that call it works fine and with the call it will not respond at all... I test it at the php page in the browser and it echos the initial function but the sms portion doesn't pass the response. Thanks again. – Eric.18 Feb 26 '16 at 16:33