使用 Twilio 的多个问题

问题描述 投票:0回答:1

我正在尝试使用 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();
}

我可以将此代码包装在一个函数中,立即调用该函数,然后在其中一个函数中调用另一个函数吗?我尝试过这个,但它永远不会涉及第二个问题。例如:

有人向该号码发送短信。他们得到动物问题,他们回答“狗”,得到响应,然后我希望它进入函数 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();

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

谢谢, 艾瑞克

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

php twilio twilio-php
1个回答
0
投票

虽然您可以在 PHP 中使用嵌套函数,但我认为它没有提供任何帮助。它们的使用似乎有点有争议(PHP 嵌套函数有什么用?),除非是为了使用匿名函数。

这行不通?:

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

正如其他人所说,在现实生活中,为每只动物使用具有不同功能的开关装置有点费力。您可能想用更合理的东西来代替它。但是,我只是想提供一些可以满足您需求的东西。

© www.soinside.com 2019 - 2024. All rights reserved.