The use-case you mention cannot be achieved using a label handler in the bot-scripting tool.
One possible solution to your issue can be a cron service.
Assuming you have a bot script like below:
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
A user initiates a conversation with your bot on Facebook messenger, startchatting event is received in the EventHandler function. Using advance data persistence creates a "Senders" Object which has a unique user sender ID as the key. EventHandler() calls MessageHandler() which stores the current time for incoming user message and thereby a call is made to ScriptHandler() which executes your bot script written in the default.src of the project folder. Hence, the user receives a quick reply message "Welcome to the menu!".
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);
});
});
};
After startchatting event. Whatever, user sends a message to the bot - MessageHandler() will receive the incoming user payload, updates the time current time(this is the time of last message received from the user) and a call is made to ScriptHandler() which execute your bot script.
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);
});
});
}
Now, Create a web service in the bot using EndpointHandler(). This Web service retrieves the Stored sender object and check the value of stored last user activity time against the current time. Here in the below code snippet, it is 45-sec difference check. If it is true, a message is sent to the user using sendMessage API. You can access this web service using https://www.gupshup.io/developer/bot/{Gupshup_bot_name}/public
. Set a cron for this web service.
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 used to send message to the user.
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);
}
Reference docs to understand the sample code better: