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

[Question] Set Session timeout

Open Dmdl opened this issue 9 years ago • 11 comments

Is there a way to end user session after a predefined time? For example that a user has left the chat in middle of a dialog and when that user start a chat again after a day or two, start the conversation from a fresh state instead of continuing previous one?

Dmdl avatar Jun 05 '17 11:06 Dmdl

@Dmdl are you using .NET or Node SDK?

nwhitmont avatar Jun 05 '17 15:06 nwhitmont

@nwhitmont, I am using the Node SDK.

Dmdl avatar Jun 05 '17 16:06 Dmdl

@Dmdl Not sure if it's the best way, but you can have a middleware with a timer for shorter terms, or a scheduled job for longer terms that will call session.endConversation once the gap between the last conversation and now > day/two

amitbend avatar Jun 05 '17 17:06 amitbend

@Dmdl I know that many people store conversation history in a DB to display in future conversations. You could use that method then if the new message is >1-2 days purge that data, or not if its less than that.

JasonSowers avatar Jun 05 '17 17:06 JasonSowers

@Dmdl The current Bot Framework does not have this feature yet. Contributions welcome.

nwhitmont avatar Jun 05 '17 22:06 nwhitmont

Here is a snippet for Prompt and Timeout. Basically you need to get in the middle between the dialog and the user input- use async time out func.

var n=0;
var current=0;
// Trigger secondary dialogs when 'settings' or 'support' is called
bot.use({
    botbuilder: function (session, next) {
        n++;
        current++;
        console.log(n);
        prompt(session, n);
        timeout(session, n);
        
        // continue normal flow
        next();
    }
});


function timeout(session, n)
{
    setTimeout(function()
    {
        if(n===current)
        {
            // session.send(n);
            session.endConversation('Ending conversation since you\'ve been inactive too long. Hope to see you soon.');
        }

    }, 300000);
    
}

function prompt(session, n)
{
    setTimeout(function()
    {
        if(n===current)
        {
            session.send('Are you there?');
        }
        
    }, 120000);
}

SerendioSoftwares avatar Sep 18 '17 17:09 SerendioSoftwares

any way to do the same in C# ?? Couldn't find session management in C# code

uditx avatar Dec 12 '17 11:12 uditx

@uditx @nwhitmont I am building bot using microsoft bot framework. anyway to identify that the user have not responded for about x min(say 5 min) in C#

athiappang avatar Nov 13 '18 14:11 athiappang

@athiappang, I also needed to identify that the user has not responded in a set period of time. I followed the approach at https://github.com/microsoft/botframework-sdk/issues/2496, which involves using a dictionary with conversation ID as key and timer as value, with a few changes (Timer is in System.Threading, not System.Threading.Tasks, and I stopped the timer with timer.Dispose(), since that is what is specified in the Microsoft documentation (https://docs.microsoft.com/en-us/dotnet/api/system.threading.timer?view=netframework-4.8)).

This is the code for my timer helper class: `using Microsoft.Bot.Builder.Dialogs; using System.Collections.Generic; using System.Threading;

namespace HelpDeskBot.Helpers { public static class TimerHelper { private static Dictionary<string, Timer> _timerDictionary = new Dictionary<string, Timer>(); // Keep track of timers (key is conversation ID) public static void StartTimer(TimerCallback callbackMethod, int timeOut, DialogContext dialogContext) { var timer = new Timer(callbackMethod, dialogContext, timeOut, 0); _timerDictionary.Add(dialogContext.Context.Activity.Conversation.Id, timer); } public static void StopTimer(string conversationId) { if (_timerDictionary.TryGetValue(conversationId, out Timer timer)) { timer.Dispose(); _timerDictionary.Remove(conversationId); } } } }`

kds-snyder avatar Jul 25 '19 22:07 kds-snyder

@kds-snyder I am working on the same requirements for my bot and i am stuck with the timer implementation. Do you have the timer code in a Github Repository which i can refer to. Thanks.

MukulSingh10 avatar Aug 10 '21 06:08 MukulSingh10

I also have similar requirements. Any updates on this feature?

newbazz avatar May 10 '22 06:05 newbazz