Search code examples
botframework

Get all the conversations of a user


(New to Microsoft Bot Framework) Is there a way in which I can find all the existing conversations of a user? I am using the Microsoft Bot Framework (SDK4) to make a chat bot.

Requirement: I want to list all the conversations a user has had till now.


Solution

  • Bot Framework, at this time, doesn't have a way of ingesting all past conversations from a transcript or store.

    However, there are a couple options for how you can capture user conversations so you don't have to rely on a complete transcript of a conversation.

    The first option is to implement middleware that logs the activity or elements of the activity you want. What you choose to capture and what you choose to do with it is up to you. In the following code, I am logging in the console all user responses and all bot activity that isn't a typing event or an endOfConversation event. You would replace the console.log() calls with your custom code that would store the data. Just keep in mind, that whatever call you make here would happen on every user / bot activity. If you're looking to make API calls, consider storing the data in an object and saving it when the 'endOfConversation' is returned (or something similar).

    adapter.use(async (turnContext, next) => {
        // turnContext.(async (ctx, activities, next) => {
        //     activities.filter(a => a.type === 'message').forEach(a => console.log('From user: ', a));
        // });
        const userActivity = turnContext.activity;
        if (userActivity.from.role === 'user' && turnContext.activity.text.length > 0) {
            console.log('From user: ', userActivity);
        }
    
        turnContext.onSendActivities(async (sendContext, activities, nextSend) => {
            await nextSend();
    
            activities.filter(a => a.type !== 'typing' && a.type !== 'endOfConversation').forEach(a => console.log('From bot: ', a));
        });
        await next();
    });
    

    The second option is to model your bot after the logger / transcript-logger samples from the Botbuilder-Samples repo. You can modify the customLogger.js file to match your needs and output to a store.

    Hope of help!