我正在使用Microsoft bot框架来创建机器人并使用直接渠道合并到Web应用程序中。在对话期间,我需要书签或喜欢来自机器人的消息或响应。
答案 0 :(得分:1)
Bot Framework在SDK中没有实现这个功能。您可以利用中间件功能实现自己。
一般的想法是,您可以与用户一起保存每个活动消息对。并为mark
或like
创建global message handlers或检测中间件中的每封邮件,以检查用户是否mark
或like
。如果您可以为之前保存的最后一条消息标记mard
标记。
有关中间件使用的示例,请参阅https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/core-Middleware了解C#和https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/capability-middlewareLogging了解Node.js。
如有任何疑问,请随时告诉我。
答案 1 :(得分:0)
实施 Microsoft.Bot.Builder.History 命名空间中的 IActivityLogger ,以将IMessageActivity消息存储/标记到数据库或缓存中。
IActivityLogger 将intercept来自您的对话框中实现IDialog接口的每条消息。
这是用于拦截从用户和机器人发送和接收的每条消息。
1)对于实现IDialog界面的对话框:
using Microsoft.Bot.Builder.History;
using Microsoft.Bot.Connector;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Threading.Tasks;
namespace DemoBot.Dialogs
{
public class Logger : IActivityLogger
{
private readonly IMongoClient client;
private readonly IMongoCollection<BsonDocument> collection;
public Logger()
{
client = new MongoClient();
collection = client.GetDatabase("test").GetCollection<BsonDocument>("botLog");
}
public Task LogAsync(IActivity activity)
{
IMessageActivity msgToBeLogged = activity.AsMessageActivity();
BsonDocument objectToBeLogged = new BsonDocument
{
{ "messageText", new BsonString(msgToBeLogged.Text) },
{ "timeStamp", new BsonDateTime(Convert.ToDateTime(msgToBeLogged.Timestamp)) },
{ "recipientId", new BsonString(msgToBeLogged.Recipient.Id) },
{ "fromId", new BsonString(msgToBeLogged.From.Id) },
{ "conversationId", new BsonString(msgToBeLogged.Conversation.Id) },
{ "fromName", new BsonString(msgToBeLogged.From.Name) },
{ "toName", new BsonString(msgToBeLogged.Recipient.Name) },
{ "channnel", new BsonString(msgToBeLogged.ChannelId) },
{ "serviceUrl",new BsonString(msgToBeLogged.ServiceUrl) },
{ "locale", new BsonString(msgToBeLogged.Locale)}
};
return Task.Run(() =>
{
LogIntoDB(objectToBeLogged);
});
}
public void LogIntoDB(BsonDocument activityDetails)
{
collection.InsertOne(activityDetails);
}
}
}
2)对于继承LuisDialog 类的Dialog,在DispatchToIntentHandler方法中编写日志代码,因为传入的消息将通过该方法解析为适当的处理程序:
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
using Microsoft.Bot.Connector;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DemoBot.Dialogs
{
[Serializable]
public class RootDialog : LuisDialog<object>
{
public Task StartAsync(IDialogContext context)
{
return Task.Run(() => { context.Wait(MessageReceived); });
}
protected override Task DispatchToIntentHandler(IDialogContext context, IAwaitable<IMessageActivity> item, IntentRecommendation bestIntent, LuisResult result)
{
IMessageActivity msgToBeLogged = context.MakeMessage();
BsonDocument objectToBeLogged = new BsonDocument
{
{ "messageText", new BsonString(msgToBeLogged.Text) },
{ "timeStamp", new BsonDateTime(Convert.ToDateTime(msgToBeLogged.Timestamp)) },
{ "recipientId", new BsonString(msgToBeLogged.Recipient.Id) },
{ "fromId", new BsonString(msgToBeLogged.From.Id) },
{ "conversationId", new BsonString(msgToBeLogged.Conversation.Id) },
{ "fromName", new BsonString(msgToBeLogged.From.Name) },
{ "toName", new BsonString(msgToBeLogged.Recipient.Name) },
{ "channnel", new BsonString(msgToBeLogged.ChannelId) },
{ "serviceUrl",new BsonString(msgToBeLogged.ServiceUrl) },
{ "locale", new BsonString(msgToBeLogged.Locale)}
};
Task.Run(() =>
{
LogIntoDB(objectToBeLogged);
});
return base.DispatchToIntentHandler(context, item, bestIntent, result);
}
public void LogIntoDB(BsonDocument activityDetails)
{
collection.InsertOne(activityDetails);
}
public Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> item)
{
return Task.Run(() =>
{
context.Wait(MessageReceived);
});
}
}
}
对于日志记录我正在使用MongoDB,但如果您愿意,也可以使用SQL Server。
最后使用Autofac IoC在global.asax.cs文件中注入依赖项。
using Autofac;
using DemoBot.Dialogs;
using Microsoft.Bot.Builder.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
namespace DemoBot
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Conversation.UpdateContainer(builder =>
{
builder.RegisterType<Logger>().AsImplementedInterfaces().InstancePerDependency();
});
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
}