UNPKG

9.37 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 return new (P || (P = Promise))(function (resolve, reject) {
11 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
14 step((generator = generator.apply(thisArg, _arguments || [])).next());
15 });
16};
17Object.defineProperty(exports, "__esModule", { value: true });
18const botbuilder_core_1 = require("botbuilder-core");
19const botframework_connector_1 = require("botframework-connector");
20class TeamsInfo {
21 static getTeamDetails(context, teamId) {
22 return __awaiter(this, void 0, void 0, function* () {
23 const t = teamId || this.getTeamId(context);
24 if (!t) {
25 throw new Error('This method is only valid within the scope of a MS Teams Team.');
26 }
27 return yield this.getTeamsConnectorClient(context).teams.fetchTeamDetails(t);
28 });
29 }
30 static sendMessageToTeamsChannel(context, activity, teamsChannelId) {
31 return __awaiter(this, void 0, void 0, function* () {
32 if (!context) {
33 throw new Error("TurnContext cannot be null");
34 }
35 if (!activity) {
36 throw new Error("Activity cannot be null");
37 }
38 if (!teamsChannelId || !teamsChannelId) {
39 throw new Error("The teamsChannelId cannot be null or empty");
40 }
41 const convoParams = {
42 isGroup: true,
43 channelData: {
44 channel: {
45 id: teamsChannelId
46 }
47 },
48 activity: activity
49 };
50 const connectorClient = context.adapter.createConnectorClient(context.activity.serviceUrl);
51 const conversationResourceResponse = yield connectorClient.conversations.createConversation(convoParams);
52 const conversationReference = botbuilder_core_1.TurnContext.getConversationReference(context.activity);
53 conversationReference.conversation.id = conversationResourceResponse.id;
54 return [conversationReference, conversationResourceResponse.activityId];
55 });
56 }
57 static getTeamChannels(context, teamId) {
58 return __awaiter(this, void 0, void 0, function* () {
59 const t = teamId || this.getTeamId(context);
60 if (!t) {
61 throw new Error('This method is only valid within the scope of a MS Teams Team.');
62 }
63 const channelList = yield this.getTeamsConnectorClient(context).teams.fetchChannelList(t);
64 return channelList.conversations;
65 });
66 }
67 static getMembers(context) {
68 return __awaiter(this, void 0, void 0, function* () {
69 const teamId = this.getTeamId(context);
70 if (teamId) {
71 return yield this.getTeamMembers(context, teamId);
72 }
73 else {
74 const conversation = context.activity.conversation;
75 const conversationId = conversation && conversation.id ? conversation.id : undefined;
76 return yield this.getMembersInternal(this.getConnectorClient(context), conversationId);
77 }
78 });
79 }
80 static getPagedMembers(context, pageSize, continuationToken) {
81 return __awaiter(this, void 0, void 0, function* () {
82 const teamId = this.getTeamId(context);
83 const options = {
84 "continuationToken": continuationToken,
85 "pageSize": pageSize
86 };
87 if (teamId) {
88 return yield this.getPagedTeamMembers(context, teamId, pageSize, continuationToken);
89 }
90 else {
91 const conversation = context.activity.conversation;
92 const conversationId = conversation && conversation.id ? conversation.id : undefined;
93 return yield this.getPagedMembersInternal(this.getConnectorClient(context), conversationId, options);
94 }
95 });
96 }
97 static getMember(context, userId) {
98 return __awaiter(this, void 0, void 0, function* () {
99 const teamId = this.getTeamId(context);
100 if (teamId) {
101 return yield this.getTeamMember(context, teamId, userId);
102 }
103 else {
104 const conversation = context.activity.conversation;
105 const conversationId = conversation && conversation.id ? conversation.id : undefined;
106 return yield this.getMemberInternal(this.getConnectorClient(context), conversationId, userId);
107 }
108 });
109 }
110 static getTeamMembers(context, teamId) {
111 return __awaiter(this, void 0, void 0, function* () {
112 const t = teamId || this.getTeamId(context);
113 if (!t) {
114 throw new Error('This method is only valid within the scope of a MS Teams Team.');
115 }
116 return yield this.getMembersInternal(this.getConnectorClient(context), t);
117 });
118 }
119 static getPagedTeamMembers(context, teamId, pageSize, continuationToken) {
120 return __awaiter(this, void 0, void 0, function* () {
121 const t = teamId || this.getTeamId(context);
122 if (!t) {
123 throw new Error('This method is only valid within the scope of a MS Teams Team.');
124 }
125 const options = {
126 "continuationToken": continuationToken,
127 "pageSize": pageSize
128 };
129 return yield this.getPagedMembersInternal(this.getConnectorClient(context), t, options);
130 });
131 }
132 static getTeamMember(context, teamId, userId) {
133 return __awaiter(this, void 0, void 0, function* () {
134 const t = teamId || this.getTeamId(context);
135 if (!t) {
136 throw new Error('This method is only valid within the scope of a MS Teams Team.');
137 }
138 return yield this.getMemberInternal(this.getConnectorClient(context), t, userId);
139 });
140 }
141 static getMembersInternal(connectorClient, conversationId) {
142 return __awaiter(this, void 0, void 0, function* () {
143 if (!conversationId) {
144 throw new Error('The getMembers operation needs a valid conversationId.');
145 }
146 const teamMembers = yield connectorClient.conversations.getConversationMembers(conversationId);
147 teamMembers.forEach((member) => {
148 member.aadObjectId = member.objectId;
149 });
150 return teamMembers;
151 });
152 }
153 static getPagedMembersInternal(connectorClient, conversationId, options) {
154 return __awaiter(this, void 0, void 0, function* () {
155 if (!conversationId) {
156 throw new Error('The getPagedMembers operation needs a valid conversationId.');
157 }
158 const pagedMembersResult = yield connectorClient.conversations.getConversationPagedMembers(conversationId, options);
159 const teamsPagedMembersResult = {
160 "continuationToken": pagedMembersResult.continuationToken,
161 "members": pagedMembersResult.members
162 };
163 return teamsPagedMembersResult;
164 });
165 }
166 static getMemberInternal(connectorClient, conversationId, userId) {
167 return __awaiter(this, void 0, void 0, function* () {
168 if (!conversationId) {
169 throw new Error('The getMember operation needs a valid conversationId.');
170 }
171 if (!userId) {
172 throw new Error('The getMember operation needs a valid conversationId.');
173 }
174 const teamMember = yield connectorClient.conversations.getConversationMember(conversationId, userId);
175 return teamMember;
176 });
177 }
178 static getTeamId(context) {
179 if (!context) {
180 throw new Error('Missing context parameter');
181 }
182 if (!context.activity) {
183 throw new Error('Missing activity on context');
184 }
185 const channelData = context.activity.channelData;
186 const team = channelData && channelData.team ? channelData.team : undefined;
187 const teamId = team && typeof (team.id) === 'string' ? team.id : undefined;
188 return teamId;
189 }
190 static getConnectorClient(context) {
191 if (!context.adapter || !('createConnectorClient' in context.adapter)) {
192 throw new Error('This method requires a connector client.');
193 }
194 return context.adapter.createConnectorClient(context.activity.serviceUrl);
195 }
196 static getTeamsConnectorClient(context) {
197 const connectorClient = this.getConnectorClient(context);
198 return new botframework_connector_1.TeamsConnectorClient(connectorClient.credentials, { baseUri: context.activity.serviceUrl });
199 }
200}
201exports.TeamsInfo = TeamsInfo;
202//# sourceMappingURL=teamsInfo.js.map
\No newline at end of file