Detect when conversation end so I can delete stored conversation state data

Viewed 118

I setup a bot that have a normal stack of dialog with each dialog storing some state data in Cosmos DB in Azure. When a dialog end, I use the OnEndDialogAsync to delete the data specific to that dialog.

My question is, how do I detect when the entire conversation end so that I can delete the whole thing? Or does conversation never end?

My current code that delete each dialog data on end:

    protected override async Task OnEndDialogAsync(ITurnContext context, DialogInstance instance, DialogReason reason, CancellationToken cancellationToken)
    {
        DialogStateDictionary dictionary = await Dependencies.StateAccessor.GetAsync(context, () => null);
        if (dictionary != null && dictionary.ContainsKey(DialogID) == true)
        {
            dictionary[DialogID] = null;
        }
        await Dependencies.StateAccessor.SetAsync(context, dictionary);
    }

The code to delete the entire thing would be:

await Dependencies.StateAccessor.SetAsync(context, null);
2 Answers

The concept of a conversation "ending" will be channel-specific. In Web Chat you can have your client respond to the browser leaving the page by letting your bot know. In channels like Teams the conversation is effectively permanent, but you can always arbitrarily define any point in the conversation as the "end" by having your bot reset its state like you're doing. Perhaps you could have a confirm prompt that asks the user "Will that be all?" and if the user says "yes" then the bot could say "Goodbye" or something.

Not sure which channel you are using, recommend to look at endofconversation activity type.

Related