Yoctol / bottender

⚡️ A framework for building conversational user interfaces.
https://bottender.js.org
MIT License
4.22k stars 334 forks source link

[Question] - Handover control to User and show a message #356

Closed pavei closed 5 years ago

pavei commented 6 years ago

Hi,

In facebook messenger i give the power of "stoping" the bot to the Inbox. So, to do this i treat all echo messages to find this comands like "stop", "continue". It was working, on old version of bottender. But now on new version there is a code block that not let me advise the user that the bot was stopped or continued.

So, is there a way to send a message to user, that chatbot is stopped when Inbox send a command?

Code block on https://github.com/Yoctol/bottender/blob/36981aa7b4fb2690f136b5dbaa50c3cab0ff5c03/src/context/MessengerContext.js#L98

 if (this._event.isEcho || this._event.isDelivery || this._event.isRead) {
      warning(
        false,
        'sendText: calling Send APIs in `message_reads`(event.isRead), `message_deliveries`(event.isDelivery) or `message_echoes`(event.isEcho) events may cause endless self-responding, so they are ignored by default.\nYou may like to turn off subscription of those events or handle them without Send APIs.'
      );
      return;

My Code

  if (context.event.isEcho && context.event.isText) {
            console.log("echo", context.event.text);

            let instructions = ["stop"]
            let instructionsSuccess = ["continue"];

            let text = context.event.text.toLowerCase().trim();

            if (instructions.indexOf(text) >= 0) {

                console.log("stop");
                await context.passThreadControlToPageInbox();
                await context.sendText("The chatbot has been stopped")

            } else if (instructionsSuccess.indexOf(text) >= 0) {

                 console.log("back")
                await context.takeThreadControl();
                await context.sendText("🤖 Chatbot is ready!")

            }
Calvin-Lin commented 5 years ago

You can use the Move to Main and Mark as done buttons of Messenger platform to control your bot manually, instead of listening to echo event.

2018-11-06 4 29 49 2018-11-06 4 30 02

To do this, you need to assign app roles first: https://developers.facebook.com/docs/messenger-platform/handover-protocol/assign-app-roles

Let your bot app be Primary Receiver, and let the inbox be Secondary Receiver.

After the roles be assigned, your users will be in Done when they interact with your bot.

When you press the Move to Main button, the inbox will send a request_thread_control event to your bot, and move user from Done to Main, then you can do

if (context.event.isRequestThreadControlFromPageInbox) {
  await context.passThreadControlToPageInbox();
  await context.sendText("The chatbot has been stopped")
}

When you press the Mark as done button, the inbox will send a pass_thread_control event to your bot, and move user from Main to Done, then you can do

if (context.event.isPassThreadControl) {
  await context.sendText("🤖 Chatbot is ready!")
}

You don't need to call context.takeThreadControl() in this scenario, because the inbox has already passed thread control back to your bot.

chentsulin commented 5 years ago

Hi @pavei, It seems that @Calvin-Lin has already provided a great answer, so I'm going to close this.