2
votes

I think I must be missing something simple here. I am trying to find a way to create conditional collect statements in the task.

I created a simple Autopilot task that is asking for a gift amount (gift_amount). In the training screen, I setup one of the phrases as "Give {gift_amount}". So someone should be able to say "Give" and it will then ask for an amount. Or, they can say "Give 10" and it should skip asking for an amount. It always seems to ask for the amount though, even if I give it in the phase. Here is the task code I created.

{
  "actions": [
    {
      collect": {
    	"name": "gift_amount",
    	"questions": [
    	  {
    	    "question": "Thank for your generosity. How much would you like to give?",
            "name": "gift_amount",
            "type": "Twilio.NUMBER"
    	  }
    	],
    	"on_complete": {
    	  "redirect": {
            "method": "POST",
            "uri": "https://webhook.site"
    	  }
    	}
      }
    }
  ]
}
2

2 Answers

1
votes

According to the docs, the format is different than the previous answer given.

https://www.twilio.com/blog/intelligent-coffee-order-system-with-twilio-autopilot

This is what worked:

exports.handler = function(context, event, callback) {
   let actions = [];
   console.log(event.CurrentTask);
   console.log(event.Field_gift_amount_Value);
   let giftAmount = event.Field_gift_amount_Value;
   const response = {
       actions: []
   }
   if (giftAmount) {
       response.actions.push({ "say": "Thanks for donating!"});
   } else {
       response.actions.push({ "say": "Cheapskate!"});
   }
   callback(null, response);
}
1
votes

Welcome to Stackoverflow! I work at Twilio.

I think what you're looking for can be done with a Dynamic Action. Instead of using a static piece of JSON in the editor, you can use a redirect to call a Twilio function that will return different action JSON depending on the context.

Your action would look something like:

{
    "actions": [
        {
            "redirect": "https://random-string-1234.twilio.com/functionname"
        }
    ]
}

Inside your Twilio function, you could check to see if the amount has already been collected. Just a heads up that this code is untested and might have typos:

exports.handler = function(context, event, callback) {
    let actions = [];

    let giftAmount = event.Field_gift_amount_Value;

    if (giftAmount) {
        actions.push({ "say": "Thanks for donating!"});
    } else {
        actions.push({ "collect" : { ... Collect JSON here }});
    }

    callback(null, { actions });
}