0

I've made a simple bot that gives information and collects some data from the users that use it. For example a user can ask for an appoinment and give his cellphone. What I'm trying to do is send a WA or a SMS message to a third number with the information that the bot collected. This is a sample of the Task code for the collect part

"collect": {
            "name": "schedule_appt",
            "questions": [
                {
                    
                {
                    "question": "Let us your phone number and an expert will get in touch with you",
                    "name": "appt_phone_number",
                    "type": "Twilio.PHONE_NUMBER"
                }
            ]

Now, I want to send a WA or SMS message with the value of "Twilio.PHONE_NUMBER" to another number. Is it possible to do with the options that twilio give in the bot creator or should I create some custom code for this situation? If so, which would be the best way to do it? A PHP app, or something Web developed?

I'm kinda lost so any advice would be appreciated.

Thanks!

OmLp
  • 1
  • 1

1 Answers1

1

You can do something like this:

{
    "actions": [
        {
            "collect": {
                "name": "schedule_appt",
                "questions": [
                    {
                        "question": "Let us your phone number and an expert will get in touch with you",
                        "name": "appt_phone_number",
                        "type": "Twilio.PHONE_NUMBER"
                    }
                ],
                "on_complete": {
                    "redirect": {
                        "method": "POST",
                        "uri": "https://xyz.twil.io/sostub"
                    }
                }
            }
        }
    ]
}

Twilio Function (destination for URL:https://xyz.twil.io/sostub match to your unique Twilio Function domain)

exports.handler = async function(context, event, callback) {
    let twilioClient = context.getTwilioClient();
    let memory = JSON.parse(event.Memory);
    console.log("User Identifier: "+ event.UserIdentifier);
    console.log("Task: "+ event.CurrentTask);
    console.log(event.CurrentInput);
    let message = "Your Message Has been Sent!";
    let responseObject = {
        "actions": [
            {
                "say": message
            }]
    };
    
    let sendSMS = () => { 
        try {   
        let result = twilioClient.messages
        .create({
           body: `Appointment Scheduled, Mobile Phone ${event.CurrentInput}`,
           to: '+14075551212',
           from: '+15095551212',
         });
         return result;
        } catch(e) {
          console.log(e);
          callback(e);
        }
    };
    
    let result = await sendSMS();    
    
    callback(null, responseObject);
};
Alan
  • 10,465
  • 2
  • 8
  • 9