Search code examples
botframeworkmicrosoft-teams

How to get user details in Bot Framework


I am building a Teams Bot which needs to send out Adaptive Cards to users.

When the bot is installed, i am sending a Welcome message to the user. From the welcome message, i am able to get the user's Teams ID using activity.from.id.

From ID::29:1O_abckkskldjflkjfslxxxxxxxx

With this id, i tried to get the User Details using const member=TeamsInfo.getMember(context, context.activity.from.id);

However I am still unable to get the user details.

How to get the email id of the user so that in future i can send notifications to the user ?


Solution

  • According to the docs, you pass the "id" to getMember only for the dotnet libraries. For Node, it looks like it wants the email/UPN - see here, whereas you're sending activity.from.id. Perhaps try call getPagedMembers instead - if there's only one user (i.e. it's a 1-1 chat), it's effectively the same as getMember.

    Working Snippet::

      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) {
                        var continuationToken;
                        var members = [];
                        do {
                            var pagedMembers = await TeamsInfo.getPagedMembers(context, 100, continuationToken);
                            continuationToken = pagedMembers.continuationToken;
                            members.push(...pagedMembers.members);
                        } while (continuationToken !== undefined);
                        members.forEach(member => {
                            const userEmail=member.userPrincipalName;
                        });
                        const card = CardFactory.adaptiveCard(welcomeCard);
                        const message = {
                            attachments: [card],
                            summary: 'Welcome'
                        };
                        await context.sendActivity(message);
                    }
                }
                // By calling next() you ensure that the next BotHandler is run.
                await next();
            });