Skip to content

Commit cf9dd7f

Browse files
committed
Update get-teams-context.md
1 parent e068d3f commit cf9dd7f

1 file changed

Lines changed: 94 additions & 145 deletions

File tree

msteams-platform/bots/how-to/get-teams-context.md

Lines changed: 94 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Get Teams specific context for your bot, fetch user profile, get si
44
ms.topic: conceptual
55
ms.localizationpriority: high
66
ms.owner: angovil
7-
ms.date: 03/16/2026
7+
ms.date: 03/24/2026
88
---
99
# Get Teams specific context for your bot
1010

@@ -30,23 +30,13 @@ The following sample code uses the paged endpoint for fetching the roster:
3030
* [Sample code reference](https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/main/samples/graph-proactive-installation/csharp/ProactiveAppInstallation/Bots/ProactiveBot.cs#L78)
3131

3232
```csharp
33-
public class MyBot : TeamsActivityHandler
33+
app.OnMessage(async context =>
3434
{
35-
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
36-
{
37-
var members = new List<TeamsChannelAccount>();
38-
string continuationToken = null;
39-
40-
do
41-
{
42-
// Gets a paginated list of members of one-on-one, group, or team conversation.
43-
var currentPage = await TeamsInfo.GetPagedMembersAsync(turnContext, 100, continuationToken, cancellationToken);
44-
continuationToken = currentPage.ContinuationToken;
45-
members.AddRange(currentPage.Members);
46-
}
47-
while (continuationToken != null);
48-
}
49-
}
35+
var conversationId = context.Activity.Conversation.Id;
36+
37+
// Gets all members of the conversation.
38+
var members = await context.Api.Conversations.Members.GetAsync(conversationId);
39+
});
5040
```
5141

5242
# [TypeScript](#tab/typescript)
@@ -56,40 +46,25 @@ public class MyBot : TeamsActivityHandler
5646
* [Sample code reference](https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/main/samples/graph-proactive-installation/nodejs/bots/proactiveBot.js#L38)
5747

5848
```typescript
59-
export class MyBot extends TeamsActivityHandler {
60-
constructor() {
61-
super();
62-
63-
// See https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0 to learn more about the message and other activity types.
64-
this.onMessage(async (turnContext, next) => {
65-
var continuationToken;
66-
var members = [];
67-
68-
do {
69-
// Gets a paginated list of members of one-on-one, group, or team conversation.
70-
var pagedMembers = await TeamsInfo.getPagedMembers(turnContext, 100, continuationToken);
71-
continuationToken = pagedMembers.continuationToken;
72-
members.push(...pagedMembers.members);
73-
}
74-
while(continuationToken !== undefined)
75-
76-
// By calling next() you ensure that the next BotHandler is run.
77-
await next();
78-
});
79-
}
80-
}
49+
app.on('message', async ({ activity, api }) => {
50+
const conversationId = activity.conversation.id;
51+
52+
// Gets all members of the conversation.
53+
const members = await api.conversations.members(conversationId).get();
54+
});
8155
```
8256

8357
# [Python](#tab/python)
8458

8559
[SDK reference](/python/api/botbuilder-core/botbuilder.core.teams.teamsinfo?view=botbuilder-py-latest&preserve-view=true#botbuilder-core-teams-teamsinfo-get-team-members)
8660

8761
```python
88-
async def _show_members(
89-
self, turn_context: TurnContext
90-
):
91-
# Get a conversationMember from a team.
92-
members = await TeamsInfo.get_team_members(turn_context)
62+
@app.on_message
63+
async def handle_message(ctx: ActivityContext[MessageActivity]):
64+
conversation_id = ctx.activity.conversation.id
65+
66+
# Gets all members of the conversation.
67+
members = await ctx.api.conversations.members(conversation_id).get_all()
9368
```
9469

9570
# [JSON](#tab/json)
@@ -150,15 +125,14 @@ The following sample code is used to get single member details:
150125
* [Sample code reference](https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/main/samples/bot-sequential-flow-adaptive-cards/csharp/SequentialUserSpecificFlow/Bots/UserSpecificBot.cs#L37)
151126

152127
```csharp
153-
public class MyBot : TeamsActivityHandler
128+
app.OnMessage(async context =>
154129
{
155-
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
156-
{
157-
// Gets the account of a single conversation member.
158-
// This works in one-on-one, group, and team scoped conversations.
159-
var member = await TeamsInfo.GetMemberAsync(turnContext, turnContext.Activity.From.Id, cancellationToken);
160-
}
161-
}
130+
var conversationId = context.Activity.Conversation.Id;
131+
var memberId = context.Activity.From.Id;
132+
133+
// Gets a single member by ID.
134+
var member = await context.Api.Conversations.Members.GetByIdAsync(conversationId, memberId);
135+
});
162136
```
163137

164138
# [TypeScript](#tab/typescript)
@@ -168,19 +142,13 @@ public class MyBot : TeamsActivityHandler
168142
* [Sample code reference](https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/main/samples/bot-conversation/nodejs/bots/teamsConversationBot.js#L186)
169143

170144
```typescript
171-
export class MyBot extends TeamsActivityHandler {
172-
constructor() {
173-
super();
145+
app.on('message', async ({ activity, api }) => {
146+
const conversationId = activity.conversation.id;
147+
const memberId = activity.from.id;
174148

175-
// See learn.microsoft.com/en-us/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0 to learn more about the message and other activity types.
176-
this.onMessage(async (turnContext, next) => {
177-
const member = await TeamsInfo.getMember(turnContext, encodeURI('[email protected]'));
178-
179-
// By calling next() you ensure that the next BotHandler is run.
180-
await next();
181-
});
182-
}
183-
}
149+
// Gets a single member by ID.
150+
const member = await api.conversations.members(conversationId).getById(memberId);
151+
});
184152
```
185153

186154
# [Python](#tab/python)
@@ -190,11 +158,13 @@ export class MyBot extends TeamsActivityHandler {
190158
* [Sample code reference](https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/main/samples/bot-conversation/python/bots/teams_conversation_bot.py#L67)
191159

192160
```python
193-
async def _show_members(
194-
self, turn_context: TurnContext
195-
):
196-
# TeamsInfo.get_member: Gets the member of a team scoped conversation.
197-
member = await TeamsInfo.get_member(turn_context, turn_context.activity.from_property.id)
161+
@app.on_message
162+
async def handle_message(ctx: ActivityContext[MessageActivity]):
163+
conversation_id = ctx.activity.conversation.id
164+
member_id = ctx.activity.from_.id
165+
166+
# Gets a single member by ID.
167+
member = await ctx.api.conversations.members(conversation_id).get(member_id)
198168
```
199169

200170
# [JSON](#tab/json)
@@ -250,22 +220,21 @@ The following sample code is used to get team's details:
250220
* [Sample code reference](https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/main/samples/TeamsJS/msteams-application-qbot/Source/Microsoft.Teams.Apps.QBot.Web/Bot/QBotTeamInfo.cs#L44)
251221

252222
```csharp
253-
public class MyBot : TeamsActivityHandler
223+
app.OnMessage(async context =>
254224
{
255-
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
225+
var teamId = context.Activity.ChannelData?.Team?.Id;
226+
227+
if (teamId != null)
256228
{
257-
// Gets the details for the given team id. This only works in team scoped conversations.
258-
// TeamsGetTeamInfo: Gets the TeamsInfo object from the current activity.
259-
TeamDetails teamDetails = await TeamsInfo.GetTeamDetailsAsync(turnContext, turnContext.Activity.TeamsGetTeamInfo().Id, cancellationToken);
260-
if (teamDetails != null) {
261-
await turnContext.SendActivityAsync($"The groupId is: {teamDetails.AadGroupId}");
262-
}
263-
else {
264-
// Sends a message activity to the sender of the incoming activity.
265-
await turnContext.SendActivityAsync($"Message did not come from a channel in a team.");
266-
}
229+
// Gets team details by ID.
230+
var teamDetails = await context.Api.Teams.GetByIdAsync(teamId);
231+
await context.Send($"The groupId is: {teamDetails.AadGroupId}");
267232
}
268-
}
233+
else
234+
{
235+
await context.Send("Message did not come from a channel in a team.");
236+
}
237+
});
269238
```
270239

271240
# [TypeScript](#tab/typescript)
@@ -275,45 +244,34 @@ public class MyBot : TeamsActivityHandler
275244
* [Sample code reference](https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/main/samples/app-complete-sample/nodejs/server/dialogs/teams/fetchTeamInfoDialog.js#L21)
276245

277246
```typescript
278-
export class MyBot extends TeamsActivityHandler {
279-
constructor() {
280-
super();
281-
282-
// See https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0 to learn more about the message and other activity types.
283-
this.onMessage(async (turnContext, next) => {
284-
285-
// Gets the details for the given team id.
286-
const teamDetails = await TeamsInfo.getTeamDetails(turnContext);
287-
if (teamDetails) {
288-
289-
// Sends a message activity to the sender of the incoming activity.
290-
await turnContext.sendActivity(`The group ID is: ${teamDetails.aadGroupId}`);
291-
} else {
292-
await turnContext.sendActivity('This message did not come from a channel in a team.');
293-
}
294-
295-
// By calling next() you ensure that the next BotHandler is run.
296-
await next();
297-
});
247+
app.on('message', async ({ activity, api, send }) => {
248+
const teamId = activity.channelData?.team?.id;
249+
250+
if (teamId) {
251+
// Gets team details by ID.
252+
const teamDetails = await api.teams.getById(teamId);
253+
await send(`The group ID is: ${teamDetails.aadGroupId}`);
254+
} else {
255+
await send('This message did not come from a channel in a team.');
298256
}
299-
}
257+
});
300258
```
301259

302260
# [Python](#tab/python)
303261

304262
[SDK reference](/python/api/botbuilder-core/botbuilder.core.teams.teamsinfo?view=botbuilder-py-latest&preserve-view=true#botbuilder-core-teams-teamsinfo-get-team-details)
305263

306264
```python
307-
async def _show_details(self, turn_context: TurnContext):
308-
309-
# Gets the details for the given team id.
310-
team_details = await TeamsInfo.get_team_details(turn_context)
311-
312-
# MessageFactory.text(): Specifies the type of text data in a message attachment.
313-
reply = MessageFactory.text(f"The team name is {team_details.name}. The team ID is {team_details.id}. The AADGroupID is {team_details.aad_group_id}.")
314-
315-
# Sends a message activity to the sender of the incoming activity.
316-
await turn_context.send_activity(reply)
265+
@app.on_message
266+
async def handle_message(ctx: ActivityContext[MessageActivity]):
267+
team_id = ctx.activity.channel_data.get("team", {}).get("id") if ctx.activity.channel_data else None
268+
269+
if team_id:
270+
# Gets team details by ID.
271+
team_details = await ctx.api.teams.get_by_id(team_id)
272+
await ctx.send(f"The team name is {team_details.name}. The AADGroupID is {team_details.aad_group_id}.")
273+
else:
274+
await ctx.send("Message did not come from a channel in a team.")
317275
```
318276

319277
# [JSON](#tab/json)
@@ -353,57 +311,48 @@ The following sample code is used to get the list of channels in a team:
353311
* [Sample code reference](https://github.com/OfficeDev/Microsoft-Teams-Samples/blob/main/samples/TeamsJS/msteams-application-qbot/Source/Microsoft.Teams.Apps.QBot.Web/Bot/QBotTeamInfo.cs#L57)
354312

355313
```csharp
356-
public class MyBot : TeamsActivityHandler
314+
app.OnMessage(async context =>
357315
{
358-
// Override this in a derived class to provide logic specific to Message activities.
359-
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
360-
{
361-
// Returns a list of channels in a Team. This only works in team scoped conversations.
362-
IEnumerable<ChannelInfo> channels = await TeamsInfo.GetTeamChannelsAsync(turnContext, turnContext.Activity.TeamsGetTeamInfo().Id, cancellationToken);
316+
var teamId = context.Activity.ChannelData?.Team?.Id;
363317

364-
// Sends a message activity to the sender of the incoming activity.
365-
await turnContext.SendActivityAsync($"The channel count is: {channels.Count()}");
318+
if (teamId != null)
319+
{
320+
// Gets channels (conversations) for the team.
321+
var channels = await context.Api.Teams.GetConversationsAsync(teamId);
322+
await context.Send($"The channel count is: {channels.Count}");
366323
}
367-
}
324+
});
368325
```
369326

370327
# [TypeScript](#tab/typescript)
371328

372329
[SDK reference](/javascript/api/botbuilder/teamsinfo?view=botbuilder-ts-latest&preserve-view=true#botbuilder-teamsinfo-getteamchannels)
373330

374331
```typescript
375-
export class MyBot extends TeamsActivityHandler {
376-
constructor() {
377-
super();
378-
379-
// See https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0 to learn more about the message and other activity types.
380-
this.onMessage(async (turnContext, next) => {
332+
app.on('message', async ({ activity, api, send }) => {
333+
const teamId = activity.channelData?.team?.id;
381334

382-
// Supports retrieving channels hosted by a team.
383-
const channels = await TeamsInfo.getTeamChannels(turnContext);
384-
385-
// Sends a message activity to the sender of the incoming activity.
386-
await turnContext.sendActivity(`The channel count is: ${channels.length}`);
387-
388-
// By calling next() you ensure that the next BotHandler is run.
389-
await next();
390-
});
335+
if (teamId) {
336+
// Gets channels (conversations) for the team.
337+
const channels = await api.teams.getConversations(teamId);
338+
await send(`The channel count is: ${channels.length}`);
391339
}
392-
}
340+
});
393341
```
394342

395343
# [Python](#tab/python)
396344

397345
[SDK reference](/python/api/botbuilder-core/botbuilder.core.teams.teamsinfo?view=botbuilder-py-latest&preserve-view=true#botbuilder-core-teams-teamsinfo-get-team-channels)
398346

399347
```python
400-
async def _show_channels(
401-
self, turn_context: TurnContext
402-
):
403-
# Supports retrieving channels hosted by a team.
404-
channels = await TeamsInfo.get_team_channels(turn_context)
405-
reply = MessageFactory.text(f"Total of {len(channels)} channels are currently in team")
406-
await turn_context.send_activity(reply)
348+
@app.on_message
349+
async def handle_message(ctx: ActivityContext[MessageActivity]):
350+
team_id = ctx.activity.channel_data.get("team", {}).get("id") if ctx.activity.channel_data else None
351+
352+
if team_id:
353+
# Gets channels (conversations) for the team.
354+
channels = await ctx.api.teams.get_conversations(team_id)
355+
await ctx.send(f"Total of {len(channels)} channels are currently in team")
407356
```
408357

409358
# [JSON](#tab/json)

0 commit comments

Comments
 (0)