recime-bot-runtime
Version:
This runtime is intended to run inside a micro-service container with platform specific integration and module interpreter.
85 lines (84 loc) • 4.11 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var channel_1 = require("./channel");
var logger_1 = require("./logger");
var request = require("request");
var SendBird = /** @class */ (function (_super) {
__extends(SendBird, _super);
function SendBird(bot) {
return _super.call(this, { bot: bot, log: new logger_1.SendBirdLogger(bot) }) || this;
}
SendBird.prototype.execute = function (body, parameters, handler) {
var _this = this;
// log incoming
this.context.log.incoming(body);
var handle = "/" + (process.env.RECIME_SENDBIRD_HANDLE || this.context.bot.config.RECIME_SENDBIRD_HANDLE);
var message = body.message;
return new Promise(function (resolve, reject) {
// if (message.text){
// request to this bot.
console.log('sendbird ', message);
if (message.text && message.text.startsWith(handle + " ")) {
message.text = message.text.substring(handle.length + 1);
handler(_this.context.bot, body, "sendbird", function (messages) {
var response = (messages || []).map(function (message) { return function () {
return new Promise(function (resolve, reject) {
var options = {
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",
method: 'POST',
json: true,
body: {
message: message.text || message.attachment.type,
channel_url: body.channel.channel_url,
data: JSON.stringify(message)
},
headers: {
'Content-Type': 'application/json, charset=utf8',
'Api-Token': "" + (process.env.RECIME_SENDBIRD_API_KEY || _this.context.bot.config.RECIME_SENDBIRD_API_KEY)
},
};
console.log('sendbird req', JSON.stringify(options.body));
request(options, function (err, response, result) {
_this.context.log.outgoing(options, result);
console.log('sendbird res', result);
if (err) {
console.error(err);
return reject(err);
}
return resolve();
});
});
}; });
return response
.reduce(function (a, b) {
return a.then(function (messages) {
return b().then(function (message) {
return;
});
});
}, Promise.resolve()).then(function () {
resolve();
});
}, function (err) {
reject(err);
});
}
else {
resolve();
}
// } //
});
};
return SendBird;
}(channel_1.Channel));
exports.SendBird = SendBird;