Search code examples
node.jsbotframeworkchatbotmicrosoft-teams

Get all team members when bot is installed in MS Teams


I'm trying to implement Proactive Messaging with a MS Teams bot. According to the docs i have to get a conversationReference before sending any message to the user so I implemented the onMembersAdded event listener as follows:

class TeamsBot extends TeamsActivityHandler {

  constructor() {
    super();

    this.onConversationUpdate(async (context, next) => {
      this.addConversationReference(context);
    });

    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) {
          this.addConversationReference(context);
        }
      }
      await next();
    });
  }

The problem is that I get notified only of the user that is installing the app even tough my Team (and channel) has plenty of members:

team members

How do I get conversation references for every team member once the bot is installed?


Solution

  • It's important to note the difference between the users in a Team and the conversation reference between a user and a bot. What the docs are -trying- to say is this:

    1. When you bot is added to the team, you can get a list of the users in the team.
    2. You can use that list of users to look up in your own database the conversation references you need to have already for those users in order to messages. This is because those are references to separate conversations - the bot is in the Team/Channel and has that reference, but the 1-1 chats the bot wants to use to message the users personally are each separate individual chats.
    3. If you don't yet have a conversation reference for any/all of those users, you need to get them. That means the user needs to either (a) install the bot themselves or you need to (b) install it for them via the Graph.

    The docs, imo, don't really explain that well - they assume that by enumerating the list of users you'll be getting -actual- conversation references straight away, and/or that you might be able to initiate the conversation using the bot framework itself.