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