2

I want to send a prompt (I am waiting) if I don't receive any message from the user after say 5 mins in directline webchat channel.

It is Demo bot, so I am using local memory storage.

Any help would be appreciated.

billoverton
  • 2,705
  • 2
  • 9
  • 32
nandini336
  • 45
  • 1
  • 8
  • Your title mentions resetting the conversation, but then your question just mentions sending a message. What exactly do you want to happen after 5 minutes? Just a message sent to the user? Also, how are you deploying the bot? There are different solutions if you are using directline botframework-webchat channel vs. an OOTB channel like OOTB WebChat or Teams. – billoverton Apr 20 '20 at 16:09
  • You can use the timer function in the onturn activity. I hope this logic works for you. – Sainath Reddy Apr 20 '20 at 10:07
  • I want to send simple message when user is idle after 5mins ,iam deploying as OOTB webchat @billoverton – nandini336 Apr 21 '20 at 04:43
  • I see directline mentioned in the comments, so let me ask another way. Are you using the embed URL provided in the channel blade? Or are you deploying as a separate HTML file with webchat.render or something similar called? – billoverton Apr 22 '20 at 18:06

1 Answers1

0

There are a couple of ways you can handle this. If you are calling this via script (using something like the botframework-webchat option using Directline channel), you can check out this answer on SO which shows you how to set it up in your HTML file.

If you want to implement this directly in your bot, you can use a time function like Sainath Reddy mentioned. However, I noticed that the context object becomes invalid, so you have to use Proactive Messaging instead. I'm not sure if this is the most efficient method, but here is how I was able to accomplish this.

First, you must import TurnContext and BotFrameworkAdapter from botbuilder

const { TurnContext, BotFrameworkAdapter } = require('botbuilder');

Then, add the following code in the onMessage function (or onTurn if you are using an earlier setup). setTimeout will run only once. You can instead use setInterval if you want it to repeat.

            // Save the conversationReference
            var conversationReference = TurnContext.getConversationReference(context.activity);

            // Reset the inactivity timer
            clearTimeout(this.inactivityTimer);
            this.inactivityTimer = setTimeout(async function(conversationReference) {
                console.log('User is inactive');
                try {
                    const adapter = new BotFrameworkAdapter({
                        appId: process.env.microsoftAppID,
                        appPassword: process.env.microsoftAppPassword
                    });
                    await adapter.continueConversation(conversationReference, async turnContext => {
                        await turnContext.sendActivity('Are you still there?');
                    });
                } catch (error) {
                    console.log(error);
                }
            }, 300000, conversationReference);

I prefer this method because it also works if you are using botframework-webchat or similar. In fact, it will work in every channel. If there are certain channels where you don't want this to happen, you'd have to add some additional logic to the function. Or, if you want it only in something like botframework-webchat, you can just use the first method I linked.

billoverton
  • 2,705
  • 2
  • 9
  • 32
  • Did this work for you? If so please accept this answer, or otherwise let me know if you are still having difficulty! – billoverton Apr 28 '20 at 14:58