Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?

like image 488
Tas D Avatar asked Dec 24 '16 01:12

Tas D


People also ask

How do you get a teams conversation ID?

Get the conversation ID You can get this ID by either creating the conversation or storing it from any activity sent to you from that context. Store this ID for reference. After you get the appropriate address information, you can send your message.

What is activity in bot framework?

The Bot Framework Activity schema defines the activities that can be exchanged between a user or channel and a bot. Activities can represent human text or speech, app-to-app notifications, reactions to other messages, and so on.


1 Answers

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();
    }
});
like image 165
sGambolati Avatar answered Nov 09 '22 06:11

sGambolati