Azure Bot Framework| If there is no response from the user for some time, then how to send reminder to the users?

Viewed 353

By above question, I mean this-

If there is no activity on the bot for 10 seconds

Bot sends the message>> Looks like you aren't there for now.

Bot>> Ping me again once you are back. Bye for now.

1 Answers

In nodejs you can do this by setting a timeout in your turn handler (onTurn or onMessage). You need to clear the timeout and reset it on every turn if you want the message to be X time after the user's last message. A timeout will send the message once. If you want it to repeat, e.g. every X time after the user's last message, you can use an interval instead of a timeout. I found the easiest way to send the message was as a proactive message, so you do need to include TurnContext and BotFrameworkAdapter from the botbuilder library with this method. The syntax is likely different for C#, but this should point you in the right direction. Here is the function I use:

    async onTurn(context) {

        if (context.activity.type === ActivityTypes.Message) {

            // Save the conversationReference
            const conversationData = await this.dialogState.get(context, {});
            conversationData.conversationReference = TurnContext.getConversationReference(context.activity);
            await this.conversationState.saveChanges(context);
            console.log(conversationData.conversationReference);

            // 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('Bad Request. Please ensure your message contains the conversation reference and message text.');
                    console.log(error);
                }
            }, 300000, conversationData.conversationReference);

            //<<THE REST OF YOUR TURN HANDLER>>
        }
    }
Related