简体   繁体   中英

How to store only the users response when using the Azure chat bot

I am trying to store the users response in a table storage. I only want to store the users data they enter, and am not interested in the bots response. How is this possible and additionally would this be possible on a trigger word, for example when the user says "no" it logs there first interaction with the bot for example "Hello".

I have done lots of research on this subject but storing only the users input seems to be less documented.

Any help with this would be much appreciated!

I am trying to store the users response in a table storage. I only want to store the users data they enter, and am not interested in the bots response. How is this possible and additionally would this be possible on a trigger word, for example when the user says "no" it logs there first interaction with the bot for example "Hello".

It seems that you just want to store user inputs in table storage, not store data of bot's response. To achieve the requirement, you can intercept message that user send in MessagesController (or in dialog MessageReceivedAsync method), and then extract properties values that you want from activity and store values in your table storage.

public static string firstmessage = null;

/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
    if (activity.Type == ActivityTypes.Message)
    {
        if (firstmessage == null)
        {
            firstmessage = activity.Text?.ToString();
        }

        storeuserinput(activity);

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

    }
    else
    {
        HandleSystemMessage(activity);
    }
    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

private void storeuserinput(Activity activity)
{
    var uid = activity.From.Id;
    var uname = activity.From.Name;

    if (activity.Text?.ToLower().ToString() == "no")
    {
        var userinput = firstmessage;
    }

    //extract other data from "activity" object

    //your code logic here
    //store data in your table storage

    //Note: specifcial scenario of user send attachment
}

And If you'd like to store data into Azure table storage, you can use WindowsAzure.Storage client library to store/add entities to a table.

Besides, the middleware functionality in the Bot Builder SDK enables us to intercept all messages that are exchanged between user and bot, you can refer to the following code snippet to achieve same requirement.

public class MyActivityLogger : IActivityLogger
{
    public async Task LogAsync(IActivity activity)
    {
        if (activity.From.Name!= "{your_botid_here}")
        {
            var uid = activity.From.Id;
            var uname = activity.From.Name;

            var userinput = (activity as IMessageActivity).Text?.ToString();

            //extract other data from "activity" properties

            //your code logic here
            //store data in your table storage

            //Note: specifcial scenario of user send attachment

        }
    }
}

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