botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Question: sending proactive message without breaking the dialog stack

Open bobzsj87 opened this issue 10 years ago • 10 comments

Hi. I read the document and already know that saving address and use with universalBot.send can send any message, or beginDialog to clean up existing stack and start a new one. However, I wonder if there is a way to "hack" into session and send proactive message without breaking the current dialog stack?

Thanks! I am using the Nodejs SDK.

bobzsj87 avatar Sep 30 '16 07:09 bobzsj87

what do you mean by that? Explain in details..

rahul251993 avatar Sep 30 '16 10:09 rahul251993

Hi. The use case is like this: my user is in the middle of a dialog stack doing something, and I want to send a "broadcast" quiz (a choice prompt) and get the result from the user input. After that, the user can resume the previous dialog. The problem now is that by "beginDialog" from a UniversalBot, it clears up all existing dialog stack and restart a new one, as compared to a dialog started by a session. So how to send a proactive message using a stored Address and push it to the existing dialog stack?

bobzsj87 avatar Oct 01 '16 06:10 bobzsj87

That's an interesting problem... I had to think about that a bit but it is doable. You need to manually trigger an global beginDialogAction(). Ironically enough you'd do this in a much similar way to sending a proactive message using bot.send() but you'll want to call bot.receive() with your proactive message. Define your action as something like this:

bot.beginDialogAction('sendQuiz', '/sendQuiz');

bot.dialog('sendQuiz', [
     function (session, args) {
          var question = args.data;

Then to trigger it do something like this:

var msg = new builder.Message().address(savedAddress).text('action?sendQuiz=' + question);
bot.receive(msg);

Conceptually you can think of this as the user pressing a button called "sendQuiz" and you've bound that button to your beginDialogAction(). Since you never send them a card with a button for them to press you're just proactively pressing it for them.

Stevenic avatar Nov 14 '16 00:11 Stevenic

I forgot to add that the '/sendQuiz' dialog will get pushed onto the stack and after they answer the question they'll automatically be returned to where they were which sounds like your desired behavior.

Stevenic avatar Nov 14 '16 00:11 Stevenic

Did @Stevenic solve your problem?

chrimc62 avatar Nov 26 '16 23:11 chrimc62

I'm closing this for now. I'm actually thinking of a simpler way to support this scenario in 3.6. But the above should work.

Stevenic avatar Dec 12 '16 08:12 Stevenic

so to summarize this, you would have the bot.beginDialogAction inside a setInterval?

setInterval(function () {
    var newAddresses = addresses.splice(0);
    newAddresses.forEach(function (address) {

        console.log('Starting survey for address:', address);

        // new conversation address, copy without conversationId
        var newConversationAddress = Object.assign({}, address);
        delete newConversationAddress.conversation;

        // start survey dialog
        bot.beginDialogAction('sendQuiz', '/sendQuiz')

    });
}, 20000);

bot.dialog('sendQuiz', [
     function (session, args) {
          var question = args.data;
          ..
)

can you please clarify where var msg = new builder.Message().address(savedAddress).text('action?sendQuiz=' + question); bot.receive(msg); comes into the picture?

slidenerd avatar Dec 23 '16 13:12 slidenerd

How is this done from a lib.dialog library ?

I get lib.receive(msg) ^

TypeError: lib.receive is not a function

IanLawrence avatar Aug 16 '17 19:08 IanLawrence

workaround doesn't work in 3.13, getting type error id of undefined. any updates @Stevenic @chrimc62 ?

nebgor avatar Feb 15 '18 10:02 nebgor

best link to follow : https://github.com/Microsoft/BotBuilder/issues/1930

nebgor avatar Feb 16 '18 03:02 nebgor