microsoft / botframework-sdk

Bot Framework provides the most comprehensive experience for building conversation applications.
MIT License
7.5k stars 2.44k forks source link

How to reset the conversation if the bot is idle for 5 mins in C# #2385

Closed nandananr closed 7 years ago

nandananr commented 7 years ago

Hi,

How to reset the conversation if the bot is idle for 5 mins. Bot asks for a question (for example? what is your Name?), and user gives response after 5 mins. Bot should say that session is ended and it has to start the conversation from the beginning. Is this possible to do? I am using Bot builder SDK for C#. I have tried below code

` if (activity.Type == ActivityTypes.Message) {

            await Task.Delay(5000).ContinueWith(async (t) =>
            {
                ConnectorClient client = new ConnectorClient(new Uri(activity.ServiceUrl));
                StateClient stateClient = activity.GetStateClient();
                await stateClient.BotState.DeleteStateForUserAsync(activity.ChannelId, activity.From.Id, CancellationToken.None);
                var clearMsg = activity.CreateReply();
                clearMsg.Text = $"Reseting everything for conversation: {activity.From.Id}";

                await client.Conversations.SendToConversationAsync(clearMsg);

            });
            await Conversation.SendAsync(activity, () => new RootDialog());                               
        }`

This is not working. Any ideas?

willportnoy commented 7 years ago

If you're running in ASP.NET and IIS, there is not guarantee that a method after a Task.Delay (which means that it is detached from an http request) will even execute - any web server can tear down the server once http requests are completed. Generally, you should use a worker and a queue, but you can try to make it work with something like https://msdn.microsoft.com/en-us/library/dn636893(v=vs.110).aspx.

nandananr commented 7 years ago

Hi, Do you have any specific example. I have integrated the bot with Twilio. If I stop the conversation in the middle, the session keeps open forever. For example, my bot collects user details(5 steps). If I do not reply back to bot at 3rd step, it is expecting me to continue form 3rd step. However I need the session to expire after 20 mins. If user does not reply in 20 mins, the flow should start from the beginning.

arthurtina724 commented 7 years ago

@nandananr I'm building a bot utilizing the SMS channel as well. Have you ever tried creating a state client via service url and app creds? I'm having some issues with the state client when trying to proactively create a conversation and set its data. The state client returns back an exception stating it cannot deserialize the response. Also did you ever figure out the your 20 min timeout requirement? I've stored my own conversation state in my own repo and will manage if my "session" is expired and start at my root dialog. The problem is that it is a polling windows service to see if my conversations have gone over a configured duration.

willportnoy commented 7 years ago

@nandananr it's easier to check for expiration when you receive each new message and clear out the conversation state if it's been "too long".

PeterWone commented 5 years ago

@willportnoy

it's easier to check for expiration when you receive each new message and clear out the conversation state if it's been "too long".

And if the user closed the browser and walked away? Your "strategy" will leak sessions.

Services like Twilio (already mentioned) and Live Chat integration with Dynamics 365 cost money for each dead session that your approach produces.

It's a non-answer and the issue should not have been closed. If you can't resolve this problem then bot framework isn't ready for prime-time.

EricDahlvang commented 5 years ago

Hi @PeterWone

This issue is a couple years old, and this problem has been solved many times. The best two options are as Will initially suggested:

Generally, you should use a worker and a queue, but you can try to make it work with something like https://msdn.microsoft.com/en-us/library/dn636893(v=vs.110).aspx

Nonetheless, there is some discussion of adding internal sdk support for timing out a conversation on this open issue: https://github.com/microsoft/botframework-sdk/issues/5237 Please add your feedback and interest in this feature there.

sw-ms-ankitsangani commented 5 years ago

Hello all,

I have same issues. How can i handle this. I used direct line service with waterfall method. Please help. Thanks.