【问题标题】:How to call a Function from a Function in Twilio Serverless?如何从 Twilio Serverless 中的函数调用函数?
【发布时间】:2021-08-19 15:14:44
【问题描述】:

我正在尝试了解如何使用 Twilio Serverless 来替换我的 IVR。我想在函数中使用一些集中的函数。

例如,我的 main 可能类似于 /MainMenu,它将包含所有 Twml。

但它还需要调用 /LogStats 之类的函数,该函数将对我的 API 进行 REST 调用以收集统计信息。

非常感谢您在这方面的指导。我也有点困惑,为什么会有 Functions Classic 和 Functions Services。我是否认为 Functions Classic 会消失?

谢谢

来自 cmets 的更新

嗨,莉齐,感谢您的回复。我让它与 zoltar 示例一起使用.. 但是当我尝试使用它来创建对 REST API 的调用时,它并没有始终如一地调用 API.. 有什么想法吗?

这就是我要说的......

const axios = require('axios');
const log = {
  ask: async function(event){
    try{
      const res = await axios.post('https://myid.ngrok.io/api/calllogger/lognewcall', {
        CallSid: event.CallSid,
        Caller: event.Caller,
        App: "EmgLine",
        CallerCity: event.CallerCity,
        CallerState: event.CallerState
      });
      if(!res.ok){
        throw new Error(`HTTP error! Status ${res.status}`);
      }
      const data = await res.Message;
      return data;
    } catch(err){
      const errMessage = `test: ${err}`;
      return errMessage;
    }
  }
};

module.exports = log;

【问题讨论】:

    标签: twilio twilio-functions


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    Functions Classic 是具有旧版 Functions UI 的旧版 Functions。它仍然有效,但 Functions Services 更新并建议使用。服务是一个应用程序容器,用于存储您的所有功能和资产,并用于管理部署和单独的环境。您可能会为您从事的每个新项目创建一个新服务。

    您可以使用code from another Function in another Function with code like this

    exports.handler = function (context, event, callback) {
      // First, get the path for the Function. Note that the key of the function
      // is not preceded by a "/" as is the case with Assets
      const zoltarPath = Runtime.getFunctions()['zoltar'].path;
        
      // Next, use require() to import the library
      const zoltar = require(zoltarPath);
        
      // Finally, use the module as you would any other!
      console.log('The answer to your riddle is: ' + zoltar.ask());
        
      return callback();
    }
    

    如果这有帮助,请告诉我!

    【讨论】:

    • 嗨,莉齐,感谢您的回复。我让它与 zoltar 示例一起使用.. 但是当我尝试使用它来创建对 REST API 的调用时,它并没有始终如一地调用 API.. 有什么想法吗?
    • 这就是我所说的......const axios = require('axios'); const log = { ask: async function(event){ try{const res = await axios.post('https://myid.ngrok.io/api/calllogger/lognewcall',{CallSid: event.CallSid,Caller: event.Caller,App: "EmgLine",CallerCity: event.CallerCity, CallerState: event.CallerState}); if(!res.ok){throw new Error(HTTP 错误!状态 ${res.status});} const data = await res.Message; return data; } catch(err){ const errMessage = test: ${err}; return errMessage; } } }; module.exports = log;
    • 我敢打赌,在调用函数的callback 方法之前,您不会等待此函数调用的结果。一旦您调用callback,函数就会关闭,未完成的异步调用将被取消。确保在运行log.ask 时使用async/await 或在then/catch 函数中调用callback
    猜你喜欢
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 2020-04-13
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2017-08-14
    相关资源
    最近更新 更多