Botframework-sdk: [Question] Set Session timeout

Created on 5 Jun 2017  路  9Comments  路  Source: microsoft/botframework-sdk

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?

customer-replied-to

Most helpful comment

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);
}

All 9 comments

@Dmdl are you using .NET or Node SDK?

@nwhitmont, I am using the Node SDK.

@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

@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.

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

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);
}

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

@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, 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 _timerDictionary = new Dictionary(); // 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);
}
}
}
}`

Was this page helpful?
0 / 5 - 0 ratings