diff --git a/libraries/botbuilder-dialogs-adaptive/package.json b/libraries/botbuilder-dialogs-adaptive/package.json index 4e750e8752..8e0a83bb66 100644 --- a/libraries/botbuilder-dialogs-adaptive/package.json +++ b/libraries/botbuilder-dialogs-adaptive/package.json @@ -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" }, diff --git a/libraries/botbuilder-dialogs-adaptive/src/actions/getActivityMembers.ts b/libraries/botbuilder-dialogs-adaptive/src/actions/getActivityMembers.ts index 554e2433cc..9b55199f46 100644 --- a/libraries/botbuilder-dialogs-adaptive/src/actions/getActivityMembers.ts +++ b/libraries/botbuilder-dialogs-adaptive/src/actions/getActivityMembers.ts @@ -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; @@ -103,14 +105,41 @@ export class GetActivityMembers 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(context.adapter.ConnectorClientKey); + if (!client) { + throw new Error('This method requires a connector client.'); + } + + return client; } /** diff --git a/libraries/botbuilder-dialogs-adaptive/src/actions/getConversationMembers.ts b/libraries/botbuilder-dialogs-adaptive/src/actions/getConversationMembers.ts index 2ae035ea02..4ca344cd9e 100644 --- a/libraries/botbuilder-dialogs-adaptive/src/actions/getConversationMembers.ts +++ b/libraries/botbuilder-dialogs-adaptive/src/actions/getConversationMembers.ts @@ -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 { @@ -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; @@ -95,15 +88,32 @@ export class GetConversationMembers 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(context.adapter.ConnectorClientKey); + if (!client) { + throw new Error('This method requires a connector client.'); } + + return client; } /**