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 fs = require("fs");
|
14 | var path = require("path");
|
15 | var request = require("request");
|
16 | var channel_1 = require("./channel");
|
17 | var logger_1 = require("./logger");
|
18 | var recime_keyvalue_store_1 = require("recime-keyvalue-store");
|
19 | var Slack = (function (_super) {
|
20 | __extends(Slack, _super);
|
21 | function Slack(bot) {
|
22 | return _super.call(this, { bot: bot, log: new logger_1.SlackLogger(bot) }) || this;
|
23 | }
|
24 | Slack.prototype.preprocessParams = function (params) {
|
25 | Object.keys(params).forEach(function (name) {
|
26 | var param = params[name];
|
27 | if (param &&
|
28 | typeof param === 'object') {
|
29 | params[name] = JSON.stringify(param);
|
30 | }
|
31 | });
|
32 | return params;
|
33 | };
|
34 | Slack.prototype.authorize = function (code) {
|
35 | var _this = this;
|
36 | return new Promise(function (resolve, reject) {
|
37 | var payload = {
|
38 | url: "https://slack.com/api/oauth.access",
|
39 | method: "POST",
|
40 | form: _this.preprocessParams({
|
41 | code: code,
|
42 | client_id: process.env.RECIME_SLACK_CLIENT_ID || _this.context.bot.config.RECIME_SLACK_CLIENT_ID,
|
43 | client_secret: process.env.RECIME_SLACK_CLIENT_SECRET || _this.context.bot.config.RECIME_SLACK_CLIENT_SECRET,
|
44 | redirect_uri: process.env.RECIME_SLACK_REDIRECT_URI || _this.context.bot.config.RECIME_SLACK_REDIRECT_URI
|
45 | })
|
46 | };
|
47 | request(payload, function (err, response, body) {
|
48 | var result = JSON.parse(body);
|
49 | if (result.bot) {
|
50 | var uid = _this.context.bot.id + "-" + result.team_id;
|
51 | var db = recime_keyvalue_store_1.default.init(_this.context.bot.id);
|
52 | db.set(uid, result.bot.bot_access_token).then(function (_) {
|
53 | resolve({
|
54 | success: true,
|
55 | data: result
|
56 | });
|
57 | });
|
58 | }
|
59 | else {
|
60 | resolve(result);
|
61 | }
|
62 | });
|
63 | });
|
64 | };
|
65 | Slack.prototype.getUser = function (data) {
|
66 | var _this = this;
|
67 | var user = data.event.user;
|
68 | return new Promise(function (resolve, reject) {
|
69 | var uid = _this.context.bot.id + "-" + data.team_id;
|
70 | var db = recime_keyvalue_store_1.default.init(_this.context.bot.id);
|
71 | db.get(uid).then(function (token) {
|
72 | var methodName = "users.info";
|
73 | var payload = {
|
74 | url: "https://slack.com/api/" + methodName,
|
75 | method: "POST",
|
76 | form: _this.preprocessParams({
|
77 | token: token || process.env.RECIME_SLACK_ACCESS_TOKEN || _this.context.bot.config.RECIME_SLACK_ACCESS_TOKEN,
|
78 | user: user
|
79 | })
|
80 | };
|
81 | request(payload, function (err, response, body) {
|
82 | var result = JSON.parse(body);
|
83 | if (result.ok)
|
84 | resolve(result.user);
|
85 | else
|
86 | resolve(user);
|
87 | });
|
88 | });
|
89 | });
|
90 | };
|
91 | Slack.prototype.postMessage = function (body, data) {
|
92 | var _this = this;
|
93 | var channel = body.event.channel;
|
94 | var methodName = "chat.postMessage";
|
95 | return new Promise(function (resolve, reject) {
|
96 | var uid = _this.context.bot.id + "-" + body.team_id;
|
97 | var db = recime_keyvalue_store_1.default.init(_this.context.bot.id);
|
98 | db.get(uid).then(function (token) {
|
99 | var params = {
|
100 | token: token || process.env.RECIME_SLACK_ACCESS_TOKEN || _this.context.bot.config.RECIME_SLACK_ACCESS_TOKEN,
|
101 | text: data.text || "_param.text_ is undefined",
|
102 | channel: channel,
|
103 | link_names: data.link_names,
|
104 | attachments: data.attachments,
|
105 | username: data.username,
|
106 | as_user: data.as_user,
|
107 | icon_url: data.icon_url,
|
108 | icon_emoji: data.icon_emoji,
|
109 | parse: data.parse,
|
110 | unfurl_links: data.unfurl_links,
|
111 | unfurl_media: data.unfurl_media,
|
112 | thread_ts: data.thread_ts,
|
113 | reply_broadcast: data.reply_broadcast
|
114 | };
|
115 | var payload = {
|
116 | url: "https://slack.com/api/" + methodName,
|
117 | method: "POST",
|
118 | form: _this.preprocessParams(params)
|
119 | };
|
120 | request(payload, function (err, response, body) {
|
121 | if (err) {
|
122 | resolve(err);
|
123 | return;
|
124 | }
|
125 | _this.context.log.outgoing(payload, body);
|
126 | resolve(body);
|
127 | });
|
128 | });
|
129 | });
|
130 | };
|
131 | Slack.prototype.execute = function (body, parameters, handler) {
|
132 | var _this = this;
|
133 | return new Promise(function (resolve, reject) {
|
134 | if (body.event.subtype === 'bot_message') {
|
135 | resolve({
|
136 | success: true
|
137 | });
|
138 | return;
|
139 | }
|
140 | _this.context.log.incoming(body);
|
141 | _this.getUser(body).then(function (user) {
|
142 | body.event.user = user;
|
143 | handler(_this.context.bot, body, "slack", function (data) {
|
144 | if (data && data.text) {
|
145 | _this.postMessage(body, data).then(function (result) {
|
146 | resolve(result);
|
147 | });
|
148 | }
|
149 | else {
|
150 | resolve();
|
151 | }
|
152 | }, function (err) {
|
153 | reject(err);
|
154 | });
|
155 | });
|
156 | });
|
157 | };
|
158 | Slack.prototype.resolveSuccessDialogPath = function (dir) {
|
159 | var p = path.join(dir, "dialog.html");
|
160 | if (fs.existsSync(p)) {
|
161 | return p;
|
162 | }
|
163 | return __dirname + "/../slack-oauth-dialog.html";
|
164 | };
|
165 | Slack.prototype.verifyToken = function (body) {
|
166 | return new Promise(function (resolve, reject) {
|
167 | if (body.type === "url_verification") {
|
168 | resolve({
|
169 | challenge: body.challenge
|
170 | });
|
171 | }
|
172 | else {
|
173 | reject("Invalid Access.");
|
174 | }
|
175 | });
|
176 | };
|
177 | return Slack;
|
178 | }(channel_1.Channel));
|
179 | exports.Slack = Slack;
|