javascript - 是否可以在 Twilio Functions 中使用 Axios?如果是这样,怎么做?
问题描述
我正在尝试通过配置应用程序的依赖项(如图所示)以添加 Axios 版本 0.19.2,从我的一个 Twilio Functions 与 Axios 发出 POST 请求,但唉,我仍然无法让它工作。我的问题是,我真的可以这样做还是应该不起作用?
exports.handler = function(context, event, callback) {
let responseObj = {};
let memory = JSON.parse(event.Memory);
let obj = {
prod_name: memory.twilio.collected_data.collect_order.answers.prod_name.answer || "none",
year: memory.twilio.collected_data.collect_order.answers.year.answer || "none",
make: memory.twilio.collected_data.collect_order.answers.make.answer || "none",
model: memory.twilio.collected_data.collect_order.answers.model.answer || "none",
qty: memory.twilio.collected_data.collect_order.answers.qty.answer || "none"
};
const url = 'https://htk-test.herokuapp.com/api/orders/';
// I've tried " import axios from 'axios' " but it doesn't work.
axios.post(url, obj)
.then(res => {
console.log(res.data);
})
.catch(err => {
console.log(err.message);
});
//
let msg = "Your order has been confirmed.\n"+
"------\n"+
"Details are as followed:\n"+
"\tPart: "+obj.prod_name+"\n"+
"\tYear: "+obj.year+"\n"+
"\tMake: "+obj.make+"\n"+
"\tModel: "+obj.model+"\n"+
"\tQuantity: "+obj.qty+"\n"+
"------\n"+
"We'll be in touch shortly! :D"+
"\n"+
"What else would you like to do?";
responseObj = {
"actions": [
{
"say": msg
},
{
"listen": true
}
]
};
callback(null, responseObj);
};
编辑:添加代码。
解决方案
Twilio 函数不支持“导入”语法,您需要使用require()
.
https://www.twilio.com/blog/2017/09/npm-support-for-twilio-functions.html
我还建议使用 await 语法,因为 axios 是基于 Promise 的。只需将您的函数转换为这样的异步函数即可。
exports.handler = async (event) => {
const axios = require("axios");
const memory = JSON.parse(event.Memory);
const obj = {
prod_name: memory.twilio.collected_data.collect_order.answers.prod_name.answer || "none",
year: memory.twilio.collected_data.collect_order.answers.year.answer || "none",
make: memory.twilio.collected_data.collect_order.answers.make.answer || "none",
model: memory.twilio.collected_data.collect_order.answers.model.answer || "none",
qty: memory.twilio.collected_data.collect_order.answers.qty.answer || "none"
};
const url = 'https://htk-test.herokuapp.com/api/orders/';
try {
const res = await axios.post({
method: "post",
url,
data: obj
});
console.log(res.status);
const msg = "Your order has been confirmed.\n"+
"------\n"+
"Details are as followed:\n"+
"\tPart: "+obj.prod_name+"\n"+
"\tYear: "+obj.year+"\n"+
"\tMake: "+obj.make+"\n"+
"\tModel: "+obj.model+"\n"+
"\tQuantity: "+obj.qty+"\n"+
"------\n"+
"We'll be in touch shortly! :D"+
"\n"+
"What else would you like to do?";
const responseObj = {
"actions": [
{
"say": msg
},
{
"listen": true
}
]
};
return responseObj;
} catch (error) {
console.log("An error occurred:", error)
throw error;
}
}
推荐阅读
- python - scapy的ARP属性是什么意思
- java - 使用 KStreams 有状态或无状态转换查找最大值、最小值和平均值?
- reactjs - 反应重定向与扩展不起作用
- javascript - 在 JavaScript 中将 i-- 表达式作为参数传递
- pointers - 在 SWIG 输入类型映射中释放指针数组的正确方法?
- java - 如何使 JTextField 或 JFormattedTextField 接受带小数位的数字?
- sql - Oracle SQL:按州查找最常见的姓氏
- azure-data-factory-2 - 数据工厂 V2 - 最大并发限制回填
- service - 使用 monit 签入远程主机服务
- javascript - IBM Watson Visual Recognition - 由于凭据无效,访问被拒绝