1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var __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 | };
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.TeamsInfo = void 0;
|
20 | const botbuilder_core_1 = require("botbuilder-core");
|
21 | const botframework_connector_1 = require("botframework-connector");
|
22 | const cloudAdapter_1 = require("./cloudAdapter");
|
23 | const teamsActivityHelpers_1 = require("./teamsActivityHelpers");
|
24 |
|
25 |
|
26 |
|
27 | class TeamsInfo {
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
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 |
|
58 |
|
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 |
|
70 |
|
71 |
|
72 |
|
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 |
|
92 |
|
93 |
|
94 |
|
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 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
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 |
|
153 |
|
154 |
|
155 |
|
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 |
|
169 |
|
170 |
|
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 |
|
187 |
|
188 |
|
189 |
|
190 |
|
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 |
|
211 |
|
212 |
|
213 |
|
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 |
|
230 |
|
231 |
|
232 |
|
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 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
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 |
|
266 |
|
267 |
|
268 |
|
269 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 | }
|
362 | exports.TeamsInfo = TeamsInfo;
|
363 |
|
\ | No newline at end of file |