Search code examples
twiliotwilio-apitwilio-programmable-chattwilio-functions

Check Twilio autopilot user input against multiple Twilio Types and custom Field Types after collect


I hope you are well and healthy. I am trying to check the user input in a Twilio function for a specific "collect" from autopilot against multiple Field Types which some are custom.

For instance, I ask the user for their name first and then their phone number. The set types for these collect questions are Twilio.FIRST_NAME and Twilio.PHONE_NUMBER. However, the user might say "connect me to someone" on any of these 2 questions which will result in a failure because "connect me to someone" is not a name or a phone number. I want to be able to somehow handle a case like that which provides me control over the flow of the conversation but also be able to detect specific phrases and handle it properly. In this instance, connect them to someone.

Here is my Twilio function that executes after the initiation task:

exports.handler = function (context, event, callback) {
    var responseObject;

        responseObject ={
            "actions": [
                {
                    "collect": {
                        "name": "details",
                        "questions": [
                            {
                                "question": "May I get your first name?",
                                "name": "name",
                                "type": "Twilio.FIRST_NAME",
                                "validate": {
                                    "on_failure": {
                                        "messages": [
                                            {
                                                "say": "Sorry, that doesn't seem to be right, can you please try again?"
                                            }
                                        ],
                                        "repeat_question": true
                                    },
                                    "max_attempts": {
                                        "redirect":{
                                            "method": "POST",
                                            "uri": "https://quartz-salamander-1024.twil.io/call-forwarding"
                                        },
                                        "num_attempts": 2
                                    }
                                }
                            },
                            {
                                "question": "Can I get your contact number?",
                                "name": "phoneNumber",
                                "type": "Twilio.PHONE_NUMBER",
                                "validate": {
                                    "on_failure": {
                                        "messages": [
                                            {
                                                "say": "Sorry, that doesn't seem to be right, can you please try again?"
                                            }
                                        ],
                                        "repeat_question": true
                                    },
                                    "max_attempts": {
                                        "redirect":{
                                            "method": "POST",
                                            "uri": "https://quartz-salamander-1024.twil.io/call-forwarding"
                                        },
                                        "num_attempts": 2
                                    }
                                }
                            },
                        ],
                        "on_complete": {
                            "redirect": {
                                "method": "POST",
                                "uri": "https://quartz-salamander-1024.twil.io/confirmation"
                            }
                        }
                    }
                }
            ]
        }
    callback(null, responseObject);
}

I thought of creating multiple tasks for each question however I intend on adding more questions that I must collect their answers and validate in some fashion and by introducing multiple tasks I lose control over the flow of the conversation.

Thank you


Solution

  • I also had a similar problem where I needed to listen for a yes/no value and the name of an employee at our company at the same time. I came up with sort of a workaround solution that will hopefully be able to solve your problem as well:

    1. Got to your Autopilot's field types (https://www.twilio.com/console/autopilot/your_bot/field-types).
    2. Create a custom field type and name it Get_First_Name (or whatever you'd like).
    3. For your Value field, you'll want two values: First_Name and Connect_To_Someone
    4. In your First_Name value field, add in some synonyms that are akin to what the built-in Twilio.FIRST_NAME field type is (e.g. "John", "Dave", "My name is Bob", "I am Joe", etc.).
    5. For your Connect_To_Someone value field, add in synonyms that you'd like to trigger your event (e.g. "I'd like to talk to someone", "Can you connect me to a human", etc.).
    6. Where you are using Twilio.FIRST_NAME, use Get_First_Name instead. Your Get_First_Name field will now give you one of two valid values back: First_Name or Connect_To_Someone.

    The downside of this is that you'll have to split up your "collect" questions and redirect the task to another function that will handle the logic to say "If answer is Connect_To_Someone, then connect them to someone" or "If the user gave a first name, then ask for a phone number". Then, do the same song and dance for phone number.

    In essence, you'll now have 5 functions:

    1. Ask for first name
    2. Read whether or not the user entered a first name or asked to be connected
    3. Connect user to someone
    4. Ask for phone number
    5. Read whether or not the user entered a phone number or asked to be connected