fanshaoO

在Bot V3版本中我们可以通过实现IActivityLogger接口在进行,但是在Bot V4版本中已经没有这个接口了

不过在V4中可以使用TranscriptLoggerMiddleware日志记录。

话不多说,开始吧。

通过 Bot Builder SDK for .NET 创建机器人

VS新建项目选择BotFramework,如果没有这个模板,可以到下面链接去下载

https://marketplace.visualstudio.com/items?itemName=BotBuilder.botbuilderv4

ITranscriptLogger

 实现ITranscriptLogger接口

1 public class TranscriptLogger : ITranscriptLogger
2 {
3         public async Task LogActivityAsync(IActivity activity)
4         {
5             Debug.WriteLine($"From:{activity.From.Id} - To:{activity.Recipient.Id} - Message:{activity.AsMessageActivity()?.Text}");
6         }
7 }

在bot中注入TranscriptLoggerMiddleware中间件

1 services.AddTransient<ITranscriptLogger, TranscriptLogger>();
2             
3 services.AddBot<EchoBot1Bot>(options =>
4 {
5         var transcriptLoggerService = services.BuildServiceProvider().GetService<ITranscriptLogger>();
6         options.Middleware.Add(new TranscriptLoggerMiddleware(transcriptLoggerService));
7     ......
8 }

使用Bot Framework Emulator与bot交互

Emulator使用说明请看https://github.com/Microsoft/BotFramework-Emulator/wiki/Getting-Started

启动项目

 

然后打开Emulator,点击open bot

选择项目中的.bot文件

 发送消息给bot

 

Log Result

查看ITranscriptLogger记录的信息

 

在输出窗口我们可以看到两条记录,一条是给bot发送的消息,一条是bot的回复消息。

搞完

 就这么简单搞完啦,这个中间件还可以做其他更多的用途,怎么用就看大家脑洞啦~~~~

相关文章:

  • 2021-09-09
  • 2019-01-08
  • 2021-06-21
  • 2021-10-03
  • 2018-09-25
  • 2021-05-16
  • 2021-06-10
  • 2021-11-11
猜你喜欢
  • 2021-08-15
  • 2021-09-13
  • 2021-08-27
  • 2021-05-03
  • 2018-07-11
  • 2021-12-27
  • 2021-07-20
相关资源
相似解决方案