Skip to content

Commit

Permalink
Merge pull request #261 from southworks/southworks/port/correct-getCo…
Browse files Browse the repository at this point in the history
…nversationMembers-to-use-teaminfo

port: [microsoft#3922] Corrected GetConversationMembers to use TeamsInfo helper class rather… (#5874)
  • Loading branch information
mraitersw authored Mar 9, 2022
2 parents c21cf94 + 1764479 commit 8cdfa35
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
1 change: 1 addition & 0 deletions libraries/botbuilder-dialogs-adaptive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"botbuilder-dialogs-adaptive-runtime-core": "4.1.6",
"botbuilder-dialogs-declarative": "4.1.6",
"botbuilder-lg": "4.1.6",
"botframework-connector": "4.1.6",
"lodash": "^4.17.21",
"node-fetch": "^2.6.7"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
DialogContext,
DialogTurnResult,
} from 'botbuilder-dialogs';
import { BotFrameworkAdapter, TurnContext } from 'botbuilder';
import { ConnectorClient } from 'botframework-connector';

export interface GetActivityMembersConfiguration extends DialogConfiguration {
activityId?: StringProperty;
Expand Down Expand Up @@ -103,14 +105,41 @@ export class GetActivityMembers<O extends object = {}> extends Dialog implements
id = value.toString();
}

const adapter = dc.context.adapter;
if (typeof adapter['getActivityMembers'] === 'function') {
const result = await adapter['getActivityMembers'].getActivityMembers(dc.context, id);
dc.state.setValue(this.property.getValue(dc.state), result);
return await dc.endDialog(result);
} else {
throw new Error('getActivityMembers() not supported by the current adapter.');
const result = await this.getActivityMembers(dc.context, id);
dc.state.setValue(this.property.getValue(dc.state), result);
return await dc.endDialog(result);
}

private async getActivityMembers(context: TurnContext, activityId: string) {
// If no activity was passed in, use the current activity.
if (!activityId) {
activityId = context.activity.id;
}

if (!context.activity.conversation) {
throw new Error('[GetActivityMembers]: Missing conversation');
}
const conversationId = context.activity.conversation.id;

if (!conversationId || conversationId.trim() === '') {
throw new Error('[GetActivityMembers]: Missing conversation.id');
}

const connectorClient = this.getConnectorClient(context);
const accounts = await connectorClient.conversations.getActivityMembers(conversationId, activityId);
return accounts;
}

private getConnectorClient(context: TurnContext): ConnectorClient {
const client =
context.adapter && 'createConnectorClient' in context.adapter
? (context.adapter as BotFrameworkAdapter).createConnectorClient(context.activity.serviceUrl)
: context.turnState?.get<ConnectorClient>(context.adapter.ConnectorClientKey);
if (!client) {
throw new Error('This method requires a connector client.');
}

return client;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import type { TurnContext } from 'botbuilder';
import type { BotFrameworkAdapter, TurnContext } from 'botbuilder';
import { BoolProperty, StringProperty } from '../properties';

import {
Expand All @@ -23,14 +23,7 @@ import {
DialogContext,
DialogTurnResult,
} from 'botbuilder-dialogs';

interface CompatibleAdapter {
getConversationMembers(context: TurnContext): unknown;
}

function isCompatibleAdapter(adapter: unknown): adapter is CompatibleAdapter {
return adapter && typeof (adapter as CompatibleAdapter).getConversationMembers === 'function';
}
import { ConnectorClient } from 'botframework-connector';

export interface GetConversationMembersConfiguration extends DialogConfiguration {
property?: StringProperty;
Expand Down Expand Up @@ -95,15 +88,32 @@ export class GetConversationMembers<O extends object = {}>
if (this.disabled && this.disabled.getValue(dc.state)) {
return await dc.endDialog();
}
const result = await this.getMembers(dc.context);
dc.state.setValue(this.property.getValue(dc.state), result);
return await dc.endDialog(result);
}

private async getMembers(context: TurnContext) {
const conversationId = context.activity?.conversation?.id;
if (!conversationId) {
throw new Error('[GetConversationMembers]: The getMembers operation needs a valid conversation id.');
}

const adapter = dc.context.adapter;
if (isCompatibleAdapter(adapter)) {
const result = await adapter.getConversationMembers(dc.context);
dc.state.setValue(this.property.getValue(dc.state), result);
return await dc.endDialog(result);
} else {
throw new Error('getConversationMembers() not supported by the current adapter.');
const connectorClient = this.getConnectorClient(context);
const teamMembers = await connectorClient.conversations.getConversationMembers(conversationId);
return teamMembers;
}

private getConnectorClient(context: TurnContext): ConnectorClient {
const client =
context.adapter && 'createConnectorClient' in context.adapter
? (context.adapter as BotFrameworkAdapter).createConnectorClient(context.activity.serviceUrl)
: context.turnState?.get<ConnectorClient>(context.adapter.ConnectorClientKey);
if (!client) {
throw new Error('This method requires a connector client.');
}

return client;
}

/**
Expand Down

0 comments on commit 8cdfa35

Please sign in to comment.