Handle user and conversation events in the v3 JavaScript SDK

APPLIES TO: SDK v3

This article demonstrates how your bot can handle events such as a user joining a conversation, adding a bot to a contacts list, or saying Goodbye when a bot is being removed from a conversation.

Greet a user on conversation join

The Bot Framework provides the conversationUpdate event for notifying your bot whenever a member joins or leaves a conversation. A conversation member can be a user or a bot.

The following code snippet allows the bot to greet new member(s) to a conversation or say Goodbye if it is being removed from the conversation.

bot.on('conversationUpdate', function (message) {
    if (message.membersAdded && message.membersAdded.length > 0) {
        // Say hello
        var isGroup = message.address.conversation.isGroup;
        var txt = isGroup ? "Hello everyone!" : "Hello...";
        var reply = new builder.Message()
                .address(message.address)
                .text(txt);
        bot.send(reply);
    } else if (message.membersRemoved) {
        // See if bot was removed
        var botId = message.address.bot.id;
        for (var i = 0; i < message.membersRemoved.length; i++) {
            if (message.membersRemoved[i].id === botId) {
                // Say goodbye
                var reply = new builder.Message()
                        .address(message.address)
                        .text("Goodbye");
                bot.send(reply);
                break;
            }
        }
    }
});

Acknowledge add to contacts list

The contactRelationUpdate event notifies your bot that a user has added you to their contacts list.

bot.on('contactRelationUpdate', function (message) {
    if (message.action === 'add') {
        var name = message.user ? message.user.name : null;
        var reply = new builder.Message()
                .address(message.address)
                .text("Hello %s... Thanks for adding me.", name || 'there');
        bot.send(reply);
    }
});

Add a first-run dialog

Since the conversationUpdate and the contactRelationUpdate event are not supported by all channels, a universal way to greet a user who joins a conversation is to add a first-run dialog.

In the following example we’ve added a function that triggers the dialog any time we’ve never seen a user before. You can customize the way an action is triggered by providing an onFindAction handler for your action.

// Add first run dialog
bot.dialog('firstRun', function (session) {    
    session.userData.firstRun = true;
    session.send("Hello...").endDialog();
}).triggerAction({
    onFindAction: function (context, callback) {
        // Only trigger if we've never seen user before
        if (!context.userData.firstRun) {
            // Return a score of 1.1 to ensure the first run dialog wins
            callback(null, 1.1);
        } else {
            callback(null, 0.0);
        }
    }
});

You can also customize what an action does after its been triggered by providing an onSelectAction handler. For trigger actions you can provide an onInterrupted handler to intercept an interruption before it occurs. For more information, see Handle user actions.

Additional resources