2

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"
    	  }
    	}
      }
    }
  ]
}

1

2 Answers 2

1

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

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 });
}
3
  • 1
    it looks like event.Memory doesn't contain a "collected_data" object. But the form post parameters can be accessed with "event.CurrentTask" or "event.Field_custom_field_Value". Commented Sep 6, 2019 at 21:14
  • 1
    @JeffDoolittle thanks for tracking that down - I've updated my code sample as well. Commented Sep 6, 2019 at 21:59
  • Thanks @CharlieWeems Commented Sep 11, 2019 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.