我使用botframework,c#创建机器人。我也使用qnamaker。我的问题是如何在数据库中存储用户的问题和答案。目前我只能在日志表中存储用户发送的消息,如下所示:
MessagesController.cs:
// if (activity.Type == ActivityTypes.Message)
{
*************************
// Log to Database
// *************************
// Instantiate the BotData dbContext
Model.qnamakerbotdataEntities DB = new Model.qnamakerbotdataEntities();
// Create a new UserLog object
Model.UserLog NewUserLog = new Model.UserLog();
// Set the properties on the UserLog object
NewUserLog.Channel = activity.ChannelId;
NewUserLog.UserID = activity.From.Id;
NewUserLog.UserName = activity.From.Name;
NewUserLog.created = DateTime.UtcNow;
NewUserLog.Message = activity.Text.Truncate(500);
// Add the UserLog object to UserLogs
DB.UserLogs.Add(NewUserLog);
// Save the changes to the database
DB.SaveChanges();
您是否知道如何存储用户发送的消息和机器人的响应?
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using QnABot.API;
using Microsoft.Bot.Builder.Dialogs.Internals;
namespace QnABot.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context,
IAwaitable<object> result)
{
//var activity = await result as Activity;
//// Prompt text
//await context.PostAsync("Feel free to ask me");
var privateData = context.PrivateConversationData;
var privateConversationInfo = IncrementInfoCount(privateData,
BotStoreType.BotPrivateConversationData.ToString());
var conversationData = context.ConversationData;
var conversationInfo = IncrementInfoCount(conversationData,
BotStoreType.BotConversationData.ToString());
var userData = context.UserData;
var userInfo = IncrementInfoCount(userData,
BotStoreType.BotUserData.ToString());
context.Wait(QnADialog);
PrivateData.SetValue(BotStoreType.BotPrivateConversationData.ToString(),
privateConversationInfo);
conversationData.SetValue(BotStoreType.BotConversationData.ToString(), conversationInfo);
userData.SetValue(BotStoreType.BotUserData.ToString(), userInfo);
}
private async Task QnADialog(IDialogContext context, IAwaitable<object> result)
{
var activityResult = await result as Activity;
var query = activityResult.Text;
var qnaResult = QnaApi.GetFirstQnaAnswer(query);
if (qnaResult == null)
{
string message = $"Sorry, I did not understand . Please
reformulate your question";
}
else
{
await context.PostAsync(qnaResult.answers[0].answer);
}
context.Wait(MessageReceivedAsync);
}
public class BotDataInfo
{
public int Count { get; set; }
}
private BotDataInfo IncrementInfoCount(IBotDataBag botdata, string key)
{
BotDataInfo info = null;
if (botdata.ContainsKey(key))
{
info = botdata.GetValue<BotDataInfo>(key);
info.Count++;
}
else
info = new BotDataInfo() { Count = 1 };
return info;
}
}
}
答案 0 :(得分:1)
你可以这样下面的东西。请注意我没有完成编译所需的所有代码,但如果需要,您应该可以调整它。主要部分是创建这样的回复var reply = activityResult.CreateReply();
,然后在message
中的if-else
变量中设置文字,然后设置回复文本然后发送
NewUserLog.Message = reply.Text;
await context.PostAsync(reply);
private async Task QnADialog(IDialogContext context, IAwaitable<object> result)
{
var activityResult = await result as Activity;
var query = activityResult.Text;
var reply = activityResult.CreateReply();
var qnaResult = QnaApi.GetFirstQnaAnswer(query);
string message = "";
if (qnaResult == null)
{
message = $"Sorry, I did not understand. Please reformulate your question";
}
else
{
message = qnaResult.answers[0].answer;
}
reply.Text = message;
Model.qnamakerbotdataEntities DB = new Model.qnamakerbotdataEntities();
// Create a new UserLog object
Model.UserLog NewUserLog = new Model.UserLog();
// Set the properties on the UserLog object
NewUserLog.Channel = reply.ChannelId;
NewUserLog.UserID = reply.From.Id;
NewUserLog.UserName = reply.From.Name;
NewUserLog.created = DateTime.UtcNow;
NewUserLog.Message = reply.Text;
await context.PostAsync(reply);
context.Wait(MessageReceivedAsync);
}