简体   繁体   中英

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:

团队成员

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

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.

To start conversation in channel - I think you are looking for something like this - see 3 point MessageAllMembers . Try the sample available - Teams Conversation Bot . (see for code snippet )

You can use TeamsInfo.getPagedMembers to get members details in the team

UPDATE - To start 1:1 conversation - We have this implemented with C# in Company Communicator App template . Company communicator's code snippet for app installation.

To install it first, refer the document shared by @Hilton Giesenow, JavaScript snippet and follow create authProvider .

Have a look at this method where we are creating a 1:1 conversation in Company Communicator. ( JavaScript method ref ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM