My bot is running fine when the user starts the conversation. But what I want to know is if I can start the conversation with the user instead user with it.
Based on the Facebook docs, I think I can do this with FB directly once I got the page scoped usedId -- e.g. through using the 'message us' plugin on our page
Now though - I use the MS botframework and wonder how to do it using that framework or if I just have to 'circumvent' the framework and do a manual POST for the initial message
cross-opted to SO: http://stackoverflow.com/questions/39524913/can-i-start-the-conversation-with-messenger-bot-fb-botframework
https://github.com/Microsoft/BotBuilder/issues/1239 looks promising but says it doesn't really work yet
On Facebook messenger I resulted in using the 'Get Started Button' which you can then use to start a dialog using the postback. I also use this to gather the users information before starting a conversation with them ;).
I might be too dumb to get it but.. you aren't writing proactively to the user though
for fb, I can use the fb send API with the page-scoped userID, but how to do it with ms bf – Daij-Djan 37
Well you use the postback from the Get Started Button to then start a dialog. See issue: #1238, there I explain how to setup various FB messenger settings. From there you can initiate the conversation in said prompt. For example I get the users information using the page scoped id:
// Get users profile
bot.dialog('/getprofile', [
function (session) {
console.log("=== DIALOG: GETPROFILE | STEP: 1/1 ====");
// Store the returned user page-scoped id (USER_ID) and page id
session.userData.userid = session.message.sourceEvent.sender.id;
session.userData.pageid = session.message.sourceEvent.recipient.id;
// Let the user know we are 'working'
session.sendTyping();
// Get the users profile information from FB
request({
url: 'https://graph.facebook.com/v2.6/'+ session.userData.userid +'?fields=first_name,last_name,profile_pic,locale,timezone,gender',
qs: { access_token: process.env.FB_PAGE_ACCESS_TOKEN },
method: 'GET'
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
// Parse the JSON returned from FB
body = JSON.parse(body);
// Save profile to userData
session.dialogData.firstname = body.first_name;
session.dialogData.lastname = body.last_name;
session.dialogData.profilepic = body.profile_pic;
session.dialogData.locale = body.locale;
session.dialogData.timezone = body.timezone;
session.dialogData.gender = body.gender;
// Return to /getstarted
session.endDialogWithResult({ response: session.dialogData });
} else {
// TODO: Handle errors
console.log(error);
console.log("Get user profile failed");
}
});
}
]);
Hope this helps :).
@SamuelTassell how are you invoking the /getprofile dialog
is your condition inside the / dialog similar to this
if(!session.userData.user){
session.beginDialog('/getProfile')
}
Hi @slidenerd. I'm simply using the postback from the getstarted button as I referenced on #1238.
In my case:
{
"setting_type":"call_to_actions",
"thread_state":"new_thread",
"call_to_actions":[
{
"payload":"action?getstarted"
}
]
}
Then create a dialog action to handle the postback:
bot.beginDialogAction('getstarted', '/getstarted');
bot.dialog('/getstarted', [
function (session) {
console.log("=== DIALOG: GETSTARTED | STEP: 1/4 ====");
console.log(session.userData);
// Let the user know we are 'working'
session.sendTyping();
if( !session.userData.firstRun ) {
// Store the returned user page-scoped id (USER_ID) and page id
session.userData.userid = session.message.sourceEvent.sender.id;
session.userData.pageid = session.message.sourceEvent.recipient.id;
// DELAY ISN'T THE REQUEST - I THINK IT'S THE INITIAL REQUEST TO BOTFRAMEWORK
// Move to the /getprofile dialog
session.beginDialog('/getprofile');
} else {
// The firstname has been stored so the user has completed the /getstarted dialog
// Stop this dialog and Welcome them back
session.replaceDialog('/welcomeback');
}
}.....
A bit late, but I hope it helps.
@SamuelTassell Thanks for the example code. I am trying to implement the Facebook persistent menu inside the get started dialog. The thing is how from the "get started" dialog we can invoke the waterfall steps? I mean after I have pressed the get started button in messenger it populates the menu but then bot does not move forward with the steps specified in waterfall function list.
Most helpful comment
Hi @slidenerd. I'm simply using the postback from the getstarted button as I referenced on #1238.
In my case:
Then create a dialog action to handle the postback:
A bit late, but I hope it helps.