@mastra/slack 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1042 @@
1
+ import { IntegrationFieldTypeEnum, IntegrationAuth, Integration } from '@mastra/core';
2
+ import { z } from 'zod';
3
+ import { WebClient } from '@slack/web-api';
4
+
5
+ function asyncGeneratorStep(n, t, e, r, o, a, c) {
6
+ try {
7
+ var i = n[a](c),
8
+ u = i.value;
9
+ } catch (n) {
10
+ return void e(n);
11
+ }
12
+ i.done ? t(u) : Promise.resolve(u).then(r, o);
13
+ }
14
+ function _asyncToGenerator(n) {
15
+ return function () {
16
+ var t = this,
17
+ e = arguments;
18
+ return new Promise(function (r, o) {
19
+ var a = n.apply(t, e);
20
+ function _next(n) {
21
+ asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
22
+ }
23
+ function _throw(n) {
24
+ asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
25
+ }
26
+ _next(void 0);
27
+ });
28
+ };
29
+ }
30
+ function _inheritsLoose(t, o) {
31
+ t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o);
32
+ }
33
+ function _regeneratorRuntime() {
34
+ _regeneratorRuntime = function () {
35
+ return e;
36
+ };
37
+ var t,
38
+ e = {},
39
+ r = Object.prototype,
40
+ n = r.hasOwnProperty,
41
+ o = Object.defineProperty || function (t, e, r) {
42
+ t[e] = r.value;
43
+ },
44
+ i = "function" == typeof Symbol ? Symbol : {},
45
+ a = i.iterator || "@@iterator",
46
+ c = i.asyncIterator || "@@asyncIterator",
47
+ u = i.toStringTag || "@@toStringTag";
48
+ function define(t, e, r) {
49
+ return Object.defineProperty(t, e, {
50
+ value: r,
51
+ enumerable: !0,
52
+ configurable: !0,
53
+ writable: !0
54
+ }), t[e];
55
+ }
56
+ try {
57
+ define({}, "");
58
+ } catch (t) {
59
+ define = function (t, e, r) {
60
+ return t[e] = r;
61
+ };
62
+ }
63
+ function wrap(t, e, r, n) {
64
+ var i = e && e.prototype instanceof Generator ? e : Generator,
65
+ a = Object.create(i.prototype),
66
+ c = new Context(n || []);
67
+ return o(a, "_invoke", {
68
+ value: makeInvokeMethod(t, r, c)
69
+ }), a;
70
+ }
71
+ function tryCatch(t, e, r) {
72
+ try {
73
+ return {
74
+ type: "normal",
75
+ arg: t.call(e, r)
76
+ };
77
+ } catch (t) {
78
+ return {
79
+ type: "throw",
80
+ arg: t
81
+ };
82
+ }
83
+ }
84
+ e.wrap = wrap;
85
+ var h = "suspendedStart",
86
+ l = "suspendedYield",
87
+ f = "executing",
88
+ s = "completed",
89
+ y = {};
90
+ function Generator() {}
91
+ function GeneratorFunction() {}
92
+ function GeneratorFunctionPrototype() {}
93
+ var p = {};
94
+ define(p, a, function () {
95
+ return this;
96
+ });
97
+ var d = Object.getPrototypeOf,
98
+ v = d && d(d(values([])));
99
+ v && v !== r && n.call(v, a) && (p = v);
100
+ var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
101
+ function defineIteratorMethods(t) {
102
+ ["next", "throw", "return"].forEach(function (e) {
103
+ define(t, e, function (t) {
104
+ return this._invoke(e, t);
105
+ });
106
+ });
107
+ }
108
+ function AsyncIterator(t, e) {
109
+ function invoke(r, o, i, a) {
110
+ var c = tryCatch(t[r], t, o);
111
+ if ("throw" !== c.type) {
112
+ var u = c.arg,
113
+ h = u.value;
114
+ return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
115
+ invoke("next", t, i, a);
116
+ }, function (t) {
117
+ invoke("throw", t, i, a);
118
+ }) : e.resolve(h).then(function (t) {
119
+ u.value = t, i(u);
120
+ }, function (t) {
121
+ return invoke("throw", t, i, a);
122
+ });
123
+ }
124
+ a(c.arg);
125
+ }
126
+ var r;
127
+ o(this, "_invoke", {
128
+ value: function (t, n) {
129
+ function callInvokeWithMethodAndArg() {
130
+ return new e(function (e, r) {
131
+ invoke(t, n, e, r);
132
+ });
133
+ }
134
+ return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
135
+ }
136
+ });
137
+ }
138
+ function makeInvokeMethod(e, r, n) {
139
+ var o = h;
140
+ return function (i, a) {
141
+ if (o === f) throw Error("Generator is already running");
142
+ if (o === s) {
143
+ if ("throw" === i) throw a;
144
+ return {
145
+ value: t,
146
+ done: !0
147
+ };
148
+ }
149
+ for (n.method = i, n.arg = a;;) {
150
+ var c = n.delegate;
151
+ if (c) {
152
+ var u = maybeInvokeDelegate(c, n);
153
+ if (u) {
154
+ if (u === y) continue;
155
+ return u;
156
+ }
157
+ }
158
+ if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
159
+ if (o === h) throw o = s, n.arg;
160
+ n.dispatchException(n.arg);
161
+ } else "return" === n.method && n.abrupt("return", n.arg);
162
+ o = f;
163
+ var p = tryCatch(e, r, n);
164
+ if ("normal" === p.type) {
165
+ if (o = n.done ? s : l, p.arg === y) continue;
166
+ return {
167
+ value: p.arg,
168
+ done: n.done
169
+ };
170
+ }
171
+ "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
172
+ }
173
+ };
174
+ }
175
+ function maybeInvokeDelegate(e, r) {
176
+ var n = r.method,
177
+ o = e.iterator[n];
178
+ if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
179
+ var i = tryCatch(o, e.iterator, r.arg);
180
+ if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
181
+ var a = i.arg;
182
+ return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
183
+ }
184
+ function pushTryEntry(t) {
185
+ var e = {
186
+ tryLoc: t[0]
187
+ };
188
+ 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
189
+ }
190
+ function resetTryEntry(t) {
191
+ var e = t.completion || {};
192
+ e.type = "normal", delete e.arg, t.completion = e;
193
+ }
194
+ function Context(t) {
195
+ this.tryEntries = [{
196
+ tryLoc: "root"
197
+ }], t.forEach(pushTryEntry, this), this.reset(!0);
198
+ }
199
+ function values(e) {
200
+ if (e || "" === e) {
201
+ var r = e[a];
202
+ if (r) return r.call(e);
203
+ if ("function" == typeof e.next) return e;
204
+ if (!isNaN(e.length)) {
205
+ var o = -1,
206
+ i = function next() {
207
+ for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
208
+ return next.value = t, next.done = !0, next;
209
+ };
210
+ return i.next = i;
211
+ }
212
+ }
213
+ throw new TypeError(typeof e + " is not iterable");
214
+ }
215
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
216
+ value: GeneratorFunctionPrototype,
217
+ configurable: !0
218
+ }), o(GeneratorFunctionPrototype, "constructor", {
219
+ value: GeneratorFunction,
220
+ configurable: !0
221
+ }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
222
+ var e = "function" == typeof t && t.constructor;
223
+ return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
224
+ }, e.mark = function (t) {
225
+ return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
226
+ }, e.awrap = function (t) {
227
+ return {
228
+ __await: t
229
+ };
230
+ }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
231
+ return this;
232
+ }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
233
+ void 0 === i && (i = Promise);
234
+ var a = new AsyncIterator(wrap(t, r, n, o), i);
235
+ return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
236
+ return t.done ? t.value : a.next();
237
+ });
238
+ }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
239
+ return this;
240
+ }), define(g, "toString", function () {
241
+ return "[object Generator]";
242
+ }), e.keys = function (t) {
243
+ var e = Object(t),
244
+ r = [];
245
+ for (var n in e) r.push(n);
246
+ return r.reverse(), function next() {
247
+ for (; r.length;) {
248
+ var t = r.pop();
249
+ if (t in e) return next.value = t, next.done = !1, next;
250
+ }
251
+ return next.done = !0, next;
252
+ };
253
+ }, e.values = values, Context.prototype = {
254
+ constructor: Context,
255
+ reset: function (e) {
256
+ if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
257
+ },
258
+ stop: function () {
259
+ this.done = !0;
260
+ var t = this.tryEntries[0].completion;
261
+ if ("throw" === t.type) throw t.arg;
262
+ return this.rval;
263
+ },
264
+ dispatchException: function (e) {
265
+ if (this.done) throw e;
266
+ var r = this;
267
+ function handle(n, o) {
268
+ return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
269
+ }
270
+ for (var o = this.tryEntries.length - 1; o >= 0; --o) {
271
+ var i = this.tryEntries[o],
272
+ a = i.completion;
273
+ if ("root" === i.tryLoc) return handle("end");
274
+ if (i.tryLoc <= this.prev) {
275
+ var c = n.call(i, "catchLoc"),
276
+ u = n.call(i, "finallyLoc");
277
+ if (c && u) {
278
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
279
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
280
+ } else if (c) {
281
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
282
+ } else {
283
+ if (!u) throw Error("try statement without catch or finally");
284
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
285
+ }
286
+ }
287
+ }
288
+ },
289
+ abrupt: function (t, e) {
290
+ for (var r = this.tryEntries.length - 1; r >= 0; --r) {
291
+ var o = this.tryEntries[r];
292
+ if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
293
+ var i = o;
294
+ break;
295
+ }
296
+ }
297
+ i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
298
+ var a = i ? i.completion : {};
299
+ return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
300
+ },
301
+ complete: function (t, e) {
302
+ if ("throw" === t.type) throw t.arg;
303
+ return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
304
+ },
305
+ finish: function (t) {
306
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
307
+ var r = this.tryEntries[e];
308
+ if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
309
+ }
310
+ },
311
+ catch: function (t) {
312
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
313
+ var r = this.tryEntries[e];
314
+ if (r.tryLoc === t) {
315
+ var n = r.completion;
316
+ if ("throw" === n.type) {
317
+ var o = n.arg;
318
+ resetTryEntry(r);
319
+ }
320
+ return o;
321
+ }
322
+ }
323
+ throw Error("illegal catch attempt");
324
+ },
325
+ delegateYield: function (e, r, n) {
326
+ return this.delegate = {
327
+ iterator: values(e),
328
+ resultName: r,
329
+ nextLoc: n
330
+ }, "next" === this.method && (this.arg = t), y;
331
+ }
332
+ }, e;
333
+ }
334
+ function _setPrototypeOf(t, e) {
335
+ return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
336
+ return t.__proto__ = e, t;
337
+ }, _setPrototypeOf(t, e);
338
+ }
339
+
340
+ var img = "data:image/svg+xml,%3csvg enable-background='new 0 0 2447.6 2452.5' viewBox='0 0 2447.6 2452.5' xmlns='http://www.w3.org/2000/svg'%3e%3cg clip-rule='evenodd' fill-rule='evenodd'%3e%3cpath d='m897.4 0c-135.3.1-244.8 109.9-244.7 245.2-.1 135.3 109.5 245.1 244.8 245.2h244.8v-245.1c.1-135.3-109.5-245.1-244.9-245.3.1 0 .1 0 0 0m0 654h-652.6c-135.3.1-244.9 109.9-244.8 245.2-.2 135.3 109.4 245.1 244.7 245.3h652.7c135.3-.1 244.9-109.9 244.8-245.2.1-135.4-109.5-245.2-244.8-245.3z' fill='%2336c5f0'/%3e%3cpath d='m2447.6 899.2c.1-135.3-109.5-245.1-244.8-245.2-135.3.1-244.9 109.9-244.8 245.2v245.3h244.8c135.3-.1 244.9-109.9 244.8-245.3zm-652.7 0v-654c.1-135.2-109.4-245-244.7-245.2-135.3.1-244.9 109.9-244.8 245.2v654c-.2 135.3 109.4 245.1 244.7 245.3 135.3-.1 244.9-109.9 244.8-245.3z' fill='%232eb67d'/%3e%3cpath d='m1550.1 2452.5c135.3-.1 244.9-109.9 244.8-245.2.1-135.3-109.5-245.1-244.8-245.2h-244.8v245.2c-.1 135.2 109.5 245 244.8 245.2zm0-654.1h652.7c135.3-.1 244.9-109.9 244.8-245.2.2-135.3-109.4-245.1-244.7-245.3h-652.7c-135.3.1-244.9 109.9-244.8 245.2-.1 135.4 109.4 245.2 244.7 245.3z' fill='%23ecb22e'/%3e%3cpath d='m0 1553.2c-.1 135.3 109.5 245.1 244.8 245.2 135.3-.1 244.9-109.9 244.8-245.2v-245.2h-244.8c-135.3.1-244.9 109.9-244.8 245.2zm652.7 0v654c-.2 135.3 109.4 245.1 244.7 245.3 135.3-.1 244.9-109.9 244.8-245.2v-653.9c.2-135.3-109.4-245.1-244.7-245.3-135.4 0-244.9 109.8-244.8 245.1 0 0 0 .1 0 0' fill='%23e01e5a'/%3e%3c/g%3e%3c/svg%3e";
341
+
342
+ var SEND_MESSAGE_TO_CHANNEL_SCHEMA = /*#__PURE__*/z.object({
343
+ channelId: /*#__PURE__*/z.string().trim().min(1, 'Required'),
344
+ message: /*#__PURE__*/z.string().describe("type::" + IntegrationFieldTypeEnum.LONG_TEXT)
345
+ });
346
+ var CREATE_NEW_CHANNEL_SCHEMA = /*#__PURE__*/z.object({
347
+ channelName: /*#__PURE__*/z.string().trim().min(1, 'Required'),
348
+ isPrivate: /*#__PURE__*/z["enum"](['true', 'false'])
349
+ });
350
+ var CREATE_NEW_CHANNEL_OUTPUT_SCHEMA = /*#__PURE__*/z.object({
351
+ channelId: /*#__PURE__*/z.string().nullable()
352
+ });
353
+ var INVITE_TO_CHANNEL_SCHEMA = /*#__PURE__*/z.object({
354
+ channelId: /*#__PURE__*/z.string().trim().min(1, 'Required'),
355
+ users: /*#__PURE__*/z.array(/*#__PURE__*/z.string())
356
+ });
357
+
358
+ var CREATE_NEW_CHANNEL = function CREATE_NEW_CHANNEL(_ref) {
359
+ var name = _ref.name,
360
+ makeClient = _ref.makeClient;
361
+ return {
362
+ integrationName: name,
363
+ executor: function () {
364
+ var _executor = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref2) {
365
+ var _createdChannel$chann;
366
+ var data, connectionId, client, channelName, isPrivate, createdChannel, channelId;
367
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
368
+ while (1) switch (_context.prev = _context.next) {
369
+ case 0:
370
+ data = _ref2.data, connectionId = _ref2.ctx.connectionId;
371
+ _context.next = 3;
372
+ return makeClient({
373
+ connectionId: connectionId
374
+ });
375
+ case 3:
376
+ client = _context.sent;
377
+ channelName = data.channelName, isPrivate = data.isPrivate;
378
+ _context.next = 7;
379
+ return client.createChannel({
380
+ name: channelName == null ? void 0 : channelName.replaceAll(' ', '-').toLowerCase(),
381
+ isPrivate: isPrivate === 'true' ? true : false
382
+ });
383
+ case 7:
384
+ createdChannel = _context.sent;
385
+ if (!(createdChannel != null && createdChannel.error)) {
386
+ _context.next = 10;
387
+ break;
388
+ }
389
+ throw new Error('Error creating channel');
390
+ case 10:
391
+ channelId = createdChannel == null || (_createdChannel$chann = createdChannel.channel) == null ? void 0 : _createdChannel$chann.id;
392
+ return _context.abrupt("return", {
393
+ channelId: channelId
394
+ });
395
+ case 12:
396
+ case "end":
397
+ return _context.stop();
398
+ }
399
+ }, _callee);
400
+ }));
401
+ function executor(_x) {
402
+ return _executor.apply(this, arguments);
403
+ }
404
+ return executor;
405
+ }(),
406
+ icon: {
407
+ alt: 'Slack Icon',
408
+ icon: img
409
+ },
410
+ type: 'CREATE_NEW_CHANNEL',
411
+ schema: CREATE_NEW_CHANNEL_SCHEMA,
412
+ description: 'Create a new channel',
413
+ label: 'Create New Channel',
414
+ outputSchema: CREATE_NEW_CHANNEL_OUTPUT_SCHEMA
415
+ };
416
+ };
417
+
418
+ var INVITE_TO_CHANNEL = function INVITE_TO_CHANNEL(_ref) {
419
+ var name = _ref.name,
420
+ makeClient = _ref.makeClient;
421
+ return {
422
+ integrationName: name,
423
+ executor: function () {
424
+ var _executor = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref2) {
425
+ var data, connectionId, client, channelId, users;
426
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
427
+ while (1) switch (_context.prev = _context.next) {
428
+ case 0:
429
+ data = _ref2.data, connectionId = _ref2.ctx.connectionId;
430
+ _context.next = 3;
431
+ return makeClient({
432
+ connectionId: connectionId
433
+ });
434
+ case 3:
435
+ client = _context.sent;
436
+ channelId = data.channelId, users = data.users;
437
+ if (users != null && users.length) {
438
+ _context.next = 7;
439
+ break;
440
+ }
441
+ return _context.abrupt("return", {
442
+ success: false,
443
+ message: 'No users to invite'
444
+ });
445
+ case 7:
446
+ _context.next = 9;
447
+ return client.invite({
448
+ channelId: channelId,
449
+ users: users
450
+ });
451
+ case 9:
452
+ return _context.abrupt("return", {
453
+ success: true,
454
+ message: 'Users invited'
455
+ });
456
+ case 10:
457
+ case "end":
458
+ return _context.stop();
459
+ }
460
+ }, _callee);
461
+ }));
462
+ function executor(_x) {
463
+ return _executor.apply(this, arguments);
464
+ }
465
+ return executor;
466
+ }(),
467
+ type: 'INVITE_TO_CHANNEL',
468
+ description: 'Invite users to a channel',
469
+ label: 'Invite to Channel',
470
+ schema: INVITE_TO_CHANNEL_SCHEMA,
471
+ icon: {
472
+ icon: img,
473
+ alt: 'Slack Icon'
474
+ },
475
+ getSchemaOptions: function getSchemaOptions(_ref3) {
476
+ return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
477
+ var ctx, usersSet, client, users, channels, schemaOptions, channelOptions;
478
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
479
+ while (1) switch (_context2.prev = _context2.next) {
480
+ case 0:
481
+ ctx = _ref3.ctx;
482
+ usersSet = new Set();
483
+ _context2.next = 4;
484
+ return makeClient({
485
+ connectionId: ctx.connectionId
486
+ });
487
+ case 4:
488
+ client = _context2.sent;
489
+ _context2.next = 7;
490
+ return client.getActiveUsers();
491
+ case 7:
492
+ users = _context2.sent;
493
+ _context2.next = 10;
494
+ return client.getAllChannels();
495
+ case 10:
496
+ channels = _context2.sent;
497
+ users == null || users.forEach(function (user) {
498
+ usersSet.add(user);
499
+ });
500
+ schemaOptions = Array.from(usersSet).map(function (user) {
501
+ return {
502
+ label: user.name,
503
+ value: user.id
504
+ };
505
+ });
506
+ channelOptions = channels == null ? void 0 : channels.map(function (channel) {
507
+ var _channel$name, _channel$id;
508
+ return {
509
+ label: (_channel$name = channel.name) != null ? _channel$name : '',
510
+ value: (_channel$id = channel.id) != null ? _channel$id : ''
511
+ };
512
+ });
513
+ return _context2.abrupt("return", {
514
+ users: {
515
+ options: schemaOptions
516
+ },
517
+ channelId: {
518
+ options: channelOptions != null ? channelOptions : []
519
+ }
520
+ });
521
+ case 15:
522
+ case "end":
523
+ return _context2.stop();
524
+ }
525
+ }, _callee2);
526
+ }))();
527
+ }
528
+ };
529
+ };
530
+
531
+ var SEND_MESSAGE_TO_CHANNEL = function SEND_MESSAGE_TO_CHANNEL(_ref) {
532
+ var name = _ref.name,
533
+ makeClient = _ref.makeClient;
534
+ return {
535
+ integrationName: name,
536
+ executor: function () {
537
+ var _executor = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref2) {
538
+ var data, connectionId, client, channelId, message;
539
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
540
+ while (1) switch (_context.prev = _context.next) {
541
+ case 0:
542
+ data = _ref2.data, connectionId = _ref2.ctx.connectionId;
543
+ _context.next = 3;
544
+ return makeClient({
545
+ connectionId: connectionId
546
+ });
547
+ case 3:
548
+ client = _context.sent;
549
+ channelId = data.channelId, message = data.message;
550
+ _context.prev = 5;
551
+ _context.next = 8;
552
+ return client.joinChannel({
553
+ channelId: channelId
554
+ });
555
+ case 8:
556
+ _context.next = 13;
557
+ break;
558
+ case 10:
559
+ _context.prev = 10;
560
+ _context.t0 = _context["catch"](5);
561
+ console.log(_context.t0);
562
+ // fail silently if the channel is already joined
563
+ case 13:
564
+ _context.next = 15;
565
+ return client.sendMessage({
566
+ channelId: channelId,
567
+ message: message
568
+ });
569
+ case 15:
570
+ return _context.abrupt("return", {
571
+ success: true,
572
+ message: 'Message sent'
573
+ });
574
+ case 16:
575
+ case "end":
576
+ return _context.stop();
577
+ }
578
+ }, _callee, null, [[5, 10]]);
579
+ }));
580
+ function executor(_x) {
581
+ return _executor.apply(this, arguments);
582
+ }
583
+ return executor;
584
+ }(),
585
+ type: 'SEND_MESSAGE_TO_CHANNEL',
586
+ schema: SEND_MESSAGE_TO_CHANNEL_SCHEMA,
587
+ description: 'Send a message to a channel',
588
+ icon: {
589
+ icon: img,
590
+ alt: 'Slack Icon'
591
+ },
592
+ getSchemaOptions: function getSchemaOptions(_ref3) {
593
+ return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
594
+ var ctx, channelsSet, client, channels, schemaOptions;
595
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
596
+ while (1) switch (_context2.prev = _context2.next) {
597
+ case 0:
598
+ ctx = _ref3.ctx;
599
+ channelsSet = new Set();
600
+ _context2.next = 4;
601
+ return makeClient({
602
+ connectionId: ctx.connectionId
603
+ });
604
+ case 4:
605
+ client = _context2.sent;
606
+ _context2.next = 7;
607
+ return client.getAllChannels();
608
+ case 7:
609
+ channels = _context2.sent;
610
+ channels == null || channels.forEach(function (channel) {
611
+ channelsSet.add(channel);
612
+ });
613
+ schemaOptions = Array.from(channelsSet).map(function (channel) {
614
+ return {
615
+ label: "# " + channel.name,
616
+ value: channel.id
617
+ };
618
+ });
619
+ return _context2.abrupt("return", {
620
+ channelId: {
621
+ options: schemaOptions
622
+ }
623
+ });
624
+ case 11:
625
+ case "end":
626
+ return _context2.stop();
627
+ }
628
+ }, _callee2);
629
+ }))();
630
+ },
631
+ label: 'Send Message to Channel'
632
+ };
633
+ };
634
+
635
+ var SlackClient = /*#__PURE__*/function () {
636
+ function SlackClient(_ref) {
637
+ var token = _ref.token;
638
+ // @ts-ignore
639
+ this.token = void 0;
640
+ this.client = void 0;
641
+ this.token = token;
642
+ this.client = new WebClient(token);
643
+ }
644
+ var _proto = SlackClient.prototype;
645
+ _proto.getAllChannels = /*#__PURE__*/function () {
646
+ var _getAllChannels = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
647
+ var _response$channels;
648
+ var response;
649
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
650
+ while (1) switch (_context.prev = _context.next) {
651
+ case 0:
652
+ _context.next = 2;
653
+ return this.client.conversations.list({
654
+ types: 'private_channel,public_channel',
655
+ limit: 999
656
+ });
657
+ case 2:
658
+ response = _context.sent;
659
+ return _context.abrupt("return", (_response$channels = response.channels) == null ? void 0 : _response$channels.filter(function (_ref2) {
660
+ var is_archived = _ref2.is_archived;
661
+ return !is_archived;
662
+ }));
663
+ case 4:
664
+ case "end":
665
+ return _context.stop();
666
+ }
667
+ }, _callee, this);
668
+ }));
669
+ function getAllChannels() {
670
+ return _getAllChannels.apply(this, arguments);
671
+ }
672
+ return getAllChannels;
673
+ }();
674
+ _proto.channelByName = /*#__PURE__*/function () {
675
+ var _channelByName = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(channelName) {
676
+ var channels;
677
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
678
+ while (1) switch (_context2.prev = _context2.next) {
679
+ case 0:
680
+ _context2.next = 2;
681
+ return this.getAllChannels();
682
+ case 2:
683
+ channels = _context2.sent;
684
+ return _context2.abrupt("return", channels == null ? void 0 : channels.find(function (_ref3) {
685
+ var name = _ref3.name;
686
+ return name === channelName;
687
+ }));
688
+ case 4:
689
+ case "end":
690
+ return _context2.stop();
691
+ }
692
+ }, _callee2, this);
693
+ }));
694
+ function channelByName(_x) {
695
+ return _channelByName.apply(this, arguments);
696
+ }
697
+ return channelByName;
698
+ }();
699
+ _proto.getActiveUsers = /*#__PURE__*/function () {
700
+ var _getActiveUsers = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
701
+ var _yield$this$client$us, members, activeMembers;
702
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
703
+ while (1) switch (_context3.prev = _context3.next) {
704
+ case 0:
705
+ _context3.next = 2;
706
+ return this.client.users.list({});
707
+ case 2:
708
+ _yield$this$client$us = _context3.sent;
709
+ members = _yield$this$client$us.members;
710
+ activeMembers = members == null ? void 0 : members.filter(function (_ref4) {
711
+ var deleted = _ref4.deleted,
712
+ is_bot = _ref4.is_bot;
713
+ return !deleted && !is_bot;
714
+ });
715
+ return _context3.abrupt("return", activeMembers);
716
+ case 6:
717
+ case "end":
718
+ return _context3.stop();
719
+ }
720
+ }, _callee3, this);
721
+ }));
722
+ function getActiveUsers() {
723
+ return _getActiveUsers.apply(this, arguments);
724
+ }
725
+ return getActiveUsers;
726
+ }();
727
+ _proto.userIdsByNames = /*#__PURE__*/function () {
728
+ var _userIdsByNames = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(names) {
729
+ var _users$filter;
730
+ var users, peopleIds;
731
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
732
+ while (1) switch (_context4.prev = _context4.next) {
733
+ case 0:
734
+ _context4.next = 2;
735
+ return this.getActiveUsers();
736
+ case 2:
737
+ users = _context4.sent;
738
+ peopleIds = users == null || (_users$filter = users.filter(function (_ref5) {
739
+ var name = _ref5.name;
740
+ if (name) {
741
+ var nameMatch = names.find(function (n) {
742
+ return n.toLowerCase().includes(name.toLowerCase()) || name.toLowerCase().includes(n.toLowerCase());
743
+ });
744
+ return !!nameMatch;
745
+ }
746
+ return false;
747
+ })) == null ? void 0 : _users$filter.map(function (_ref6) {
748
+ var id = _ref6.id;
749
+ return id;
750
+ });
751
+ return _context4.abrupt("return", peopleIds);
752
+ case 5:
753
+ case "end":
754
+ return _context4.stop();
755
+ }
756
+ }, _callee4, this);
757
+ }));
758
+ function userIdsByNames(_x2) {
759
+ return _userIdsByNames.apply(this, arguments);
760
+ }
761
+ return userIdsByNames;
762
+ }();
763
+ _proto.sendMessage = /*#__PURE__*/function () {
764
+ var _sendMessage = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(_ref7) {
765
+ var channelId, message;
766
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
767
+ while (1) switch (_context5.prev = _context5.next) {
768
+ case 0:
769
+ channelId = _ref7.channelId, message = _ref7.message;
770
+ _context5.next = 3;
771
+ return this.client.chat.postMessage({
772
+ channel: channelId,
773
+ text: message
774
+ });
775
+ case 3:
776
+ return _context5.abrupt("return", _context5.sent);
777
+ case 4:
778
+ case "end":
779
+ return _context5.stop();
780
+ }
781
+ }, _callee5, this);
782
+ }));
783
+ function sendMessage(_x3) {
784
+ return _sendMessage.apply(this, arguments);
785
+ }
786
+ return sendMessage;
787
+ }();
788
+ _proto.invite = /*#__PURE__*/function () {
789
+ var _invite = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(_ref8) {
790
+ var channelId, users;
791
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
792
+ while (1) switch (_context6.prev = _context6.next) {
793
+ case 0:
794
+ channelId = _ref8.channelId, users = _ref8.users;
795
+ _context6.next = 3;
796
+ return this.client.conversations.invite({
797
+ channel: channelId,
798
+ users: users.join(',')
799
+ });
800
+ case 3:
801
+ return _context6.abrupt("return", _context6.sent);
802
+ case 4:
803
+ case "end":
804
+ return _context6.stop();
805
+ }
806
+ }, _callee6, this);
807
+ }));
808
+ function invite(_x4) {
809
+ return _invite.apply(this, arguments);
810
+ }
811
+ return invite;
812
+ }();
813
+ _proto.createChannel = /*#__PURE__*/function () {
814
+ var _createChannel = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(_ref9) {
815
+ var name, isPrivate, result;
816
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
817
+ while (1) switch (_context7.prev = _context7.next) {
818
+ case 0:
819
+ name = _ref9.name, isPrivate = _ref9.isPrivate;
820
+ _context7.next = 3;
821
+ return this.client.conversations.create({
822
+ name: name,
823
+ is_private: isPrivate
824
+ });
825
+ case 3:
826
+ result = _context7.sent;
827
+ return _context7.abrupt("return", result);
828
+ case 5:
829
+ case "end":
830
+ return _context7.stop();
831
+ }
832
+ }, _callee7, this);
833
+ }));
834
+ function createChannel(_x5) {
835
+ return _createChannel.apply(this, arguments);
836
+ }
837
+ return createChannel;
838
+ }();
839
+ _proto.joinChannel = /*#__PURE__*/function () {
840
+ var _joinChannel = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(_ref10) {
841
+ var channelId;
842
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
843
+ while (1) switch (_context8.prev = _context8.next) {
844
+ case 0:
845
+ channelId = _ref10.channelId;
846
+ _context8.next = 3;
847
+ return this.client.conversations.join({
848
+ channel: channelId
849
+ });
850
+ case 3:
851
+ return _context8.abrupt("return", _context8.sent);
852
+ case 4:
853
+ case "end":
854
+ return _context8.stop();
855
+ }
856
+ }, _callee8, this);
857
+ }));
858
+ function joinChannel(_x6) {
859
+ return _joinChannel.apply(this, arguments);
860
+ }
861
+ return joinChannel;
862
+ }();
863
+ return SlackClient;
864
+ }();
865
+
866
+ var SLACK_INTEGRATION_NAME = 'SLACK';
867
+
868
+ var SlackIntegration = /*#__PURE__*/function (_Integration) {
869
+ function SlackIntegration(_ref) {
870
+ var _this;
871
+ var config = _ref.config;
872
+ config.authType = "OAUTH";
873
+ _this = _Integration.call(this, {
874
+ config: config,
875
+ name: SLACK_INTEGRATION_NAME,
876
+ logoUrl: img
877
+ }) || this;
878
+ _this.config = void 0;
879
+ _this.availableScopes = [{
880
+ key: 'channels:manage',
881
+ description: 'Manage channels'
882
+ }, {
883
+ key: 'users:read',
884
+ description: 'Read user information'
885
+ }, {
886
+ key: 'channels:write.invites',
887
+ description: 'Invite users to channels'
888
+ }, {
889
+ key: 'chat:write',
890
+ description: 'Send messages'
891
+ }, {
892
+ key: 'channels:read',
893
+ description: 'View channels'
894
+ }, {
895
+ key: 'groups:read',
896
+ description: 'View private channels'
897
+ }, {
898
+ key: 'channels:join',
899
+ description: 'Join channels'
900
+ }, {
901
+ key: 'groups:write.invites',
902
+ description: 'Invite users to private channels'
903
+ }, {
904
+ key: 'groups:write',
905
+ description: 'Manage private channels'
906
+ }];
907
+ _this.makeClient = /*#__PURE__*/function () {
908
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref2) {
909
+ var _this$dataLayer;
910
+ var connectionId, authenticator, connection, token;
911
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
912
+ while (1) switch (_context.prev = _context.next) {
913
+ case 0:
914
+ connectionId = _ref2.connectionId;
915
+ authenticator = _this.getAuthenticator();
916
+ _context.next = 4;
917
+ return (_this$dataLayer = _this.dataLayer) == null ? void 0 : _this$dataLayer.getConnection({
918
+ connectionId: connectionId,
919
+ name: _this.name
920
+ });
921
+ case 4:
922
+ connection = _context.sent;
923
+ if (connection) {
924
+ _context.next = 7;
925
+ break;
926
+ }
927
+ throw new Error('No connection found');
928
+ case 7:
929
+ _context.next = 9;
930
+ return authenticator.getAuthToken({
931
+ k_id: connection == null ? void 0 : connection.id
932
+ });
933
+ case 9:
934
+ token = _context.sent;
935
+ return _context.abrupt("return", new SlackClient({
936
+ token: token.accessToken
937
+ }));
938
+ case 11:
939
+ case "end":
940
+ return _context.stop();
941
+ }
942
+ }, _callee);
943
+ }));
944
+ return function (_x) {
945
+ return _ref3.apply(this, arguments);
946
+ };
947
+ }();
948
+ _this.config = config;
949
+ return _this;
950
+ }
951
+ _inheritsLoose(SlackIntegration, _Integration);
952
+ var _proto = SlackIntegration.prototype;
953
+ _proto.registerApis = function registerApis() {
954
+ this.apis = {
955
+ SEND_MESSAGE_TO_CHANNEL: SEND_MESSAGE_TO_CHANNEL({
956
+ dataAccess: this == null ? void 0 : this.dataLayer,
957
+ name: this.name,
958
+ makeClient: this.makeClient
959
+ }),
960
+ CREATE_NEW_CHANNEL: CREATE_NEW_CHANNEL({
961
+ dataAccess: this == null ? void 0 : this.dataLayer,
962
+ name: this.name,
963
+ makeClient: this.makeClient
964
+ }),
965
+ INVITE_TO_CHANNEL: INVITE_TO_CHANNEL({
966
+ dataAccess: this == null ? void 0 : this.dataLayer,
967
+ name: this.name,
968
+ makeClient: this.makeClient
969
+ })
970
+ };
971
+ return this.apis;
972
+ };
973
+ _proto.onConnectionCreated = /*#__PURE__*/function () {
974
+ var _onConnectionCreated = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref4) {
975
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
976
+ while (1) switch (_context2.prev = _context2.next) {
977
+ case 0:
978
+ case 1:
979
+ case "end":
980
+ return _context2.stop();
981
+ }
982
+ }, _callee2);
983
+ }));
984
+ function onConnectionCreated(_x2) {
985
+ return _onConnectionCreated.apply(this, arguments);
986
+ }
987
+ return onConnectionCreated;
988
+ }();
989
+ _proto.onDisconnect = /*#__PURE__*/function () {
990
+ var _onDisconnect = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(_ref5) {
991
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
992
+ while (1) switch (_context3.prev = _context3.next) {
993
+ case 0:
994
+ case 1:
995
+ case "end":
996
+ return _context3.stop();
997
+ }
998
+ }, _callee3);
999
+ }));
1000
+ function onDisconnect(_x3) {
1001
+ return _onDisconnect.apply(this, arguments);
1002
+ }
1003
+ return onDisconnect;
1004
+ }();
1005
+ _proto.getAuthenticator = function getAuthenticator() {
1006
+ var _this$availableScopes,
1007
+ _this2 = this;
1008
+ var isScopesDefined = this.config.SCOPES && this.config.SCOPES.length > 0; // TODO: remove this once we a document, and we can define the scopes
1009
+ var fallbackScopes = ((_this$availableScopes = this.availableScopes) == null ? void 0 : _this$availableScopes.map(function (scope) {
1010
+ return scope.key;
1011
+ })) || [];
1012
+ return new IntegrationAuth({
1013
+ dataAccess: this.dataLayer,
1014
+ onConnectionCreated: function onConnectionCreated(connection) {
1015
+ return _this2.onConnectionCreated({
1016
+ connection: connection
1017
+ });
1018
+ },
1019
+ config: {
1020
+ CLIENT_ID: this.config.CLIENT_ID,
1021
+ CLIENT_SECRET: this.config.CLIENT_SECRET,
1022
+ REDIRECT_URI: this.config.REDIRECT_URI || this.corePresets.redirectURI,
1023
+ AUTH_TYPE: this.config.authType,
1024
+ SERVER: 'https://slack.com',
1025
+ DISCOVERY_ENDPOINT: '/.well-known/openid-configuration',
1026
+ AUTHORIZATION_ENDPOINT: '/oauth/v2/authorize',
1027
+ TOKEN_ENDPOINT: '/api/oauth.v2.access',
1028
+ REVOCATION_ENDPOINT: '/api/auth.revoke',
1029
+ SCOPES: isScopesDefined ? this.config.SCOPES : fallbackScopes,
1030
+ INTEGRATION_NAME: this.name,
1031
+ EXTRA_AUTH_PARAMS: {
1032
+ access_type: 'offline',
1033
+ prompt: 'consent'
1034
+ }
1035
+ }
1036
+ });
1037
+ };
1038
+ return SlackIntegration;
1039
+ }(Integration);
1040
+
1041
+ export { SlackIntegration };
1042
+ //# sourceMappingURL=slack.esm.js.map