使用Twilio

时间:2016-02-25 19:23:04

标签: php twilio-php

我正在尝试使用Twilio(仍处于测试阶段/试用帐户)来提出调查等问题。我找到了关于如何创建上下文基础答案的教程,但我无法弄清楚如何让它提出第二个问题。

以下代码是他们教程中的PHP。

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

我可以将这段代码包装在一个函数中,立即调用该函数,然后在其中一个函数中调用另一个函数吗?我试过这个,它从来没有得到第二个问题。例如:

有人向该号码发送文字。他们得到动物问题,他们回答“狗”,得到回应,然后我希望它转到功能问题2()。

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

感谢任何帮助。我知道有这样的平台,但我想看看如何手动构建这样的东西。

谢谢, 埃里克

编辑:澄清。我希望根据回复提出一系列问题。当有人回答问题时,我想提供回复,然后提出第二,第三,第四个问题。

1 个答案:

答案 0 :(得分:0)

虽然您可以在PHP中使用嵌套函数,但我认为它不提供任何帮助。除非出于使用匿名函数的目的,否则它们的使用似乎有点值得商榷(What are PHP nested functions for?)。

这不起作用吗?:

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

正如其他人所说 - 在每个动物中使用具有不同功能的开关装置在现实生活中有点费力。你可能想用一些更明智的东西取而代之。但是,我只是想提供能够满足您需求的东西。

相关问题