使用 Azure 聊天机器人时如何仅存储用户响应
How to store only the users response when using the Azure chat bot
我正在尝试将用户响应存储在 table 存储中。我只想存储他们输入的用户数据,对机器人的响应不感兴趣。
这怎么可能,另外这在触发词上是可能的,例如当用户说 "no" 时它会记录与机器人的第一次交互,例如 "Hello".
我对这个主题做了很多研究,但只存储用户输入的文档似乎较少。
如有任何帮助,我们将不胜感激!
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".
您似乎只想将用户输入存储在 table 存储中,而不是存储机器人响应的数据。为了达到这个要求,你可以拦截用户在MessagesController
(或对话框MessageReceivedAsync
方法中)发送的消息,然后从activity
中提取你想要的属性值并将值存储在你的table存储空间。
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
}
并且如果您想将数据存储到 Azure table 存储中,您可以使用 WindowsAzure.Storage client library 到 store/add 个实体到 table。
此外,Bot Builder SDK中的the middleware functionality可以让我们拦截用户和机器人之间交换的所有消息,您可以参考以下代码片段来实现相同的需求。
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
}
}
}
我正在尝试将用户响应存储在 table 存储中。我只想存储他们输入的用户数据,对机器人的响应不感兴趣。 这怎么可能,另外这在触发词上是可能的,例如当用户说 "no" 时它会记录与机器人的第一次交互,例如 "Hello".
我对这个主题做了很多研究,但只存储用户输入的文档似乎较少。
如有任何帮助,我们将不胜感激!
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".
您似乎只想将用户输入存储在 table 存储中,而不是存储机器人响应的数据。为了达到这个要求,你可以拦截用户在MessagesController
(或对话框MessageReceivedAsync
方法中)发送的消息,然后从activity
中提取你想要的属性值并将值存储在你的table存储空间。
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
}
并且如果您想将数据存储到 Azure table 存储中,您可以使用 WindowsAzure.Storage client library 到 store/add 个实体到 table。
此外,Bot Builder SDK中的the middleware functionality可以让我们拦截用户和机器人之间交换的所有消息,您可以参考以下代码片段来实现相同的需求。
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
}
}
}