当前位置: 首页 > 知识库问答 >
问题:

发送一条WhatsApp/SMS消息,其中包含从使用自动驾驶仪的用户那里获得的信息

伯洋
2023-03-14

我做了一个简单的机器人,它提供信息并从使用它的用户那里收集一些数据。例如,用户可以要求指定并给出他的手机。我试图做的是发送一个WA或SMS消息到第三个号码与信息,机器人收集。这是collect部分的任务代码示例

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

共有1个答案

林修真
2023-03-14

您可以这样做:

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

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);
};
 类似资料: