-
Notifications
You must be signed in to change notification settings - Fork 281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Teams] Add TeamsInfo helper class, minor fix in ConversationUpdateBot scenario #1253
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { | ||
ChannelInfo, | ||
ConversationList, | ||
TeamsChannelAccount, | ||
TeamsChannelData, | ||
TeamDetails, | ||
TurnContext | ||
} from 'botbuilder-core'; | ||
import { ConnectorClient, TeamsConnectorClient } from 'botframework-connector'; | ||
|
||
import { BotFrameworkAdapter } from './botFrameworkAdapter'; | ||
|
||
export class TeamsInfo { | ||
static async getTeamDetails(context: TurnContext): Promise<TeamDetails> { | ||
const teamId = this.getTeamId(context); | ||
if (!teamId) { | ||
throw new Error('This method is only valid within the scope of a MS Teams Team.'); | ||
} | ||
return await this.getTeamsConnectorClient(context).teams.fetchTeamDetails(teamId); | ||
} | ||
|
||
static async getChannels(context: TurnContext): Promise<ChannelInfo[]> { | ||
const teamId = this.getTeamId(context); | ||
if (!teamId) { | ||
throw new Error('This method is only valid within the scope of a MS Teams Team.'); | ||
} | ||
const channelList: ConversationList = await this.getTeamsConnectorClient(context).teams.fetchChannelList(teamId); | ||
return channelList.conversations; | ||
} | ||
|
||
static async getMembers(context: TurnContext): Promise<TeamsChannelAccount[]> { | ||
const connectorClient = this.getConnectorClient(context); | ||
const teamId = this.getTeamId(context); | ||
if (teamId) { | ||
return await this.getMembersInternal(connectorClient, teamId); | ||
} else { | ||
const conversation = context.activity.conversation; | ||
const conversationId = conversation && conversation.id ? conversation.id : undefined; | ||
return await this.getMembersInternal(connectorClient, conversationId); | ||
} | ||
} | ||
|
||
private static async getMembersInternal(connectorClient: ConnectorClient, conversationId: string): Promise<TeamsChannelAccount[]> { | ||
if (!conversationId) { | ||
throw new Error('The getMembers operation needs a valid conversationId.'); | ||
} | ||
|
||
const teamMembers = await connectorClient.conversations.getConversationMembers(conversationId); | ||
return teamMembers as TeamsChannelAccount[]; | ||
} | ||
|
||
private static getTeamId(context: TurnContext): string { | ||
if (!context) { | ||
throw new Error('Missing context parameter'); | ||
} | ||
if (!context.activity) { | ||
throw new Error('Missing activity on context'); | ||
} | ||
const channelData = context.activity.channelData as TeamsChannelData; | ||
const team = channelData && channelData.team ? channelData.team : undefined; | ||
const teamId = team && typeof(team.id) === 'string' ? team.id : undefined; | ||
return teamId; | ||
} | ||
|
||
private static getConnectorClient(context: TurnContext): ConnectorClient { | ||
if (!context.adapter || !('createConnectorClient' in context.adapter)) { | ||
throw new Error('This method requires a connector client.') | ||
} | ||
return (context.adapter as BotFrameworkAdapter).createConnectorClient(context.activity.serviceUrl); | ||
} | ||
|
||
private static getTeamsConnectorClient(context: TurnContext): TeamsConnectorClient { | ||
const connectorClient = this.getConnectorClient(context); | ||
return new TeamsConnectorClient(connectorClient.credentials, { baseUri: context.activity.serviceUrl }); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I switched this to public because the
BotFrameworkAdapter.credentials
is protected, andConnectorClient.credentials
is not.I also wanted the new ConnectorClient to include the
USER_AGENT
, not sure if this is a problem in C#...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you not make the Credentials on ConnectorClient public - that would also bring the two implementations closer together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The credentials in the connectorClient are public, I use them in
TeamsInfo.getTeamsConnectorClient()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding USER_AGENT header - the C# uses the same HttpClient as the regular connector. So that should be good.
Its really just a couple of extra REST calls on top of the current connector