Twilio 返回twiml时出现TwiIO节点语音响应错误
我已经编写了一个twilio函数,它会吐出一些twiml来读取用户当前的天气。然而,在我的代码之外的某个地方,twiml生成失败了 代码的相关位是第77-79行 第77行的console.log将以下内容转储到stdin:Twilio 返回twiml时出现TwiIO节点语音响应错误,twilio,twilio-api,twilio-twiml,Twilio,Twilio Api,Twilio Twiml,我已经编写了一个twilio函数,它会吐出一些twiml来读取用户当前的天气。然而,在我的代码之外的某个地方,twiml生成失败了 代码的相关位是第77-79行 第77行的console.log将以下内容转储到stdin: twiml: <?xml version="1.0" encoding="UTF-8"?><Response><Say voice="Polly.Joanna">Hi&l
twiml: <?xml version="1.0" encoding="UTF-8"?><Response><Say voice="Polly.Joanna">Hi</Say><Say voice="Polly.Joanna">According to OpenWeatherMap the station nearest Berkeley is currently scattered clouds
with a temperature ranging from 57.6 Fahrenheit to 60.01 degrees Fahrenheit and a relative humidity of
43% with an average wind speed of: 5.82 miles per hour</Say></Response>
twiml:hia根据OpenWeatherMap,距离伯克利最近的车站目前是分散的云层
温度从57.6华氏度到60.01华氏度,相对湿度为
43%,平均风速为:5.82英里/小时
然后框架会抛出一个XML转换错误,但据我所知,我做的一切都是正确的,有什么帮助/想法吗
(node:20051) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
--> starting at object with constructor 'XMLDocument'
| property 'children' -> object with constructor 'Array'
| index 0 -> object with constructor 'XMLDeclaration'
--- property 'parent' closes the circle
at JSON.stringify (<anonymous>)
at stringify (/Users/vat/.twilio-cli/node_modules/express/lib/response.js:1123:12)
at ServerResponse.json (/Users/vat/.twilio-cli/node_modules/express/lib/response.js:260:14)
at ServerResponse.send (/Users/vat/.twilio-cli/node_modules/express/lib/response.js:158:21)
at handleSuccess (/Users/vat/.twilio-cli/node_modules/twilio-run/dist/runtime/route.js:116:9)
at callback (/Users/vat/.twilio-cli/node_modules/twilio-run/dist/runtime/route.js:164:13)
at exports.handler (/Users/vat/workspace/machinshin/projects/idwresearch/dist/functions/get_say_weather.protected.js:71:16)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:20051) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:20051) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(节点:20051)未处理的PromisejectionWarning:TypeError:将循环结构转换为JSON
-->从构造函数为“XMLDocument”的对象开始
|属性'children'->构造函数为'Array'的对象
|索引0->具有构造函数“XMLDeclaration”的对象
---属性“parent”关闭该圆
在JSON.stringify()上
在stringify(/Users/vat/.twilio cli/node_modules/express/lib/response.js:1123:12)
位于ServerResponse.json(/Users/vat/.twilio cli/node_modules/express/lib/response.js:260:14)
在ServerResponse.send(/Users/vat/.twilio cli/node_modules/express/lib/response.js:158:21)
在handleSuccess(/Users/vat/.twilio cli/node_modules/twilio run/dist/runtime/route.js:116:9)
回调时(/Users/vat/.twilio cli/node_modules/twilio run/dist/runtime/route.js:164:13)
在exports.handler(/Users/vat/workspace/machin/projects/idwresearch/dist/functions/get\u say\u weather.protected.js:71:16)
在处理和拒绝时(内部/process/task_queues.js:97:5)
(节点:20051)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。要在未处理的承诺拒绝时终止节点进程,请使用CLI标志“---unhandled rejections=strict”(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (拒绝id:1)
(节点:20051)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。
以下内容对我来说毫无错误:
const axios = require('axios');
const qs = require('querystring');
const jsLog = (obj) => (JSON.stringify(obj, null, 2));
const voice = 'Polly.Joanna';
const OW_URL ='https://api.openweathermap.org/data/2.5/weather';
async function getWeatherForZipcode (OW_APP_ID, zipcode, countryCode, units) {
const zip = (`${zipcode},${countryCode}`).toLowerCase().trim();
return axios.get(OW_URL, {
params: {
appid: OW_APP_ID,
units: units || 'imperial',
zip,
}
}).then((res) => {
return res.data;
}).catch((err) => {
console.error(`getWeatherForZipcode: error: ${jsLog(err)}`);
throw new Error(err);
});
}
/** parseWeatherResponse
* @param
*
**/
function parseWeatherResponse(data) {
return {
current_conditions: data.weather[0].description,
station_at: data.name,
temp: data.main.temp,
temp_max: data.main.temp_max,
humidity: data.main.humidity,
wind_speed: data.wind.speed,
};
}
/** handler
* @param {Context} context
* @param {Event} event
* @param {ServerlessCallback} callback
* @returns {twiml.VoiceResponse}
**/
exports.handler = async function (context, event, callback) {
try {
context.callbackWaitsForEmptyEventLoop = false;
console.log(`-------------------\n`);
const OW_APP_ID = context.OW_APP_ID;
const { units, zip, countryCode } = event;
const z = zip.toLowerCase().trim();
const cc = countryCode.toLowerCase().trim();
const uu = units.toLowerCase().trim();
const response = await getWeatherForZipcode(OW_APP_ID, z, cc, uu);
const toSay = parseWeatherResponse(response);
// const twiml = new VoiceResponse();
const twiml = new Twilio.twiml.VoiceResponse();
const say = twiml.say({ voice: 'Polly.Joanna', }, 'Hi');
twiml.say({voice}, `According to OpenWeatherMap the station nearest ${toSay.station_at} is currently ${toSay.current_conditions}
with a temperature ranging from ${toSay.temp} Fahrenheit to ${toSay.temp_max} degrees Fahrenheit and a relative humidity of
${toSay.humidity}% with an average wind speed of: ${toSay.wind_speed} miles per hour`);
console.log(`twiml: ${twiml.toString()}`);
return callback(null, twiml);
} catch (err) {
throw err;
console.log(`some error was thrown: ${jsLog(err)}`);
return callback(`err: ${jsLog(err)}, event: ${jsLog(event)}`);
}
}
谢谢我看到了区别。。您删除了“require('twilio')”,因此代码如何使用“内置”库版本。。我想知道行为上的区别是什么..您可以通过更新Twilio函数中的依赖项(在函数V2的依赖项下)来更改内置的Twilio Helper库,以指向更新的版本。可以使用以下命令实例化内置库:
const client=context.gettwillioclient()代码>,不需要它。Ps。代码很好,非常有条理,易于理解:)