0
votes

I am using code based on https://github.com/Microsoft/BotFramework-WebChat/blob/master/samples/15.d.backchannel-send-welcome-event/index.html

When I load the web page I get two of the welcome messages. Looking at the console output of my bot I can see two conversation updates happening.

This doesn't happen with the Bot framework emulator, which only shows one welcome message.

The only place where my code differs from the sample is in rendering:

window.WebChat.renderWebChat({
   directLine: window.WebChat.createDirectLine({ token }),
   store,
   styleOptions,
   userID: guid(),
}, document.getElementById('webchat'));

Why is this hapening? Why is the web channel sending two "join" events for the user?

My code handling conversation updates looks like this:

} else if (turnContext.activity.type === ActivityTypes.ConversationUpdate) {
if (DEBUG) { console.log("ConversationUpdate"); }

// Do we have any new members added to the conversation?
if (turnContext.activity.membersAdded.length !== 0) {
    // Iterate over all new members added to the conversation
    for (var idx in turnContext.activity.membersAdded) {
        console.log(turnContext.activity.membersAdded);
        // Greet anyone that was not the target (recipient) of this message
        // the 'bot' is the recipient for events from the channel,
        // turnContext.activity.membersAdded == turnContext.activity.recipient.Id indicates the
        // bot was added to the conversation.
        if (turnContext.activity.membersAdded[idx].id != turnContext.activity.recipient.id) {
            if (DEBUG) {console.log("Starting MASTER_DIALOG");}
            const user = await this.userProfile.get(turnContext, {});
            user.id = this.guid();
            await this.userProfile.set(turnContext, user);
            await this.userState.saveChanges(turnContext);
            return await dialogContext.beginDialog(MASTER_DIALOG)
        }
    }
}

}

1

1 Answers

1
votes

Using the ConversationUpdate event for sending a welcome message is not recommended. Read more about how to properly send a greeting message.

There will be two ConversationUpdate events per connection. One for when bot joins the conversation and one for when a (human) user joins the conversation. In your current code you are iterating over all new members, where you have to filter out the bot itself.

A better option would be to make use of a custom event sent using the backchannel. In the example you mention, you already have this functionality. It will sent a new event webchat/join to your bot, which even includes the browser language by default.