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

前端 未结 2 733
闹比i
闹比i 2021-01-18 07:52

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, Confi

相关标签:
2条回答
  • 2021-01-18 08:14

    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();
        }
    });
    
    0 讨论(0)
  • 2021-01-18 08:18

    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

    0 讨论(0)
提交回复
热议问题