1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var channel_1 = require("./channel");
|
14 | var logger_1 = require("./logger");
|
15 | var request = require("request");
|
16 | var SendBird = (function (_super) {
|
17 | __extends(SendBird, _super);
|
18 | function SendBird(bot) {
|
19 | return _super.call(this, { bot: bot, log: new logger_1.SendBirdLogger(bot) }) || this;
|
20 | }
|
21 | SendBird.prototype.execute = function (body, parameters, handler) {
|
22 | var _this = this;
|
23 |
|
24 | this.context.log.incoming(body);
|
25 | var handle = "/" + (process.env.RECIME_SENDBIRD_HANDLE || this.context.bot.config.RECIME_SENDBIRD_HANDLE);
|
26 | var message = body.message;
|
27 | return new Promise(function (resolve, reject) {
|
28 |
|
29 |
|
30 | console.log('sendbird ', message);
|
31 | if (message.text && message.text.startsWith(handle + " ")) {
|
32 | message.text = message.text.substring(handle.length + 1);
|
33 | handler(_this.context.bot, body, "sendbird", function (messages) {
|
34 | var response = (messages || []).map(function (message) { return function () {
|
35 | return new Promise(function (resolve, reject) {
|
36 | var options = {
|
37 | url: "https://" + (process.env.RECIME_SENDBIRD_REGION_ID || _this.context.bot.config.RECIME_SENDBIRD_REGION_ID) + ".sendbird.com/v3/bots/" + _this.context.bot.id + "/send",
|
38 | method: 'POST',
|
39 | json: true,
|
40 | body: {
|
41 | message: message.text || message.attachment.type,
|
42 | channel_url: body.channel.channel_url,
|
43 | data: JSON.stringify(message)
|
44 | },
|
45 | headers: {
|
46 | 'Content-Type': 'application/json, charset=utf8',
|
47 | 'Api-Token': "" + (process.env.RECIME_SENDBIRD_API_KEY || _this.context.bot.config.RECIME_SENDBIRD_API_KEY)
|
48 | },
|
49 | };
|
50 | console.log('sendbird req', JSON.stringify(options.body));
|
51 | request(options, function (err, response, result) {
|
52 | _this.context.log.outgoing(options, result);
|
53 | console.log('sendbird res', result);
|
54 | if (err) {
|
55 | console.error(err);
|
56 | return reject(err);
|
57 | }
|
58 | return resolve();
|
59 | });
|
60 | });
|
61 | }; });
|
62 | return response
|
63 | .reduce(function (a, b) {
|
64 | return a.then(function (messages) {
|
65 | return b().then(function (message) {
|
66 | return;
|
67 | });
|
68 | });
|
69 | }, Promise.resolve()).then(function () {
|
70 | resolve();
|
71 | });
|
72 | }, function (err) {
|
73 | reject(err);
|
74 | });
|
75 | }
|
76 | else {
|
77 | resolve();
|
78 | }
|
79 |
|
80 | });
|
81 | };
|
82 | return SendBird;
|
83 | }(channel_1.Channel));
|
84 | exports.SendBird = SendBird;
|