Search code examples
twiliotwilio-functionstwilio-studio

Twilio studio plus function, can't use string.split()


I can't split up a string using Functions. This is an SMS app in Studio:
User texts his full name to Twilio, I call a function and add the Liquid variable with their full name and send it to a Function where I want to return only the first name.

exports.handler = function(context, event, callback) {
    var firstName = event.fullName.split(' ');
    callback(null, firstName[0]);
};

error message:Cannot read property 'split' of undefined


Solution

  • First, your code for the function is fine, except that event.fullName is undefined because is not being passed from the Studio.

    In Studio, add a fullName parameter to the function call. The value for the parameter is {{trigger.message.Body}} (to pass the incoming message text body to the function), then you will be able to access it in your function.

    Note: In the "Function Parameters" section of the "RUN FUNCTION" widget there are two "Save" buttons, you will need to click them both, first the one for the parameters, then the (red) one for the widget.

    Here is a screen capture which might help you

    enter image description here