sala96
sala96

Reputation: 63

Botframework v4. How to clear turnContext.Activity.MembersAdded, so welcome message is sent again

The bot sends a welcome message when a Facebook user messages the bot for the first time. The bot checks if turnContext.Activity.MembersAdded is not null, and then sends a welcome message. Although this list is never wiped.

Do you know how I could clear this list, so that next time a user messages the bot it sends the welcome message again

I tried simply clearing it using LINQ but it didn't work

if (turnContext.Activity.MembersAdded != null)
{
    await SendWelcomeMessageAsync(turnContext, cancellationToken);
}

Thanks in advance guys!

Upvotes: 2

Views: 632

Answers (2)

JJ_Wailes
JJ_Wailes

Reputation: 2227

The way that the bot stores user information (userState) and the way Facebook does is very different. UserState is created and maintained by the bot developer (you). Facebook ALSO maintains a list of people that have connected to an app, either through the facebook apps such as games, or via Messenger. Facebook stores this information in either PSIDs or ASIDs, accessable through the Facebook Graph API v2.0

On the bot framework side, the turnContext.Activity.MembersAdded is hit when a facebook user first connects with the bot (and thus creates their PSID or ASID to be stored in the Facebook Graph API). You could, theoretically, go in and clear the PSIDs/ASIDs from your Facebook app (the bot) on the Facebook side, but bear in mind that once a ASID/PSID is created for a Facebook user, that's THEIR ASID/PSID. If they're removed from the bot so they could be "reconnected", Facebook Graph is going to give them back that same PSID/ASID and thus the .MembersAdded flag is not going to trip and you'll be left right where you were.

If you're trying to create a new "welcome message" every time a user is away from a bot for a certain duration of time, or starts a conversation concerning a different topic, you're going to have to track the converstation and user State on your side (give your users unique userIds as soon as they connect), and store and track it in a database of your own. Then you could, using bot accessors, have a resettable welcome message (one based on time, or conversationID).

Upvotes: 3

Sebastian Zolg
Sebastian Zolg

Reputation: 1961

Make sure you're checking for the right activity type in your OnTurnAsync handler and that it is truly send again when reconnecting. Extra note that different channels might behave differently.

switch (turnContext.Activity.Type)
{
    case ActivityTypes.ConversationUpdate:
        // Send a welcome & help message to the user.
        if (turnContext.Activity.MembersAdded != null)
        {
            await SendWelcomeMessageAsync(turnContext, cancellationToken);
        }

        break;
}

The fact that you might not get the welcome message again, is due to the conversation state stored in your bot state. See the documentation for a very good introduction.

Knowing this the only way I can think of a solution is to delete the bot state after you finished the conversation. This can be archived with the DeleteAsync API.

await _stateAccessors.ConversationDialogState.DeleteAsync(turnContext, cancellationToken);

Also make sure your ending your dialogs explicitly by calling await await dc.EndDialogAsync(); or await dc.CancelAllDialogsAsync();. This way you can make sure having a fresh conversation with no state at all.

Assuming Facebook messenger is sending a ConversationUpdate activity again next time you talk to the bot, the welcome message should be send once more.

If for some reason the Facebook messenger is not sending an activity of type ConversationUpdate after you connected first, then you could send the welcome message when receiving an activity of type Message. In this scenario the user has to send something first. You can decide if you want to ignore it and send the welcome message instead or process what was said and show welcome message along.

Upvotes: 0

Related Questions