如何使用Bot Framework C#Bot Builder记录聊天对话 [英] How to log a chat conversation with Bot Framework C# Bot Builder
问题描述
我想记录我的机器人对话(到文本文件或数据库).我想捕获机器人的所有输入和输出,包括由FormFlow,Confirms等生成的任何文本.我不需要诸如卡片的图形元素,但也可以从它们中获取文本.
I would like to log my bot conversations (to a text file or DB). I want to capture all of the input and output from the bot, including any text generated by FormFlow, Confirms, etc. I don't need the graphical elements like cards, but it would be nice to have the text from them too.
在我的应用程序中的每个输入/输出之后添加日志记录语句似乎不切实际,尤其是因为我无法轻松准确地知道FormFlow向用户发送了哪些文本.
It doesn't seem practical to add logging statements after each input/output in my app, particularly since I can't easily tell exactly what text was sent to the user by FormFlow.
做到这一点的最佳方法是什么?
What's the best way to do this?
推荐答案
您可以使用对于C#版本,您必须实现 IActivityLogger 并在LogAsync方法中记录所需的内容.
For C# version you must implement the IActivityLogger and log what you want in the LogAsync method.
例如:
public class DebugActivityLogger : IActivityLogger
{
public async Task LogAsync(IActivity activity)
{
Debug.WriteLine($"From:{activity.From.Id} - To:{activity.Recipient.Id} - Message:{activity.AsMessageActivity()?.Text}");
}
}
最后,您必须使用以下内容(在global.asax中)在AutoFact中注册:
Finally, you must register in AutoFact with something like this (in global.asax):
var builder = new ContainerBuilder();
builder.RegisterType<DebugActivityLogger>().AsImplementedInterfaces().InstancePerDependency();
builder.Update(Conversation.Container);
如果您使用的是nodejs版本,则它更简单:
If you're using the nodejs version, its more straightforward:
const logUserConversation = (event) => { console.log('message: ' + event.text + ', user: ' + event.address.user.name);
};
// Middleware for logging
bot.use({
receive: function (event, next) {
logUserConversation(event);
next();
},
send: function (event, next) {
logUserConversation(event);
next();
}
});
这篇关于如何使用Bot Framework C#Bot Builder记录聊天对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!