Skip to content
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 3 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/botbuilder/src/botFrameworkAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ export class BotFrameworkAdapter extends BotAdapter implements IUserTokenProvide
* @remarks
* Override this in a derived class to create a mock connector client for unit testing.
*/
protected createConnectorClient(serviceUrl: string): ConnectorClient {
public createConnectorClient(serviceUrl: string): ConnectorClient {
Copy link
Member Author

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, and ConnectorClient.credentials is not.

I also wanted the new ConnectorClient to include the USER_AGENT, not sure if this is a problem in C#...

Copy link
Member

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.

Copy link
Member Author

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()

return new TeamsConnectorClient(connectorClient.credentials, { baseUri: context.activity.serviceUrl });

Copy link
Member

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

const client: ConnectorClient = new ConnectorClient(this.credentials, { baseUri: serviceUrl, userAgent: USER_AGENT} );
return client;
}
Expand Down
1 change: 1 addition & 0 deletions libraries/botbuilder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export * from './fileTranscriptStore';
export * from './inspectionMiddleware';
export * from './teamsActivityHandler';
export * from './teamsActivityHelpers';
export * from './teamsInfo';
export * from './teamsTurnContextHelpers';
export * from 'botbuilder-core';
82 changes: 82 additions & 0 deletions libraries/botbuilder/src/teamsInfo.ts
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 });
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {
TeamsActivityHandler,
} from 'botbuilder';
import {
CardFactory,
ChannelAccount,
ChannelInfo,
MessageFactory,
TeamInfo,
TeamsActivityHandler,
TurnContext,
} from 'botbuilder-core';
} from 'botbuilder';


export class ConversationUpdateBot extends TeamsActivityHandler {
constructor() {
Expand Down Expand Up @@ -41,7 +40,7 @@ export class ConversationUpdateBot extends TeamsActivityHandler {
await context.sendActivity(message);
await next();
});
this.onTeamsTeamRenamedEvent(async (channelInfo: ChannelInfo, teamInfo: TeamInfo, context: TurnContext, next: () => Promise<void>): Promise<void> => {
this.onTeamsTeamRenamedEvent(async (teamInfo: TeamInfo, context: TurnContext, next: () => Promise<void>): Promise<void> => {
const card = CardFactory.heroCard('Team Renamed', `${teamInfo.name} is the new Team name`);
const message = MessageFactory.attachment(card);
await context.sendActivity(message);
Expand Down