Search code examples
twiliotwilio-apitwilio-twimltwilio-programmable-chat

If /else statement in Twilio Function always return false


I want to test my if else condition in the Twilio function. The condition is if the user will text "Hi", Twilio will send him a quote. The problem the condition always runs false even if I texted "Hi". I'm not sure if the event.Body is the right code to get the value of the message body.

exports.handler = function (context, event, callback) {
 const got = require('got');
 
 got('https://cyan-sparrow-7490.twil.io/assets/quote.json').then(response =>{
     let messageBody = event.Body;
     
     const qotd = JSON.parse(response.body);
     let quote = qotd.contents.quotes[0];
     const quoteEffect = messageBody === "Hi"? callback(null,quote): callback(null,null);
     callback(null,quoteEffect);
 });
};

Solution

  • Step 1,

    • Use the split Based Widget and input to the "Variable to Test" trigger.message.Body enter image description here

    • Then add a condition in the Split Based Widget that could be found in the Transitions tab, if the incoming messages is equal to "Hi" then it will proceed to the next widget

    enter image description here

    Step 2,

    • Then add a message widget that tells the sender Fetching quote, the purpose of this is just to tell the sender that your processing the quoted message. This is just optional, you can remove this if you want.

    enter image description here

    Step 3

    • Then create a function to parse the external API for the quoted message. Make sure to add the got module as Dependencies enter image description here

    enter image description here

    enter image description here

    Step 4

    • And Lastly Parsed the Quote in the Body of the Send Message widget.

    enter image description here