Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Bot Framework - Clear Conversation State

I am making a bot using Microsoft's Bot Framework, and I've noticed that when I make changes and deploy to Microsoft Teams, it uses the same conversation state and I have to write "/deleteprofile" to clear the state.

I want to clear the state within my code, but don't know a good way to do this. I am not sure which file and what code to use to clear the conversation state.

For reference, I am currently using C#.

like image 559
Sharon Fang Avatar asked Oct 27 '17 21:10

Sharon Fang


People also ask

How do you end a conversation in bot framework?

endDialogAsync() ends the current dialog on the stack returning control to the parent dialog, if present, or to the turn handler. Additionally, it can be called from anywhere the dialog context is accessible. Best practice is to call it at the end of every dialog.

How do you delete flow bot messages in teams?

In the Bot Framework, every message has its unique activity identifier. Messages can be deleted using the Bot Framework's DeleteActivity method. To delete a message, pass that activity's ID to the DeleteActivityAsync method of the TurnContext class.

What is conversation state?

STATE is an acronym and stands for:S – Share your facts. T – Tell your story. A – Ask for the other's paths. T – Talk Tentatively. E – Encourage Testing.

How do you save chatbot conversations?

Chatbot conversations can be stored in a SQL database that is hosted on a cloud platform. For example, if you were planning on creating a chatbot within the Microsoft Teams platform, you could use CosmosDB, a noSQL database with open APIs, to store your conversations and use PowerBI to visualize the reports.


2 Answers

there are different ways to accomplish this depending on where you would like to do this from.

one way would be to simply call context.EndConversation("Conversation Ended"); from a dialog.

The other is a bit more complicated but it will accomplish the same thing here is an implementation you can tweak to suit your needs:

public static async Task AbortConversation(Activity message)
{
    using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
    {
        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(message.CreateReply("Conversation aborted."));
    }
}
like image 157
D4RKCIDE Avatar answered Sep 19 '22 00:09

D4RKCIDE


I resolved this by overriding the DefaultWaitNextMessageAsync method and ending the conversation.

like image 45
Sharon Fang Avatar answered Sep 21 '22 00:09

Sharon Fang