Search code examples
botframework

endOfConversation not a function


My bot right now is using local memory, the goal is whenever a conversation end. I want to delete everything from local memory about this user. So I tried this onEndOfConversation.

Apparently this error shows up saying that onEndOfConversation is not a function. enter image description here

This is my code :

const { CardFactory } = require('botbuilder');
const { DialogBot } = require('./dialogBot');
const WelcomeCard = require('./resources/welcomeCard.json');

class DialogAndWelcomeBot extends DialogBot {
    constructor(conversationState, userState, dialog) {
        super(conversationState, userState, dialog);

        this.onMembersAdded(async (context, next) => {
            const membersAdded = context.activity.membersAdded;
            for (let cnt = 0; cnt < membersAdded.length; cnt++) {
                if (membersAdded[cnt].id !== context.activity.recipient.id) {
                    //const welcomeCard = CardFactory.adaptiveCard(WelcomeCard);
                    //await context.sendActivity({ attachments: [welcomeCard] });
                    await dialog.run(context, conversationState.createProperty('DialogState'));
                }
            }

            // By calling next() you ensure that the next BotHandler is run.
            await next();
        });

        
        this.onEndOfConversation(async (context, next) => {
            console.log("END!");
            await conversationState.delete(context);
            await userState.delete(context);
        });
    }
}

module.exports.DialogAndWelcomeBot = DialogAndWelcomeBot;

So how should I do this? If onEndOfConversation isn't recognize, what alternatives I can do to clear user and conversation from the memory after a dialogue ends.


Solution

  • The endOfConversation activity handler is used internally when a bot is also coupled with a skill. When a conversation is ended by the user, the bot then sends this activity type to the skill notifying it that the conversation has ended with the user.

    There are different ways you could attack this. The method I use is component dialogs. Modeled after the cancelAndHelpDialog design, when a user types "cancel" or "quit", the user is brought to an exit dialog where feedback is gathered, etc.

    As part of the exiting process, you could call conversationState.delete() within the dialog followed by cancelAllDialogs(true).

    Hope of help!