使用 axios 从 twilio 运行时获取实时 json 数据 [英] get real-time json data from twilio runtime with axios
问题描述
我正在尝试从 twilio 无服务器功能中获取实时数据.我正在使用稍微编辑的样板函数.我想要的是服务器中的 json 数据和呼叫中的语音响应连续.但以下代码没有将 json 数据发送到服务器.
I am trying to achieve real-time data from twilio server-less function. I am using a boilerplate function edited a little bit.What I want is json data in server and voice response in call consecutively .but the following code is not sending json data to server.
const axios = require('axios');
exports.handler = function (context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.say('you are welcome ');
const instance = axios.create({
baseURL: 'http://fafc4eac4162.ngrok.io/',
timeout: 3000,
});
instance
.post('/test', {
id: 1,
title: 'Twilio'
})
.then((response) => {
console.log(JSON.stringify(response.data));
})
.catch((error) => {
console.log(error);
return callback(error);
});
return callback(null, twiml);
};
它显示以下错误,但如果我不使用语音响应回调 return callback(null, twiml) 而是使用简单的 return callback(null, response.data),它会成功发送数据;
It shows below error,but it sends data successfully if I do not use the voice response callback return callback(null, twiml) and rather use simple return callback(null, response.data);
{"message":"timeout of 3000ms exceeded","name":"Error","stack":"Error: timeout of 3000ms
exceeded\n at createError (/var/task/node_modules/axios/lib/core/createError.js:16:15)\n
at RedirectableRequest.handleRequestTimeout
(/var/task/node_modules/axios/lib/adapters/http.js:280:16)\n at Object.onceWrapper
(events.js:286:20)\n at RedirectableRequest.emit (events.js:198:13)\n at
Timeout._onTimeout (/var/task/node_modules/follow-redirects/index.js:166:13)\n at
ontimeout (timers.j...
推荐答案
return callback(null, twiml);
应该在 .then
块中.>
The return callback(null, twiml);
should be in the .then
block.
.then((response) => {
console.log(JSON.stringify(response.data));
return callback(null, twiml);
})
此外,该错误表明已达到 3000 毫秒超时,您的应用程序是否返回 200-OK?
Also, the error indicates the 3000ms timeout is hit, is your application returning a 200-OK?
这篇关于使用 axios 从 twilio 运行时获取实时 json 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!