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 OR end the conversation in MSTeams C# #5039

Closed xiaolangxyz closed 6 years ago

xiaolangxyz commented 6 years ago

Bot Info

Issue Description

According this, it seems context.EndConversation was not working in MS Teams. Of course I have tried again. It seems still not working.

By the way my state is saving in CosmosDB. And I want to make a timeout conversation.

I want to find some ways like EndConversation or Reset the conversation state. So I tried the way below, but it seems not working.

Code Example

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<bool> result)
        {
            var confirm = await result;
            if (confirm)
            {
                context.Done(true);
            }
            else
            {
                await context.PostAsync("Because of longtime no replying, the conversation would be close");                
                using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, context.Activity.AsMessageActivity()))
                {
                    var token = new CancellationToken();
                    var botData = scope.Resolve<IBotData>();
                    await botData.LoadAsync(token);

                    var stack = scope.Resolve<IDialogStack>();
                    stack.Reset();

                    // botData.UserData.Clear(); //<-- could clear userdata as well
                    botData.ConversationData.Clear();
                    botData.PrivateConversationData.Clear();
                    await botData.FlushAsync(token);

                    var botToUser = scope.Resolve<IBotToUser>();
                    //await botToUser.PostAsync(context.Activity.CreateOAuthReplyAsync.CreateReply("Conversation aborted."));
                }     
                //}
            }
        }

Actual Results

DocDBTrace Information: 0 : 2018-09-06T10:36:57.6711560Z, CorrelatedActivityId: e65a5302-aa5f-485e-837d-4e3d3c37759a | Last ExecuteNextAsync with ExecuteNextAsyncMetrics: [Turnaround Time: 47.2036ms, Response Time: 20.4274ms, Run Time: 0.1944ms, Wait Time: 47.0092ms, Number Of Preemptions: 1] DocDBTrace Error: 0 : DocumentClientException with status code: PreconditionFailed, message: Message: {"Errors":["One of the specified pre-condition is not met"]} ActivityId: 34d6224a-99a2-4318-8145-853c97c38267, Request URI: /apps/be83c8b6-69d9-4737-b7d1-e6cd7df307ca/services/6118cd8a-029b-40ba-b8c2-4ece54d4bc7e/partitions/bd6d451c-c279-4455-9c71-3f93c15ebba0/replicas/131796446616199021p, RequestStats: RequestStartTime: 2018-09-06T10:36:57.7245027Z, Number of regions attempted: 1 , SDK: Microsoft.Azure.Documents.Common/2.0.0.0, and response headers: { "Transfer-Encoding": "chunked", "x-ms-last-state-change-utc": "Mon, 03 Sep 2018 18:27:10.840 GMT", "lsn": "8818", "x-ms-schemaversion": "1.6", "x-ms-quorum-acked-lsn": "8818", "x-ms-current-write-quorum": "3", "x-ms-current-replica-set-size": "4", "x-ms-xp-role": "1", "x-ms-global-Committed-lsn": "8818", "x-ms-number-of-read-regions": "0", "x-ms-transport-request-id": "35096", "x-ms-cosmos-llsn": "8818", "x-ms-cosmos-quorum-acked-llsn": "8818", "x-ms-session-token": "0:8818", "x-ms-request-charge": "1.24", "x-ms-serviceversion": "version=2.0.0.0", "x-ms-activity-id": "34d6224a-99a2-4318-8145-853c97c38267", "Strict-Transport-Security": "max-age=31536000", "x-ms-gatewayversion": "version=2.0.0.0", "Date": "Thu, 06 Sep 2018 10:36:56 GMT", "Server": "Microsoft-HTTPAPI/2.0", } DocDBTrace Error: 0 : DocumentClientException with status code: PreconditionFailed, message: Message: {"Errors":["One of the specified pre-condition is not met"]} ActivityId: 9450fd52-80b4-4312-9107-c956952175ea, Request URI: /apps/be83c8b6-69d9-4737-b7d1-e6cd7df307ca/services/6118cd8a-029b-40ba-b8c2-4ece54d4bc7e/partitions/bd6d451c-c279-4455-9c71-3f93c15ebba0/replicas/131796446616199021p, RequestStats: RequestStartTime: 2018-09-06T10:36:57.7162385Z, Number of regions attempted: 1 , SDK: Microsoft.Azure.Documents.Common/2.0.0.0, and response headers: { "Transfer-Encoding": "chunked", "x-ms-last-state-change-utc": "Mon, 03 Sep 2018 18:27:10.840 GMT", "lsn": "8818", "x-ms-schemaversion": "1.6", "x-ms-quorum-acked-lsn": "8818", "x-ms-current-write-quorum": "3", "x-ms-current-replica-set-size": "4", "x-ms-xp-role": "1", "x-ms-global-Committed-lsn": "8818", "x-ms-number-of-read-regions": "0", "x-ms-transport-request-id": "26533", "x-ms-cosmos-llsn": "8818", "x-ms-cosmos-quorum-acked-llsn": "8818", "x-ms-session-token": "0:8818", "x-ms-request-charge": "1.24", "x-ms-serviceversion": "version=2.0.0.0", "x-ms-activity-id": "9450fd52-80b4-4312-9107-c956952175ea", "Strict-Transport-Security": "max-age=31536000", "x-ms-gatewayversion": "version=2.0.0.0", "Date": "Thu, 06 Sep 2018 10:36:57 GMT", "Server": "Microsoft-HTTPAPI/2.0", } DocDBTrace Error: 0 : DocumentClientException with status code: PreconditionFailed, message: Message: {"Errors":["One of the specified pre-condition is not met"]} ActivityId: 4f1073a7-4b88-44d9-b608-9d0952d4427b, Request URI: /apps/be83c8b6-69d9-4737-b7d1-e6cd7df307ca/services/6118cd8a-029b-40ba-b8c2-4ece54d4bc7e/partitions/bd6d451c-c279-4455-9c71-3f93c15ebba0/replicas/131796446616199021p, RequestStats: RequestStartTime: 2018-09-06T10:36:57.7264158Z, Number of regions attempted: 1 , SDK: Microsoft.Azure.Documents.Common/2.0.0.0, and response headers: { "Transfer-Encoding": "chunked", "x-ms-last-state-change-utc": "Mon, 03 Sep 2018 18:27:10.840 GMT", "lsn": "8818", "x-ms-schemaversion": "1.6", "x-ms-quorum-acked-lsn": "8818", "x-ms-current-write-quorum": "3", "x-ms-current-replica-set-size": "4", "x-ms-xp-role": "1", "x-ms-global-Committed-lsn": "8818", "x-ms-number-of-read-regions": "0", "x-ms-transport-request-id": "19011", "x-ms-cosmos-llsn": "8818", "x-ms-cosmos-quorum-acked-llsn": "8818", "x-ms-session-token": "0:8818", "x-ms-request-charge": "1.24", "x-ms-serviceversion": "version=2.0.0.0", "x-ms-activity-id": "4f1073a7-4b88-44d9-b608-9d0952d4427b", "Strict-Transport-Security": "max-age=31536000", "x-ms-gatewayversion": "version=2.0.0.0", "Date": "Thu, 06 Sep 2018 10:36:56 GMT", "Server": "Microsoft-HTTPAPI/2.0", } DocDBTrace Error: 0 : Operation will NOT be retried. Current attempt 0, Exception: Microsoft.Azure.Documents.DocumentClientException: Message: {"Errors":["One of the specified pre-condition is not met"]} ActivityId: 34d6224a-99a2-4318-8145-853c97c38267, Request URI: /apps/be83c8b6-69d9-4737-b7d1-e6cd7df307ca/services/6118cd8a-029b-40ba-b8c2-4ece54d4bc7e/partitions/bd6d451c-c279-4455-9c71-3f93c15ebba0/replicas/131796446616199021p, RequestStats: RequestStartTime: 2018-09-06T10:36:57.7245027Z, Number of regions attempted: 1 , SDK: Microsoft.Azure.Documents.Common/2.0.0.0, documentdb-dotnet-sdk/1.22.0 Host/32-bit MicrosoftWindowsNT/10.0.15063.0 場所 Microsoft.Azure.Documents.Client.ClientExtensions.d1.MoveNext() --- 直前に例外がスローされた場所からのスタック トレースの終わり --- 場所 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 場所 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 場所 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) 場所 Microsoft.Azure.Documents.GatewayStoreModel.<>cDisplayClass20_0.<b__0>d.MoveNext() --- 直前に例外がスローされた場所からのスタック トレースの終わり ---

sbuffkin commented 6 years ago

I see that this question has also been posted on stackoverflow over here. As this is better suited for being answered there I'm closing this one and I will follow up with you there soon.

That said, .endConversation() will probably not be enabled within MS teams in the future due to compliance reasons (this is why it throws an error when you try to use it. However, I don't think what you are trying to do is actually what .endConversation() is meant to do either, as it looks like you're actually talking about dialogs rather than conversations.

jessedemobot commented 6 years ago

Hi @xiaolangxyz
Try to do the stack.Reset(); in Post method in the MessagesController. Do not do any Resset in the MessageReceivedAsync. I guess some code logic ran after MessageReceivedAsync will do something with stack, so if we reset stack before that, some exceptions will occur.