【问题标题】:System Initiated IVR Call to trap Voice Response系统发起的 IVR 呼叫以捕获语音响应
【发布时间】:2021-07-01 01:22:48
【问题描述】:

我需要在 php 中创建一个 IVR 应用程序,系统会在其中启动出站呼叫并捕获来自用户的语音响应。

我该怎么做呢?

这是我的代码(不捕获用户的响应)

voice.xml 内容

<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/complex_gather.xml -->
<Response>
</Response>

completed.php 内容

file_put_contents('voice.txt',$_SERVER['QUERY_STRING']);

主代码

<?php
 
require_once 'vendor/autoload.php';

use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;


// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = "xxx";
$token = "xxx";
$twilio = new Client($sid, $token);

$call = $twilio->calls
               ->create("+1310XXXXXXX", // to
                        "+15676777774", // from
                        [
                            "method" => "GET",
                            "statusCallbackMethod" => "POST",
                            "url" => "http://xxxx.com/voice.xml"
                        ]
               );

$response = new VoiceResponse();
$gather = $response->gather(['action' => '/completed.php','method' => 'GET', 'input'=>'speech','timeout'=>3,''=>'true','speech_model'=>'phone']);
$gather->say('Enter something, or not');
echo $response;

【问题讨论】:

    标签: twilio ivr


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    当您create a call through the Twilio API 时,正如您所做的那样,您传递了一个 URL 参数。

    当呼叫连接时,Twilio 将向该 URL 发送一个 webhook (HTTP) request,并将按照以 TwiML 形式返回的说明进行操作。

    当前您的 /voice.xml 端点返回一个空的 &lt;Response/&gt;,这将导致调用挂断,因为没有要执行的 TwiML。

    您应该返回在脚本末尾生成的 TwiML,而不是从 /voice.xml 返回空的 &lt;Response/&gt;

    所以,也许voice.xml 应该是voice.php 并且看起来有点像这样:

    <?php 
    require_once 'vendor/autoload.php';
    
    use Twilio\Rest\Client;
    use Twilio\TwiML\VoiceResponse;
    
    header('Content-Type: application/xml');
    
    $response = new VoiceResponse();
    $gather = $response->gather(['action' => '/completed.php','method' => 'GET', 'input'=>'speech','timeout'=>3,''=>'true','speech_model'=>'phone']);
    $gather->say('Enter something, or not');
    echo $response;
    

    您还应该从您的 completed.php 端点返回 TwiML。

    <?php
    file_put_contents('voice.txt',$_SERVER['QUERY_STRING']);
    header('Content-Type: application/xml');
    ?>
    <Response/>
    

    【讨论】:

    • 谢谢!解决了它!
    • 如果这是正确答案,您能否将其标记为正确,以便其他人也能看到它也有帮助?谢谢!