1

我正在通过 Bot Scripting 和 Flow Maker 在 Gupshup 中构建潜在客户生成机器人。我对 JS 很陌生,完全被脚本工具中提供的 botscript 语言弄糊涂了。我正在使用快速响应按钮来创建一个小菜单。

如果用户没有响应任何内容(即检测到空意图),是否有办法在一段时间后触发状态更改(:call 或 :goto)?也许有一个已知的解决方法涉及使用机器人输出状态?

太感谢了。

我尝试过使用处理程序,但很快发现用户输入处理程序只有在用户做出响应时才会触发。

function revie_menu_Handler (options, event, context, callback){

    console.log("user input is:" + event.message.toLowerCase())

    if(event.message.toLowerCase() === "") {
        options.next_state = 'if_silent';
    } else if (event.message.toLowerCase() === "make me an offer") {
        options.next_state = 'revie_UI_1'
    } else if (event.message.toLowerCase() === "i have a question") {
        options.next_state = 'revie_UI_2'
    } else {
        options.next_state = 'blank'
    }

这对应于botscript:

revie_menu: Welcome to the menu! [[Make me an offer, I have a question]]
    revie_UI_1: Make me an offer:call default.menu_offer
    revie_UI_2: I have a question:call default.menu_question
    if_silent:silent:call default_revie_timeout
    blank: :onException
        Exception! What happened...?
4

1 回答 1

0

您提到的用例无法使用机器人脚本工具中的标签处理程序来实现。

您的问题的一种可能解决方案是 cron 服务。假设您有一个如下所示的机器人脚本:

revie_menu: Welcome to the menu! [[Make me an offer, I have a question]]
    revie_UI_1: Make me an offer
        :call default.menu_offer
    revie_UI_2: I have a question
        :call default.menu_question

用户在 Facebook messenger 上与您的机器人发起对话,在 EventHandler 函数中接收到startchatting事件。使用高级数据持久性创建一个“发件人”对象,该对象具有唯一的用户发件人 ID 作为键。EventHandler() 调用 MessageHandler() 来存储传入用户消息的当前时间,从而调用 ScriptHandler() 来执行您在项目文件夹的 default.src 中编写的机器人脚本。因此,用户接收到快速回复消息“欢迎来到菜单!”。

function EventHandler(context, event) {
    context.simpledb.roomleveldata = {};
    context.simpledb.doGet("Senders", function(c, e) {
        // console.log("e.dbval-----" + e.dbval);
        var senderData;
        senderData = e.dbval;
        // console.log("DATA-----" + JSON.stringify(senderData));
        senderData[event.sender] = {};
        context.simpledb.doPut("Senders", senderData, function(c_, e_) {
            MessageHandler(context, event);
        });
    });
};

开始聊天事件后。无论如何,用户向机器人发送消息 - MessageHandler() 将接收传入的用户有效负载,更新时间当前时间(这是从用户收到的最后一条消息的时间)并调用 ScriptHandler() 执行您的机器人脚本。

 function MessageHandler(context, event) {
 {
        var userInitialObject = {
            "userAttemptCounter": 0,
            "contextObj": event.contextobj,
            "lastActiveTime": new Date().getTime()
        };  
        context.simpledb.doGet("Senders", function(c, e) {
            console.log("e.dbval-----" + e.dbval);
            var data;
            data = e.dbval;
            console.log("DATA-----" + JSON.stringify(data));
            data[event.sender] = userInitialObject;
            context.simpledb.doPut("Senders", data, function(c_, e_) {
                ScriptHandler(context, event);
            });

        });
}

现在,使用EndpointHandler()在机器人中创建一个 Web 服务。此 Web 服务检索存储的发送者对象,并根据当前时间检查存储的上次用户活动时间的值。在下面的代码片段中,它是 45 秒的差异检查。如果为真,则使用 sendMessage API 向用户发送一条消息。您可以使用 访问此 Web 服务。为此 Web 服务设置一个 cron。https://www.gupshup.io/developer/bot/{Gupshup_bot_name}/public

var async = require("async");
function HttpEndpointHandler(context, event) {
    context.simpledb.doGet("Senders", function(c, e) {
        // console.log("e.dbval-----"+e.dbval);
        var users = e.dbval // get all Facebook user details basis message id which is contextObj, lastActiveTime and userAttemptCounter
        // console.log("DATA-----" + JSON.stringify(users));
        var userInitialObject;

        async.eachOf(users, function(user, key, callback) {

                // console.log(key + " = " + JSON.stringify(user));
                var userLastActiveTime = user["lastActiveTime"]; // extract last activity time from user JSON...
                var currentTime = new Date().getTime();
                var timeDifference = (currentTime - userLastActiveTime) / 1000;
                // console.log("timeDifference -- " + timeDifference);
                var userAttempts = parseInt(user["userAttemptCounter"]);
                // console.log("userAttempts -- " + userAttempts);
                context.simpledb.doGet("userAnswer", function(c_, e_) {
                    if (timeDifference > 45 && userAttempts == 0 ) // Check Survey Flag is not equal to 1(i.e survey completed) // check if 1 reminder is sent to user. *** visitEcom ** variable is true if user has clicked the 2cart link..
                    {
                        var contextObj = user["contextObj"];
                        // console.log(JSON.stringify(contextObj));
                        // var followUpMsg = "We were having so much fun. Let's keep the good times rolling!";
                        var followUpMsg = {
                            "type": "quick_reply",
                            "content": {
                                "type": "text",
                                "text": "Hello? We were having so much fun. Should we keep the good times rolling?"
                            },
                            "msgid": "reminderMessage",
                            "options": [{
                                "type": "text",
                                "title": "Yes",
                                "iconurl": "http://www.iconsplace.com/icons/preview/008342/record-256.png"
                            }, {
                                "type": "text",
                                "title": "No",
                                "iconurl": "http://www.iconsplace.com/icons/preview/red/record-256.png"
                            }]
                        };
                        context.simpledb.doGet("room:" + key, function(cs, evt) {
                            userAttempts = 1; //(userAttempts + 1); // 1
                            // --------------------------------------------------
                            // var roomLevelDataValues = evt.dbval;
                            // Logic to get fallback message + user last message....
                            // var lstmessage = roomLevelDataValues.userLastMessage;
                            // lstmessage.unshift(followUpMsg);
                            // Bot_Message(context, event.botname, JSON.stringify(contextObj), JSON.stringify(lstmessage));
                            // --------------------------------------------------
                            Bot_Message(context, event.botname, JSON.stringify(contextObj), JSON.stringify(followUpMsg));
                            // context.console.log("userAttemptsafterCondition" + userAttempts);
                            userInitialObject = {
                                "userAttemptCounter": userAttempts,
                                "contextObj": contextObj,
                                "lastActiveTime": new Date().getTime()
                            };
                            users[key] = userInitialObject;
                            // context.console.log("Wallah habibi for key " + key);
                            // context.console.log(JSON.stringify(users));
                            callback();
                        });
                    } //if 15 sec
                    else {
                        callback();
                    }
                }); //userAnswer doGet...
            },
            function(err) {

                // context.console.log(JSON.stringify(users));
                context.simpledb.doPut("Senders", users, function(c_, e_) {
                    context.sendResponse("Reminder message sent and userObject updated");
                });
            });
    });
});

SendMessage API 用于向用户发送消息。

function Bot_Message(context, botname, contextobj, message) {
    var done = "";
    var apikey = 'YOUR GUPSHUP ACCOUNT API KEY';
    botname = encodeURI(botname);
    contextobj = encodeURI(contextobj);
    apikey = encodeURI(apikey);
    message = encodeURIComponent(message);
    var url = "https://api.gupshup.io/sm/api/bot/" + botname + "/msg";
    var body = "botname=" + botname + "&context=" + contextobj + "&message=" + message;
    var headers = {
        "Accept": "application/json",
        "apikey": apikey,
        "Content-Type": "application/x-www-form-urlencoded"
    };
    context.simplehttp.makePost(url, body, headers, done);
}

参考文档以更好地理解示例代码:

于 2020-01-07T13:29:29.457 回答