2

I'm trying to see about using Twilio Serverless to replace my IVR. I would like to have some centralized functions to use within the functions.

For example, My main may be something like /MainMenu, which will have all the Twml.

but it will also need to call a function like /LogStats, which will be a function that does a REST Call to my API to collect Stats.

I'd appreciate your guidance in this. I'm also a little confused about why there's a Functions Classic, and a Functions Services. Am I to assume that Functions Classic will go away?

Thanks

Update from comments

Hi Lizzie, thanks for your response. I have it working with the zoltar example.. but when I try to use it for creating a call to a REST API, it's not consistently calling the API.. Any Ideas?

Here's what I'm talking about..

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;
philnash
  • 70,667
  • 10
  • 60
  • 88
Santy
  • 111
  • 1
  • 1
  • 2

1 Answers1

2

Twilio developer evangelist here.

The Functions Classic are the older Functions with the older Functions UI. It still works, but Functions Services are newer and recommended to use. A Service is an application container to store all your Functions and Assets, and used to manage deployments and separate environments. You will likely create a new Service for each new project you work on.

You can use 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();
}

Let me know if this helps at all!

lizziepika
  • 3,231
  • 1
  • 14
  • 23
  • 1
    Hi Lizzie, thanks for your response. I have it working with the zoltar example.. but when I try to use it for creating a call to a REST API, it's not consistently calling the API.. Any Ideas? – Santy Aug 20 '21 at 00:20
  • Here's what I'm talking about.. `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;` – Santy Aug 20 '21 at 00:38
  • 1
    My bet is that you are not waiting for the result of this function call before you call the Function's `callback` method. Once you call `callback` the Function is shutdown and unfinished asynchronous calls are cancelled. Make sure you either use `async/await` when running `log.ask` or place the call to `callback` in the `then/catch` functions. – philnash Aug 20 '21 at 06:22
  • thank you @lizziepika for this answer and for sharing the documents. that did my project. – alex May 07 '22 at 12:50