使用从自动驾驶仪用户处获取的信息发送Whatsapp / SMS消息

时间:2020-08-20 00:11:40

标签: twilio twilio-api twilio-php twilio-programmable-chat

我制作了一个简单的机器人,该机器人可以提供信息并从使用它的用户那里收集一些数据。例如,用户可以要求约住并给他的手机。我想做的是向该机器人发送WA或SMS消息到第三个号码,其中包含机器人收集的信息。这是收集部分的任务代码的示例

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

现在,我想发送WA或SMS消息,其值为“ Twilio.PHONE_NUMBER”到另一个号码。是否可以使用twilio在机器人创建者中提供的选项进行操作,还是应该为这种情况创建一些自定义代码?如果是这样,那将是最好的方法?一个PHP应用程序,还是Web开发的东西?

我有点迷茫,所以任何建议都将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

{
    "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(URL的目标:https://xyz.twil.io/sostub与您唯一的Twilio Function域匹配)

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);
};