Moblize IT
Moblize IT

Reputation: 1328

Microsoft teams bot getting members of a channel

in our use case, we have the team channel id with me. This is captured and stored into the DB in the event below

this.onTeamsChannelCreatedEvent(async (channelInfo: any, teamInfo: any, turnContext: any, next: () => Promise<void>): Promise<void> => {
}

We are running a periodic job and need to find out the members of a channel to perform some operations. So, how can i find members info from the channelid?

I have seen examples which using the context or context.activity. But in this case context wont be available. we are using nodejs v4.

My code here is

var client = new ConnectorClient(credentials, { baseUri: channel.serviceUrl });

            client.fetchMembers(channel.serviceUrl, channel.channelId,
                (err, result) => {
                  if (err) {
                      functions.logger.log("failed to fetch team members")
                  }
                  else {
                    functions.logger.log("Team members are", result)
                  }
                }
              );

This now throws error

TypeError: client.fetchMembers is not a function

i have also tried client.fetchMembers but similar error.

Upvotes: 0

Views: 901

Answers (2)

mdrichardson
mdrichardson

Reputation: 7241

You'll want to use teamsInfo.getTeamMembers() or teamsInfo.getPagedMembers() (if you want a paginated list), as in this sample:

    async getPagedMembers(context) {
        var continuationToken;
        var members = [];
        do {
            var pagedMembers = await TeamsInfo.getPagedMembers(context, 100, continuationToken);
            continuationToken = pagedMembers.continuationToken;
            members.push(...pagedMembers.members);
        } while (continuationToken !== undefined);
        return members;
    }

Upvotes: 0

Hilton Giesenow
Hilton Giesenow

Reputation: 10804

There are two main ways you can do this:

  1. Using the Microsoft Graph, in particular see the List Members operation.

  2. By having a bot registered in the channel, which it sounds like you have already. You don't need an 'activity' at all, you can do this any time by accessing the Conversations object. In fact, this code doesn't even need to run in your "bot" project at all - as long as the bot is added to the team/channel, it will work to use the bot's credentials. You can see sample code here in the Microsoft docs, and I have a sample from a recent conference session, doing this in dotnet from a console app over here.

One thing that's worth noting, the members of a channel and the members of a Team are largely the same (only excluding Private channels), so that's why some of these options refer to the Team or Group.

Upvotes: 1

Related Questions