Twilio functions calling other Twilio functions

2.1k Views Asked by At

My Twilio function is in danger of getting too large and unwieldy. I'd like to break it up into smaller functions, and have the 'master' function call the other functions to get data as needed.

I don't see anything in the documentation about this, and the few tests I've tried have not been successful. Is there an easy/best way to go about doing this? Thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

this is an example of how to include code from another function:

including function's body

exports.handler = function(context, event, callback) {
    let path = Runtime.getFunctions().helper.path;
    let helper = require(path);
    let output = helper.output_init();
}

included function's body (the name of this function needs to be 'helper' to work on this example)

function output_init(){
    let output = new Twilio.Response();
    output.setStatusCode(200);
    output.appendHeader('Content-Type', 'application/json');
    return output;
}
module.exports ={
    output_init: output_init,
};

hope this helps

0
On

there is a discussion around this topic on a Google Groups forum and the details are provided from the documentation below:

Runtime Client

https://www.twilio.com/docs/runtime/client#functions

"The Function object enables developers to compose complex applications by allowing them to include and execute code stored in multiple Functions."