How to log a chat conversation with Bot Framework C# Bot Builder

淺唱寂寞╮ 提交于 2019-12-01 16:41:29
sGambolati

You can log all messages (from bot or from user) using Middleware.

For C# version you must implement the IActivityLogger and log what you want in the LogAsync method.

For Example:

public class DebugActivityLogger : IActivityLogger
{
    public async Task LogAsync(IActivity activity)
    {
        Debug.WriteLine($"From:{activity.From.Id} - To:{activity.Recipient.Id} - Message:{activity.AsMessageActivity()?.Text}");
    }
}

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);

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();
    }
});

Figured it out. I can use the Microsoft.Bot.Builder.History.IActivityLogger Interface

https://docs.botframework.com/en-us/csharp/builder/sdkreference/dc/dc6/namespace_microsoft_1_1_bot_1_1_builder_1_1_history.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!