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

port: [#3922] Corrected GetConversationMembers to use TeamsInfo helper class rather… (#5874) #4151

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
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