UNPKG

17.4 kBJavaScriptView Raw
1"use strict";
2/**
3 * @module botbuilder
4 */
5/**
6 * Copyright (c) Microsoft Corporation. All rights reserved.
7 * Licensed under the MIT License.
8 */
9var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
10 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
11 return new (P || (P = Promise))(function (resolve, reject) {
12 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
13 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
14 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
15 step((generator = generator.apply(thisArg, _arguments || [])).next());
16 });
17};
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.TeamsInfo = void 0;
20const botbuilder_core_1 = require("botbuilder-core");
21const botframework_connector_1 = require("botframework-connector");
22const cloudAdapter_1 = require("./cloudAdapter");
23const teamsActivityHelpers_1 = require("./teamsActivityHelpers");
24/**
25 * Provides utility methods for the events and interactions that occur within Microsoft Teams.
26 */
27class TeamsInfo {
28 /**
29 * Gets the meeting participant for the given meeting id and participant id. This only works in
30 * teams scoped meeting conversations.
31 * @param context The [TurnContext](xref:botbuilder-core.TurnContext) for this turn.
32 * @param meetingId The meeting ID to fetch
33 * @param participantId The participant ID to fetch
34 * @param tenantId The tenant ID to use when scoping the request
35 * @returns The [TeamsMeetingParticipant](xref:botbuilder-core.TeamsMeetingParticipant) fetched
36 */
37 static getMeetingParticipant(context, meetingId, participantId, tenantId) {
38 return __awaiter(this, void 0, void 0, function* () {
39 if (!context) {
40 throw new Error('context is required.');
41 }
42 const activity = context.activity;
43 if (meetingId == null) {
44 const meeting = teamsActivityHelpers_1.teamsGetTeamMeetingInfo(activity);
45 meetingId = meeting === null || meeting === void 0 ? void 0 : meeting.id;
46 }
47 if (!meetingId) {
48 throw new Error('meetingId is required.');
49 }
50 if (participantId == null) {
51 const from = activity.from;
52 participantId = from === null || from === void 0 ? void 0 : from.aadObjectId;
53 }
54 if (!participantId) {
55 throw new Error('participantId is required.');
56 }
57 // Note: === undefined here because tenant ID is technically an optional parameter. If a user specifically
58 // wants to disable defaulting of tenant ID they can pass `null`.
59 if (tenantId === undefined) {
60 const tenant = teamsActivityHelpers_1.teamsGetTenant(activity);
61 tenantId = tenant === null || tenant === void 0 ? void 0 : tenant.id;
62 }
63 return this.getTeamsConnectorClient(context).teams.fetchMeetingParticipant(meetingId, participantId, {
64 tenantId,
65 });
66 });
67 }
68 /**
69 * Gets the information for the given meeting id.
70 * @param context The [TurnContext](xref:botbuilder-core.TurnContext) for this turn.
71 * @param meetingId The BASE64-encoded id of the Teams meeting.
72 * @returns The [TeamsMeetingInfo](xref:botbuilder-core.TeamsMeetingInfo) fetched
73 */
74 static getMeetingInfo(context, meetingId) {
75 return __awaiter(this, void 0, void 0, function* () {
76 if (!context) {
77 throw new Error('context is required.');
78 }
79 const activity = context.activity;
80 if (meetingId == null) {
81 const meeting = teamsActivityHelpers_1.teamsGetTeamMeetingInfo(activity);
82 meetingId = meeting === null || meeting === void 0 ? void 0 : meeting.id;
83 }
84 if (!meetingId) {
85 throw new Error('meetingId or TurnContext containing meetingId is required.');
86 }
87 return this.getTeamsConnectorClient(context).teams.fetchMeetingInfo(meetingId);
88 });
89 }
90 /**
91 * Gets the details for the given team id. This only works in teams scoped conversations.
92 * @param context The [TurnContext](xref:botbuilder-core.TurnContext) for this turn.
93 * @param teamId The id of the Teams team.
94 * @returns The [TeamDetails](xref:botbuilder-core.TeamDetails) fetched
95 */
96 static getTeamDetails(context, teamId) {
97 return __awaiter(this, void 0, void 0, function* () {
98 const t = teamId || this.getTeamId(context);
99 if (!t) {
100 throw new Error('This method is only valid within the scope of a MS Teams Team.');
101 }
102 return yield this.getTeamsConnectorClient(context).teams.fetchTeamDetails(t);
103 });
104 }
105 /**
106 * Creates a new thread in a Teams chat and sends an [Activity](xref:botframework-schema.Activity) to that new thread.
107 * @param context The [TurnContext](xref:botbuilder-core.TurnContext) for this turn.
108 * @param activity The [Activity](xref:botframework-schema.Activity) to send.
109 * @param teamsChannelId The Team's Channel ID, note this is distinct from the Bot Framework activity property with same name.
110 * @param botAppId The bot's appId. This is only used when context.adapter is an instance of CloudAdapter.
111 * @returns The [ConversationReference](xref:botframework-schema.ConversationReference) and the id of the [Activity](xref:botframework-schema.Activity) (if sent).
112 */
113 static sendMessageToTeamsChannel(context, activity, teamsChannelId, botAppId) {
114 return __awaiter(this, void 0, void 0, function* () {
115 if (!context) {
116 throw new Error('TurnContext cannot be null');
117 }
118 if (!activity) {
119 throw new Error('Activity cannot be null');
120 }
121 if (!teamsChannelId || !teamsChannelId) {
122 throw new Error('The teamsChannelId cannot be null or empty');
123 }
124 const convoParams = {
125 isGroup: true,
126 channelData: {
127 channel: {
128 id: teamsChannelId,
129 },
130 },
131 activity: activity,
132 };
133 let conversationReference;
134 let newActivityId;
135 if (botAppId && context.adapter instanceof cloudAdapter_1.CloudAdapter) {
136 yield context.adapter.createConversationAsync(botAppId, botbuilder_core_1.Channels.Msteams, context.activity.serviceUrl, null, convoParams, (turnContext) => __awaiter(this, void 0, void 0, function* () {
137 conversationReference = botbuilder_core_1.TurnContext.getConversationReference(context.activity);
138 newActivityId = turnContext.activity.id;
139 }));
140 }
141 else {
142 const connectorClient = context.adapter.createConnectorClient(context.activity.serviceUrl);
143 const conversationResourceResponse = yield connectorClient.conversations.createConversation(convoParams);
144 conversationReference = botbuilder_core_1.TurnContext.getConversationReference(context.activity);
145 conversationReference.conversation.id = conversationResourceResponse.id;
146 newActivityId = conversationResourceResponse.activityId;
147 }
148 return [conversationReference, newActivityId];
149 });
150 }
151 /**
152 * Returns a list of channels in a Team. This only works in teams scoped conversations.
153 * @param context The [TurnContext](xref:botbuilder-core.TurnContext) for this turn.
154 * @param teamId ID of the Teams team.
155 * @returns The list of [ChannelInfo](xref:botframework-schema.ChannelInfo) objects with the conversations.
156 */
157 static getTeamChannels(context, teamId) {
158 return __awaiter(this, void 0, void 0, function* () {
159 const t = teamId || this.getTeamId(context);
160 if (!t) {
161 throw new Error('This method is only valid within the scope of a MS Teams Team.');
162 }
163 const channelList = yield this.getTeamsConnectorClient(context).teams.fetchChannelList(t);
164 return channelList.conversations;
165 });
166 }
167 /**
168 * Gets the conversation members of a one-on-one or group chat.
169 * @param context The [TurnContext](xref:botbuilder-core.TurnContext) for this turn.
170 * @returns The list of [TeamsChannelAccount](xref:botframework-schema.TeamsChannelAccount).
171 */
172 static getMembers(context) {
173 return __awaiter(this, void 0, void 0, function* () {
174 const teamId = this.getTeamId(context);
175 if (teamId) {
176 return yield this.getTeamMembers(context, teamId);
177 }
178 else {
179 const conversation = context.activity.conversation;
180 const conversationId = conversation && conversation.id ? conversation.id : undefined;
181 return yield this.getMembersInternal(this.getConnectorClient(context), conversationId);
182 }
183 });
184 }
185 /**
186 * Gets a pagined list of members of one-on-one, group, or team conversation.
187 * @param context The [TurnContext](xref:botbuilder-core.TurnContext) for this turn.
188 * @param pageSize Suggested number of entries on a page.
189 * @param continuationToken A continuation token.
190 * @returns The [TeamsPagedMembersResult](xref:botframework-schema.TeamsPagedMembersResult) with the list of members.
191 */
192 static getPagedMembers(context, pageSize, continuationToken) {
193 return __awaiter(this, void 0, void 0, function* () {
194 const teamId = this.getTeamId(context);
195 const options = {
196 continuationToken: continuationToken,
197 pageSize: pageSize,
198 };
199 if (teamId) {
200 return yield this.getPagedTeamMembers(context, teamId, pageSize, continuationToken);
201 }
202 else {
203 const conversation = context.activity.conversation;
204 const conversationId = conversation && conversation.id ? conversation.id : undefined;
205 return yield this.getPagedMembersInternal(this.getConnectorClient(context), conversationId, options);
206 }
207 });
208 }
209 /**
210 * Gets the account of a single conversation member.
211 * @param context The [TurnContext](xref:botbuilder-core.TurnContext) for this turn.
212 * @param userId ID of the user in question.
213 * @returns The [TeamsChannelAccount](xref:botframework-schema.TeamsChannelAccount) of the member.
214 */
215 static getMember(context, userId) {
216 return __awaiter(this, void 0, void 0, function* () {
217 const teamId = this.getTeamId(context);
218 if (teamId) {
219 return yield this.getTeamMember(context, teamId, userId);
220 }
221 else {
222 const conversation = context.activity.conversation;
223 const conversationId = conversation && conversation.id ? conversation.id : undefined;
224 return yield this.getMemberInternal(this.getConnectorClient(context), conversationId, userId);
225 }
226 });
227 }
228 /**
229 * Gets the list of [TeamsChannelAccount](xref:botframework-schema.TeamsChannelAccount) within a team.
230 * @param context The [TurnContext](xref:botbuilder-core.TurnContext) for this turn.
231 * @param teamId ID of the Teams team.
232 * @returns The list of [TeamsChannelAccount](xref:botframework-schema.TeamsChannelAccount) of the members.
233 */
234 static getTeamMembers(context, teamId) {
235 return __awaiter(this, void 0, void 0, function* () {
236 const t = teamId || this.getTeamId(context);
237 if (!t) {
238 throw new Error('This method is only valid within the scope of a MS Teams Team.');
239 }
240 return yield this.getMembersInternal(this.getConnectorClient(context), t);
241 });
242 }
243 /**
244 * Gets a paginated list of members of a team.
245 * @param context The [TurnContext](xref:botbuilder-core.TurnContext) for this turn.
246 * @param teamId ID of the Teams team.
247 * @param pageSize The number of entries on the page.
248 * @param continuationToken The continuationToken token.
249 * @returns A [TeamsPagedMembersResult](xref:botframework-schema.TeamsPagedMembersResult) with the list of members.
250 */
251 static getPagedTeamMembers(context, teamId, pageSize, continuationToken) {
252 return __awaiter(this, void 0, void 0, function* () {
253 const t = teamId || this.getTeamId(context);
254 if (!t) {
255 throw new Error('This method is only valid within the scope of a MS Teams Team.');
256 }
257 const options = {
258 continuationToken: continuationToken,
259 pageSize: pageSize,
260 };
261 return yield this.getPagedMembersInternal(this.getConnectorClient(context), t, options);
262 });
263 }
264 /**
265 * Gets the account of a member in a teams scoped conversation.
266 * @param context The [TurnContext](xref:botbuilder-core.TurnContext) for this turn.
267 * @param teamId ID of the Teams team.
268 * @param userId ID of the Teams user.
269 * @returns The [TeamsChannelAccount](xref:botframework-schema.TeamsChannelAccount) of the member.
270 */
271 static getTeamMember(context, teamId, userId) {
272 return __awaiter(this, void 0, void 0, function* () {
273 const t = teamId || this.getTeamId(context);
274 if (!t) {
275 throw new Error('This method is only valid within the scope of a MS Teams Team.');
276 }
277 return yield this.getMemberInternal(this.getConnectorClient(context), t, userId);
278 });
279 }
280 /**
281 * @private
282 */
283 static getMembersInternal(connectorClient, conversationId) {
284 return __awaiter(this, void 0, void 0, function* () {
285 if (!conversationId) {
286 throw new Error('The getMembers operation needs a valid conversationId.');
287 }
288 const teamMembers = yield connectorClient.conversations.getConversationMembers(conversationId);
289 teamMembers.forEach((member) => {
290 member.aadObjectId = member.objectId;
291 });
292 return teamMembers;
293 });
294 }
295 /**
296 * @private
297 */
298 static getPagedMembersInternal(connectorClient, conversationId, options) {
299 return __awaiter(this, void 0, void 0, function* () {
300 if (!conversationId) {
301 throw new Error('The getPagedMembers operation needs a valid conversationId.');
302 }
303 const pagedMembersResult = yield connectorClient.conversations.getConversationPagedMembers(conversationId, options);
304 const teamsPagedMembersResult = {
305 continuationToken: pagedMembersResult.continuationToken,
306 members: pagedMembersResult.members,
307 };
308 return teamsPagedMembersResult;
309 });
310 }
311 /**
312 * @private
313 */
314 static getMemberInternal(connectorClient, conversationId, userId) {
315 return __awaiter(this, void 0, void 0, function* () {
316 if (!conversationId) {
317 throw new Error('The getMember operation needs a valid conversationId.');
318 }
319 if (!userId) {
320 throw new Error('The getMember operation needs a valid userId.');
321 }
322 const teamMember = yield connectorClient.conversations.getConversationMember(conversationId, userId);
323 return teamMember;
324 });
325 }
326 /**
327 * @private
328 */
329 static getTeamId(context) {
330 if (!context) {
331 throw new Error('Missing context parameter');
332 }
333 if (!context.activity) {
334 throw new Error('Missing activity on context');
335 }
336 const channelData = context.activity.channelData;
337 const team = channelData && channelData.team ? channelData.team : undefined;
338 const teamId = team && typeof team.id === 'string' ? team.id : undefined;
339 return teamId;
340 }
341 /**
342 * @private
343 */
344 static getConnectorClient(context) {
345 var _a;
346 const client = context.adapter && 'createConnectorClient' in context.adapter
347 ? context.adapter.createConnectorClient(context.activity.serviceUrl)
348 : (_a = context.turnState) === null || _a === void 0 ? void 0 : _a.get(context.adapter.ConnectorClientKey);
349 if (!client) {
350 throw new Error('This method requires a connector client.');
351 }
352 return client;
353 }
354 /**
355 * @private
356 */
357 static getTeamsConnectorClient(context) {
358 const connectorClient = this.getConnectorClient(context);
359 return new botframework_connector_1.TeamsConnectorClient(connectorClient.credentials, { baseUri: context.activity.serviceUrl });
360 }
361}
362exports.TeamsInfo = TeamsInfo;
363//# sourceMappingURL=teamsInfo.js.map
\No newline at end of file