简体   繁体   中英

Using a trigger word to store conversation data permanently in a Azure chat bot

I have being doing some development into azure chat bots, specifically a QnA bot in C#, and am now looking at storing the conversation history into either table or database storage.

But unlike most tutorials and documentation on the web, I don't want to store the whole conversation from start to end, I only want to store the first message the user sends to the bot. I want this message to be stored temporally until the user types "no". When the user types "no" I want what is held in the temporary storage to be stored permanently.

Is this possible in a chat bot?

Any help or insight here would be much appreciated!

This would be fairly easy to accomplish with temporary storage Like a dictionary. There would be a number of ways to accomplish this. One thing I would look into is scorables for catching the "No" text. In this example I did not use scorables But it does the basic functionality you are looking for. The idea is that when a message comes in, you check to see if you have already saved a message from that userId and saved it, if not save it. If the user sends the text "No" save the text to permanent storage and remove the entry from the dictionary. I am just doing this in a basic RootDialog.cs:

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;

        var userId = activity.From.Id;
        var message = activity.Text;
        if (!Utils.FirstMessageDictionary.ContainsKey(userId))
        {

            Utils.FirstMessageDictionary.Add(userId, message);
            await context.PostAsync($"Message saved {userId} - {Utils.FirstMessageDictionary[userId]}");
        }

        if (message.ToLower() == "no")
        {

            //save to permanent storage here 

            Utils.FirstMessageDictionary.Remove(userId);
            await context.PostAsync($"Entry Removed for {userId}");

            try
            {
                await context.PostAsync($"{userId} - {Utils.FirstMessageDictionary[userId]}");
            }
            catch (Exception e)
            {
                await context.PostAsync($"No entry found for {userId}");
            }
        }
        context.Wait(MessageReceivedAsync);
    }

I also made this simple class for the dictionary:

public static class Utils
{
    public static Dictionary<string, string> FirstMessageDictionary = new Dictionary<string, string>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM