首页 > 解决方案 > 是否可以在 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 Functions 部分

编辑:添加代码。

标签: javascriptaxiosxmlhttprequesttwiliotwilio-api

解决方案


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;
  }
}

推荐阅读