UNPKG

recime-bot-runtime

Version:

This runtime is intended to run inside a micro-service container with platform specific integration and module interpreter.

180 lines (179 loc) 7.29 kB
"use strict"; 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 fs = require("fs"); var path = require("path"); var request = require("request"); var channel_1 = require("./channel"); var logger_1 = require("./logger"); var recime_keyvalue_store_1 = require("recime-keyvalue-store"); var Slack = /** @class */ (function (_super) { __extends(Slack, _super); function Slack(bot) { return _super.call(this, { bot: bot, log: new logger_1.SlackLogger(bot) }) || this; } Slack.prototype.preprocessParams = function (params) { Object.keys(params).forEach(function (name) { var param = params[name]; if (param && typeof param === 'object') { params[name] = JSON.stringify(param); } }); return params; }; Slack.prototype.authorize = function (code) { var _this = this; return new Promise(function (resolve, reject) { var payload = { url: "https://slack.com/api/oauth.access", method: "POST", form: _this.preprocessParams({ code: code, client_id: process.env.RECIME_SLACK_CLIENT_ID || _this.context.bot.config.RECIME_SLACK_CLIENT_ID, client_secret: process.env.RECIME_SLACK_CLIENT_SECRET || _this.context.bot.config.RECIME_SLACK_CLIENT_SECRET, redirect_uri: process.env.RECIME_SLACK_REDIRECT_URI || _this.context.bot.config.RECIME_SLACK_REDIRECT_URI }) }; request(payload, function (err, response, body) { var result = JSON.parse(body); if (result.bot) { var uid = _this.context.bot.id + "-" + result.team_id; var db = recime_keyvalue_store_1.default.init(_this.context.bot.id); db.set(uid, result.bot.bot_access_token).then(function (_) { resolve({ success: true, data: result }); }); } else { resolve(result); } }); }); }; Slack.prototype.getUser = function (data) { var _this = this; var user = data.event.user; return new Promise(function (resolve, reject) { var uid = _this.context.bot.id + "-" + data.team_id; var db = recime_keyvalue_store_1.default.init(_this.context.bot.id); db.get(uid).then(function (token) { var methodName = "users.info"; var payload = { url: "https://slack.com/api/" + methodName, method: "POST", form: _this.preprocessParams({ token: token || process.env.RECIME_SLACK_ACCESS_TOKEN || _this.context.bot.config.RECIME_SLACK_ACCESS_TOKEN, user: user }) }; request(payload, function (err, response, body) { var result = JSON.parse(body); if (result.ok) resolve(result.user); else resolve(user); }); }); }); }; Slack.prototype.postMessage = function (body, data) { var _this = this; var channel = body.event.channel; var methodName = "chat.postMessage"; return new Promise(function (resolve, reject) { var uid = _this.context.bot.id + "-" + body.team_id; var db = recime_keyvalue_store_1.default.init(_this.context.bot.id); db.get(uid).then(function (token) { var params = { token: token || process.env.RECIME_SLACK_ACCESS_TOKEN || _this.context.bot.config.RECIME_SLACK_ACCESS_TOKEN, text: data.text || "_param.text_ is undefined", channel: channel, link_names: data.link_names, attachments: data.attachments, username: data.username, as_user: data.as_user, icon_url: data.icon_url, icon_emoji: data.icon_emoji, parse: data.parse, unfurl_links: data.unfurl_links, unfurl_media: data.unfurl_media, thread_ts: data.thread_ts, reply_broadcast: data.reply_broadcast }; var payload = { url: "https://slack.com/api/" + methodName, method: "POST", form: _this.preprocessParams(params) }; request(payload, function (err, response, body) { if (err) { resolve(err); return; } _this.context.log.outgoing(payload, body); resolve(body); }); }); }); }; Slack.prototype.execute = function (body, parameters, handler) { var _this = this; return new Promise(function (resolve, reject) { if (body.event.subtype === 'bot_message') { resolve({ success: true }); return; } _this.context.log.incoming(body); _this.getUser(body).then(function (user) { body.event.user = user; handler(_this.context.bot, body, "slack", function (data) { if (data && data.text) { _this.postMessage(body, data).then(function (result) { resolve(result); }); } else { resolve(); } }, function (err) { reject(err); }); }); }); }; Slack.prototype.resolveSuccessDialogPath = function (dir) { var p = path.join(dir, "dialog.html"); if (fs.existsSync(p)) { return p; } return __dirname + "/../slack-oauth-dialog.html"; }; Slack.prototype.verifyToken = function (body) { return new Promise(function (resolve, reject) { if (body.type === "url_verification") { resolve({ challenge: body.challenge }); } else { reject("Invalid Access."); } }); }; return Slack; }(channel_1.Channel)); exports.Slack = Slack;