Twilio IVR用户按键选择License功能故障排查求助
IVR系统Gather功能Digits判断逻辑失效排查
问题背景
我有一套客户服务IVR系统,流程中引导用户通过电话键盘输入数字选择持有的License。但用户输入数字后,本该调用当前文件并传递输入数字的response->gather功能失效,触发Twilio标准错误。目前仅该文件出现问题,多次调试发现只会进入else代码块,无法进入判断Digits的if逻辑块。
完整代码
<?php // Create a route that will handle Twilio webhook requests, sent as an // HTTP POST to /voice in our application require_once '../vendor/autoload.php'; require_once 'Curl.class.php'; require_once './utils/API.class.php'; API::validateMethod(['POST']); function logText($title, $content) { $logfile = fopen("./log.txt", "a"); fwrite($logfile, "\n" . $title); fwrite($logfile, "\n" . date("Y-m-d H:i:s")); fwrite($logfile, "\n" . print_r($content, true)); fwrite($logfile, "\n" . "-----------------------------------"); fclose($logfile); } function initErrorsLog() { $log_file = "errors.log"; ini_set("log_errors", true); ini_set('error_log', $log_file); } use Twilio\TwiML\VoiceResponse; // Use the Twilio PHP SDK to build an XML response $response = new VoiceResponse(); initErrorsLog(); if (array_key_exists('Digits', $_POST)) { // $rs = urldecode($_GET['rs']); $choosedLicense = $_POST['Digits']; $rsDecode = json_decode($rs); $rsDecode = json_decode($rsDecode); logText("rs decoded", $rsDecode); logText("choosedLicense", $choosedLicense); if ($choosedLicense > count($rsDecode->licenses) || $choosedLicense == 0) { $rs = urlencode($rs); $response->redirect("./invalidLicense.php?rs=$rs"); } $token = $rsDecode->auth->token; $isBlocked = $rsDecode->licenses[$choosedLicense - 1]->blocked; $licenseId = $rsDecode->licenses[$choosedLicense - 1]->id; $response->redirect("./waitForFlip.php?licenseId=$licenseId&isBlocked=$isBlocked&token=$token"); } else { $gather = $response->gather(array('numDigits' => 100, 'timeout' => '4')); $rs = urldecode($_GET['rs']); $decode = json_decode($rs); $decode = json_decode($decode); $count = 0; foreach ($decode->licenses as $license) { $count++; $gather->play("https://dl-apis.cadeadodigital.com.br/ura/assets/audios/chooseLicense$count.mp3"); $gather->play($license->nicknameAudioUrl); } $rs = urlencode(json_encode($rs)); $response->redirect("./chooseLicense.php?rs=$rs"); } // Render the response as XML in reply to the webhook request header('Content-Type: text/xml'); echo $response;
排查关键点
- Digits参数接收异常:Twilio收集用户输入后,会通过POST请求将
Digits参数传递给webhook地址。先确认API::validateMethod(['POST'])是否允许Twilio的请求,同时检查Twilio控制台中该webhook的请求方法是否设置为POST。 - 未定义变量致命错误:if代码块中注释了
$rs = urldecode($_GET['rs']);,但后续直接使用$rs变量进行JSON解析,会触发PHP未定义变量错误,导致脚本中断,无法执行后续逻辑,Twilio收到错误响应后触发标准错误。 - URL参数传递逻辑错误:else块末尾
$rs = urlencode(json_encode($rs));存在问题:$rs是urldecode后的字符串,再次json_encode会将其转为带引号的JSON字符串,后续进入if块时,两次json_decode会导致解析失败;同时当前逻辑中gather未指定action属性,却直接执行redirect,覆盖了Gather的默认提交行为,导致用户输入后无法正确回调到当前文件。 - Gather配置不合理:
gather设置numDigits => 100,远超实际场景中用户选择License所需的数字位数,会导致Twilio持续等待输入直到超时,无法及时触发提交逻辑。 - 日志记录不完整:if块的日志代码在未定义变量错误之后,导致无法记录关键参数信息,建议在if块开头添加日志记录整个
$_POST和$_GET数组,确认参数是否正常传递。
内容的提问来源于stack exchange,提问作者Iago Alexandre
相关产品推荐
相关产品推荐

