Search code examples
twiliotwilio-functions

Twilio functions calling other Twilio functions


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!


Solution

  • 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