如何使用botframework为邮件添加书签
我正在使用Microsoft bot framework创建一个bot,并使用direct channel将其整合到web应用程序中。在对话过程中,我需要为来自bot的消息或响应添加书签或添加书签 Bot框架没有在SDK中实现此功能。您可以利用中间件特性自行实现它 总体思路是,您可以与用户一起保存每个活动消息对。并创建一个for如何使用botframework为邮件添加书签,botframework,Botframework,我正在使用Microsoft bot framework创建一个bot,并使用direct channel将其整合到web应用程序中。在对话过程中,我需要为来自bot的消息或响应添加书签或添加书签 Bot框架没有在SDK中实现此功能。您可以利用中间件特性自行实现它 总体思路是,您可以与用户一起保存每个活动消息对。并创建一个formark或like或检测中间件中的每条消息,以检查用户是否说了mark或like。当您可以为先前保存的最后一条消息标记mard标记时 有关中间件使用的示例,请参阅For
mark
或like
或检测中间件中的每条消息,以检查用户是否说了mark
或like
。当您可以为先前保存的最后一条消息标记mard
标记时
有关中间件使用的示例,请参阅For C#和For Node.js
任何进一步的问题,请随时通知我。实现Microsoft.Bot.Builder.History命名空间中的IActivityLogger,将IMessageActivity消息存储/标记到DB或缓存中 IActivityLogger将显示来自实现IDialog界面的对话框的每条消息 这是为了拦截从用户和机器人发送和接收到fro的每一条消息 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);
}
}
}
使用Microsoft.Bot.Builder.History;
使用Microsoft.Bot.Connector;
使用MongoDB.Bson;
使用MongoDB.Driver;
使用制度;
使用System.Threading.Tasks;
名称空间DemoBot.Dialogs
{
公共类记录器:IActivityLogger
{
私有只读IMongoClient客户端;
私人只读IMongoCollection;
公共记录器()
{
客户端=新的MongoClient();
collection=client.GetDatabase(“test”).GetCollection(“botLog”);
}
公共任务日志异步(IActivity活动)
{
IMessageActivity msgToBeLogged=activity.AsMessageActivity();
BsonDocument OBJECTTOBELOGED=新的BsonDocument
{
{“messageText”,新的BsonString(msgtobeloged.Text)},
{“timeStamp”,新的BsonDateTime(Convert.ToDateTime(msgtobeloged.timeStamp)),
{“recipientId”,新的BsonString(msgToBeLogged.Recipient.Id)},
{“fromId”,新的BsonString(msgtobeloged.From.Id)},
{“conversationId”,新的BsonString(msgToBeLogged.Conversation.Id)},
{“fromName”,新的BsonString(msgtobeloged.From.Name)},
{“toName”,新的BsonString(msgtobeloged.Recipient.Name)},
{“channnel”,新的BsonString(msgtobeloged.ChannelId)},
{“serviceUrl”,新的BsonString(msgToBeLogged.serviceUrl)},
{“locale”,新的BsonString(msgtobeloged.locale)}
};
返回任务。运行(()=>
{
LogIntoDB(ObjectToBeloged);
});
}
public void LogIntoDB(b文档活动详细信息)
{
收藏。InsertOne(活动详情);
}
}
}
2) 对于继承LuisDialog类的对话框,请在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);
});
}
}
}
使用Microsoft.Bot.Builder.Dialogs;
使用Microsoft.Bot.Builder.Luis;
使用Microsoft.Bot.Builder.Luis.Models;
使用Microsoft.Bot.Connector;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
名称空间DemoBot.Dialogs
{
[可序列化]
公共类根对话框:LuisDialog
{
公共任务StartSync(IDialogContext上下文)
{
返回Task.Run(()=>{context.Wait(MessageReceived);});
}
受保护的覆盖任务DispatchToIntentHandler(IDialogContext上下文、IAwaitable项、IntentRecommension bestIntent、LuisResult结果)
{
IMessageActivity msgToBeLogged=context.MakeMessage();
BsonDocument OBJECTTOBELOGED=新的BsonDocument
{
{“messageText”,新的BsonString(msgtobeloged.Text)},
{“timeStamp”,新的BsonDateTime(Convert.ToDateTime(msgtobeloged.timeStamp)),
{“recipientId”,新的BsonString(msgToBeLogged.Recipient.Id)},
{“fromId”,新的BsonString(msgtobeloged.From.Id)},
{“conversationId”,新的BsonString(msgToBeLogged.Conversation.Id)},
{“fromName”,新的BsonString(msgtobeloged.From.Name)},
{“toName”,新的BsonString(msgtobeloged.Recipient.Name)},
{“channnel”,新的BsonString(msgtobeloged.ChannelId)},
{“serviceUrl”,新的BsonString(msgToBeLogged.serviceUrl)},
{“locale”,新的BsonString(msgtobeloged.locale)}
};
Task.Run(()=>
{
LogIntoDB(ObjectToBeloged);
});
返回base.DispatchToIntentHandler(上下文、项、最佳意图、结果);
}
public void LogIntoDB(b文档活动详细信息)
{
收藏。InsertOne(活动详情);
}
已接收公共任务消息(IDialogContext上下文,IAwaitable项)
{
返回任务。运行(()=>
{
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);
}
}
}
使用Autofac;
使用DemoBot.Dialogs;
使用Microsoft.Bot.Builder.Dialogs;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Http;
使用System.Web.Routing;
命名空间DemoBot
{
公共类WebAPI应用程序:System.Web.HttpApplication
{
受保护的无效应用程序\u Start()
{
C