我正在使用Twilio,借助电话号码和网络挂钩将呼叫转发到本地Node.js应用程序。 Ngrok
用于将呼叫通过http端口1337
转发到本地运行的Node.js应用程序。
调用将正确转发到本地应用程序,并带有默认的传出响应。但是,当twiml.gather
将请求转发到fact-command
块时,语音识别代码不起作用。
代码如下:
const http = require('http');
const VoiceResponse = require('twilio').twiml.VoiceResponse;
http
.createServer((req, res) => {
// Create TwiML response
const twiml = new VoiceResponse();
// twiml.say('Hello from your pals at Twilio! Have fun.');
twiml.gather({
input: 'speech',
timeout: 3,
hints: 'cat, numbers, chuck norris',
action: '/fact-command' // Route to a twilio Speech Recognition event handler
}).say('Welcome to the Twilio Facts hotline. Please say cat for cat facts, number for trivia about numbers, or chuck norris for a random chunk of chuck norris knowledge.');
res.writeHead(200, { 'Content-Type': 'text/xml' });
res.end(twiml.toString());
var url = req.url;
if(url ==='/fact-command'){
console.log("This is a Twilio route");
exports.handler = function(context, event, callback) {
const twiml = new Twilio.twiml.VoiceResponse();
const command = event.SpeechResult.toLowerCase();
twiml.say(`You said ${command}. I'll give you a ${command} fact.`); // Speech is not getting recognized
callback(null, twiml);
};
}
})
.listen(1337, '127.0.0.1');
console.log('TwiML server running at http://127.0.0.1:1337/');
如何使用TwiML和本地Node.js应用程序识别语音并将其存储在变量中?