如何在twilio的外拨电话中给用户选择并做出相应的响应?

时间:2018-03-09 21:05:27

标签: php twilio

我试图在他们的文档中找到解决方案;但我找到的最接近我的查询的是这个; https://www.twilio.com/docs/guides/how-to-gather-user-input-via-keypad-in-php#collect-user-input-with-the-gather-twiml-verb;这是为了进入的电话,而不是像我现在需要的那样的外拨电话。非常感谢任何帮助,谢谢大家提前!

2 个答案:

答案 0 :(得分:1)

这是让你入门的东西。

拨打语音电话:
// page located at http://example.com/make_call.php —>

<?php
require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]

// A Twilio number you own with SMS capabilities
$twilio_number = "+15017122661";

// Where to make a voice call (your cell phone?)
$to_number = "+15558675310";

$client = new Client($account_sid, $auth_token);
$client->account->calls->create(  
    $to_number,
    $twilio_number,
    array(
        "url" => "http://example.com/complex_gather.xml"
    )
);

https://www.twilio.com/docs/quickstart/php/programmable-voice?code-sample=code-make-a-voice-call&code-language=php&code-sdk-version=5.x

收集输入:
// page located at http://example.com/complex_gather.xml(使用内容类型XML进行响应)

<?xml version="1.0" encoding="UTF-8"?>

<Response>
    <Gather action="process_gather.php" method="POST">
        <Say>
            Please enter your account number,
            followed by the pound sign
        </Say>
    </Gather>
    <Say>We didn't receive any input. Goodbye!</Say>
</Response>

https://www.twilio.com/blog/2016/09/hitchhikers-guide-to-twilio-programmable-voice.html

流程数字:
// page located at http://example.com/process_gather.php

<?php

    if (empty($_POST["Digits"])) {
        // process digits

    } else {
        // do something

    }

?>

https://www.twilio.com/blog/2012/04/build-a-simple-phone-verification-with-twilio-php-mysql-and-jquery.html

答案 1 :(得分:1)

我完成了这个任务;我能够拨打电话并从用户的拨号器(DTMF)中获得选择;但我在用于保存这些用户输入的文件上得到解析错误;这是我正在使用的twiml对象的结构 Twilio \ Twiml对象

(
    [element:protected] => SimpleXMLElement Object
        (
            [Say] => Please enter 1 for yes, 9 for no as we try to verify your identity:
            [Gather] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [numDigits] => 1
                                    [action] => https://accounts.onsitecrm.com/test_twillo/post_script.php?q=1&id=632
                                    [method] => POST
                                )

                            [Say] => Is your full name Ammad Farooqi
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [numDigits] => 1
                                    [action] => https://accounts.onsitecrm.com/test_twillo/post_script.php?q=2&id=632
                                    [method] => POST
                                )

                            [Say] => Are the last 4-digits of your Social Security Number 1234 
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [numDigits] => 1
                                    [action] => https://accounts.onsitecrm.com/test_twillo/post_script.php?q=3&id=632
                                   [method] => POST
                               )
                            [Say] => Is your Date of Birth 24th of June, 1996?
                       )
                )
        )
)

以下是存储用户凭据的文件的结构;

$question = (int)$_GET['q'];
$leadid = (int)$_GET['id'];
$input = null;
$get = $_POST['Digits'];    //Digits Pressed

if (array_key_exists('Digits', $_POST)) {
    $x=$_POST['Digits'];
   if($x==1){
       $input = 'YES';
      //do nothing, all good
   }elseif($x==3){
       $input = 'Not Applicable';
       //do nothing
   }elseif($x==9){
       $input = 'NO';
       if($question>8){
           //call enrollment rep
       }
   }else{
       $input = "Not Recognized, Repeat The Question";
   }
}

$data = array();
$data['leadid'] = $leadid;
$data['question'] = $question;
$data['input'] = $input;
$data['get'] = $get;

$fh = fopen('/usr/share/nginx/html/usr_input.1.txt', 'a') or die("Can't open file.");
$results = print_r( $data, true);
fwrite($fh, $results);
fclose($fh);


$sql = "SELECT * FROM `tblcompcall` WHERE `leadid` = '$leadid'";
$resp = clsdatabase::runQuery($sql);
$num = mysqli_num_rows($resp);
if($num != 0){
    $sql = "UPDATE `tblcompcall` SET `q$question` = '$input' WHERE `leadid` = '$leadid'";
    $result  = clsdatabase::runQuery($sql);
}else{
    $sql = "INSERT INTO `tblcompcall` (`leadid`, `q$question`) VALUES ('$leadid', '$input')";
    $result  = clsdatabase::runQuery($sql);
}