slack.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. const
  2. WebSocket = require('ws'),
  3. https = require('https'),
  4. sleep = require("sleep").sleep
  5. ,SlackData = require("./slackData.js").SlackData
  6. ,SlackHistory = require("./slackHistory.js").SlackHistory
  7. ,config = require("../config.js")
  8. ,httpsRequest = require('./httpsRequest.js').httpsRequest
  9. ;
  10. const SLACK_ENDPOINT = "https://slack.com/api/"
  11. ,SLACK_HOSTNAME = "slack.com"
  12. ,SLACK_ENDPOINT_PATH = "/api/"
  13. ,GETAPI = {
  14. rtmStart: "rtm.start"
  15. ,oauth: "oauth.access"
  16. ,identityEmail: "users.identity"
  17. ,channelHistory: "channels.history"
  18. ,directHistory: "im.history"
  19. ,groupHistory: "groups.history"
  20. ,starChannel: "stars.add"
  21. ,unstarChannel: "stars.remove"
  22. ,postMsg: "chat.postMessage"
  23. ,postMeMsg: "chat.meMessage"
  24. ,editMsg: "chat.update"
  25. ,removeMsg: "chat.delete"
  26. ,postFile: "files.upload"
  27. ,setActive: "users.setActive"
  28. ,emojiList: "emoji.list"
  29. ,slashList: "commands.list"
  30. ,slashExec: "chat.command"
  31. ,addReaction: "reactions.add"
  32. ,removeReaction: "reactions.remove"
  33. ,sendAction: "chat.attachmentAction"
  34. ,read: {
  35. group: "groups.mark"
  36. ,im: "im.mark"
  37. ,group: "mpim.mark"
  38. ,channel: "channels.mark"
  39. }
  40. }
  41. ,HISTORY_LENGTH = 35
  42. ,HISTORY_MAX_AGE = 10000// * 60 * 1000
  43. ,UPDATE_LIVE = [
  44. "message"
  45. ,"pin_added"
  46. ,"pin_removed"
  47. ,"reaction_added"
  48. ,"reaction_removed"
  49. ,"star_added"
  50. ,"star_removed"
  51. ] // Message type that affect live history
  52. ;
  53. /**
  54. * @implements {ChatSystem}
  55. **/
  56. function Slack(slackToken, manager) {
  57. this.token = slackToken;
  58. this.manager = manager;
  59. this.rtm = null;
  60. this.rtmId = 1;
  61. this.data = new SlackData(this);
  62. this.history = {};
  63. this.pendingRtm = {};
  64. this.pendingMessages = [];
  65. this.pendingPing = false;
  66. this.connected = false;
  67. this.closing = false;
  68. }
  69. Slack.prototype.getId = function() {
  70. return this.data.team ? this.data.team.id : null;
  71. };
  72. Slack.prototype.onRequest = function() {
  73. if (this.connected === false) {
  74. this.connect();
  75. }
  76. };
  77. Slack.prototype.connect = function(cb) {
  78. var _this = this;
  79. this.connected = undefined;
  80. httpsRequest(SLACK_ENDPOINT +GETAPI.rtmStart +"?token=" +this.token, (status, body) => {
  81. if (!body || !body.ok) {
  82. _this.error = "Slack API error";
  83. _this.connected = false;
  84. console.error("Slack api responded " +status +" with body " +JSON.stringify(body));
  85. cb && cb(_this);
  86. } else if (status !== 200) {
  87. _this.error = body.error;
  88. _this.connected = false;
  89. console.error("Slack api responded " +status);
  90. cb && cb(_this);
  91. } else {
  92. _this.data.updateStatic({
  93. team: body["team"],
  94. users: body["users"],
  95. bots: body["bots"],
  96. self: body["self"]
  97. }, Date.now());
  98. _this.connectRtm(body.url);
  99. }
  100. });
  101. };
  102. Slack.prototype.sendCommand = function(room, cmd, arg) {
  103. httpsRequest(
  104. SLACK_ENDPOINT
  105. +GETAPI.slashExec
  106. +"?token=" +this.token
  107. +"&command=" +encodeURIComponent(cmd.name)
  108. +"&disp=" +encodeURIComponent(cmd.name)
  109. +"&channel=" +room.remoteId
  110. +"&text=" +arg);
  111. }
  112. Slack.prototype.sendTyping = function(room) {
  113. this.rtm.send('{"id":' +this.rtmId++ +',"type":"typing","channel":"' +room.remoteId +'"}');
  114. }
  115. Slack.prototype.getSlashCommands = function(cb) {
  116. httpsRequest(SLACK_ENDPOINT +GETAPI.slashList +"?token=" +this.token, (status, body) => {
  117. if (!status || !body || !body.ok)
  118. cb(null);
  119. else
  120. cb(body.commands || {});
  121. });
  122. };
  123. Slack.prototype.getEmojis = function(cb) {
  124. httpsRequest(SLACK_ENDPOINT +GETAPI.emojiList +"?token=" +this.token, (status, body) => {
  125. if (!status || !body || !body.ok)
  126. cb(null);
  127. else
  128. cb(body.emoji || {});
  129. });
  130. };
  131. Slack.prototype.poll = function(knownVersion, now) {
  132. if (this.connected) {
  133. var updatedCtx = this.data.getUpdates(knownVersion)
  134. ,updatedTyping = this.data.getWhoIsTyping(now)
  135. ,updatedLive = this.getLiveUpdates(knownVersion);
  136. if (updatedCtx || updatedLive || updatedTyping) {
  137. return {
  138. "static": updatedCtx,
  139. "live": updatedLive,
  140. "typing": updatedTyping,
  141. "v": Math.max(this.data.liveV, this.data.staticV)
  142. };
  143. }
  144. }
  145. };
  146. /** @return {Object|undefined} */
  147. Slack.prototype.getLiveUpdates = function(knownVersion) {
  148. var result = {};
  149. for (var roomId in this.history) {
  150. var history = this.history[roomId];
  151. if (history.isNew) {
  152. result[roomId] = history.toStatic(0);
  153. history.isNew = false;
  154. }
  155. else {
  156. var roomData = history.toStatic(knownVersion);
  157. if (roomData.length)
  158. result[roomId] = roomData;
  159. }
  160. }
  161. for (var roomId in result) {
  162. return result;
  163. }
  164. return undefined;
  165. };
  166. Slack.prototype.unstackPendingMessages = function() {
  167. for (var i = this.pendingMessages.length -1; i >= 0; i--) {
  168. this.onMessage(this.pendingMessages[0], Date.now());
  169. this.pendingMessages.shift();
  170. }
  171. };
  172. Slack.prototype.onMessage = function(msg, t) {
  173. if (msg["reply_to"] && this.pendingRtm[msg["reply_to"]]) {
  174. var ts = msg["ts"]
  175. ,rtmId = msg["reply_to"];
  176. msg = this.pendingRtm[rtmId];
  177. msg["ts"] = ts;
  178. delete this.pendingRtm[rtmId];
  179. }
  180. if (msg["type"] === "hello" && msg["start"] && msg["start"]["rtm_start"]) {
  181. var _this = this;
  182. _this.getEmojis((emojis) => {
  183. _this.getSlashCommands((commands) => {
  184. var msgContent = msg.start.rtm_start;
  185. msgContent.self = msg.self;
  186. msgContent.emojis = emojis;
  187. msgContent.commands = commands;
  188. _this.data.updateStatic(msgContent, Date.now());
  189. _this.unstackPendingMessages();
  190. _this.connected = true;
  191. _this.unstackPendingMessages();
  192. });
  193. });
  194. } else if (this.connected) {
  195. this.data.onMessage(msg, t);
  196. if ((msg["channel"] || msg["channel_id"] || (msg["item"] && msg["item"]["channel"])) && msg["type"] && UPDATE_LIVE.indexOf(msg["type"]) !== -1) {
  197. var channelId = this.data.team.id +'|' +(msg["channel"] || msg["channel_id"] || msg["item"]["channel"])
  198. ,channel = this.data.channels[channelId]
  199. ,histo = this.history[channelId];
  200. // FIXME remove typing for user
  201. if (!histo) {
  202. histo = this.history[channelId] = new SlackHistory(this, channel.remoteId, channelId, this.data.team.id +'|', HISTORY_LENGTH);
  203. histo.isNew = true;
  204. }
  205. var lastMsg = histo.push(msg, t);
  206. if (lastMsg)
  207. this.data.liveV = t;
  208. histo.resort();
  209. if (channel)
  210. channel.setLastMsg(lastMsg, t);
  211. }
  212. } else {
  213. this.pendingMessages.push(msg);
  214. }
  215. };
  216. /**
  217. * @param {SlackChan|SlackGroup|SlackIms} chan
  218. * @param {string} id
  219. * @param {number} ts
  220. **/
  221. Slack.prototype.markRead = function(chan, id, ts) {
  222. var apiURI;
  223. if (chan.remoteId[0] === 'C')
  224. apiURI = SLACK_ENDPOINT+GETAPI.read.channel;
  225. else if (chan.remoteId[0] === 'G')
  226. apiURI = SLACK_ENDPOINT+GETAPI.read.group;
  227. else if (chan.remoteId[0] === 'D')
  228. apiURI = SLACK_ENDPOINT+GETAPI.read.im;
  229. httpsRequest(apiURI
  230. +"?token=" +this.token
  231. +"&channel="+chan.remoteId
  232. +"&ts="+id);
  233. };
  234. Slack.prototype.connectRtm = function(url, cb) {
  235. var _this = this;
  236. this.rtmId = 1;
  237. var protocol = url.substr(0, url.indexOf('://') +3);
  238. url = url.substr(protocol.length);
  239. url = protocol +url.substr(0, url.indexOf('/'))+
  240. "/?flannel=1&token=" +this.token+
  241. "&start_args="+
  242. encodeURIComponent("?simple_latest=true&presence_sub=true&mpim_aware=false&canonical_avatars=true")
  243. this.rtm = new WebSocket(url);
  244. this.rtm.on("message", function(msg) {
  245. if (!_this.connected && cb) {
  246. cb();
  247. }
  248. try {
  249. msg = JSON.parse(msg);
  250. } catch (ex) {
  251. console.error("WTF invalid JSON ", msg);
  252. }
  253. _this.onMessage(msg, Date.now());
  254. });
  255. this.rtm.once("error", function(e) {
  256. _this.connected = false;
  257. console.error(e);
  258. _this.close();
  259. });
  260. this.rtm.once("end", function() {
  261. console.error("RTM hang up");
  262. _this.onClose();
  263. });
  264. };
  265. Slack.prototype.onClose = function() {
  266. this.manager.suicide(this);
  267. };
  268. Slack.prototype.ping = function() {
  269. httpsRequest(SLACK_ENDPOINT+GETAPI.setActive
  270. +"?token=" +this.token);
  271. };
  272. Slack.prototype.rtmPing = function() {
  273. if (this.connected) {
  274. if (this.pendingPing && this.pendingRtm[this.pendingPing]) {
  275. //FIXME timeout
  276. console.error("Ping timeout");
  277. } else {
  278. var rtmId = this.rtmId++;
  279. this.pendingRtm[rtmId] = { type: 'ping' };
  280. this.pendingPing = rtmId;
  281. this.rtm.send('{"id":' +rtmId +',"type":"ping"}');
  282. }
  283. }
  284. };
  285. Slack.prototype.close = function() {
  286. if (!this.closing) {
  287. this.closing = true;
  288. if (this.rtm)
  289. this.rtm.close();
  290. this.onClose();
  291. }
  292. };
  293. Slack.getUserId = function(code, redirectUri, cb) {
  294. Slack.getOauthToken(code, redirectUri, (token) => {
  295. if (token) {
  296. httpsRequest(SLACK_ENDPOINT+GETAPI.identityEmail +"?token="+token,
  297. (status, resp) => {
  298. if (status === 200 && resp.ok && resp.user && resp.user.email) {
  299. cb(resp.user.id +'_' +resp.team.id);
  300. } else {
  301. cb(null);
  302. }
  303. });
  304. } else {
  305. cb(null);
  306. }
  307. });
  308. };
  309. Slack.getOauthToken = function(code, cb) {
  310. httpsRequest(SLACK_ENDPOINT+GETAPI.oauth
  311. +"?client_id=" +config.services.Slack.clientId
  312. +"&client_secret=" +config.services.Slack.clientSecret
  313. +"&redirect_uri=" +encodeURIComponent(config.rootUrl +"account/addservice/slack")
  314. +"&code=" +code,
  315. (status, resp) => {
  316. if (status === 200 && resp.ok) {
  317. cb(resp["team_name"], resp["team_id"] +resp["user_id"], resp["access_token"]);
  318. } else {
  319. cb(null);
  320. }
  321. });
  322. };
  323. /**
  324. * @param {SlackChan|SlackGroup|SlackIms} channel
  325. * @param {string} contentType
  326. * @param {function(string|null)} callback
  327. **/
  328. Slack.prototype.openUploadFileStream = function(channel, contentType, callback) {
  329. var req = https.request({
  330. hostname: SLACK_HOSTNAME
  331. ,method: 'POST'
  332. ,path: SLACK_ENDPOINT_PATH +GETAPI.postFile
  333. +"?token=" +this.token
  334. +"&channels=" +channel.remoteId
  335. ,headers: {
  336. "Content-Type": contentType
  337. }
  338. }, (res) => {
  339. var errorJson;
  340. res.on("data", (chunk) => {
  341. errorJson = errorJson ? Buffer.concat([errorJson, chunk], errorJson.length +chunk.length) : Buffer.from(chunk);
  342. });
  343. res.once("end", () => {
  344. if (res.statusCode === 200) {
  345. callback(null);
  346. } else {
  347. try {
  348. errorJson = JSON.parse(errorJson.toString());
  349. } catch(e) {
  350. callback("error");
  351. return;
  352. }
  353. callback(errorJson["error"] || "error");
  354. }
  355. });
  356. });
  357. return req;
  358. };
  359. function findBoundary() {
  360. const prefix = '-'.repeat(15)
  361. ,alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  362. ,doYouKnowWhoManyTheyAre = alphabet.length // 26 letters in da alphabet
  363. ,nbArg = arguments.length;
  364. for (let i =0; i < doYouKnowWhoManyTheyAre; i++)
  365. bLoop: for (let j =0; j < doYouKnowWhoManyTheyAre; j++) {
  366. const boundary = prefix +alphabet[i] +alphabet[j];
  367. for (let argIndex =0; argIndex < nbArg; argIndex++) {
  368. if (arguments[argIndex].indexOf(boundary) >= 0)
  369. continue bLoop;
  370. }
  371. return boundary;
  372. }
  373. }
  374. function encodeWithBoundary(boundary, data) {
  375. var resp = "";
  376. for (var k in data) {
  377. resp += '--' +boundary +'\r\n';
  378. resp += 'Content-Disposition: form-data; name="' +k +'"\r\n\r\n'
  379. +data[k]
  380. +'\r\n';
  381. };
  382. return resp +'--' +boundary +'--\r\n';
  383. }
  384. /**
  385. * @param {string} serviceId
  386. * @param {Object} payload
  387. * @param {function(string|null)=} callback
  388. **/
  389. Slack.prototype.sendAction = function(serviceId, payload, callback) {
  390. var channel = this.data.channels[payload["channel_id"]]
  391. ,service = this.data.users[serviceId];
  392. if (channel && service) {
  393. payload["channel_id"] = channel.remoteId;
  394. var payloadString = JSON.stringify(payload)
  395. ,boundary = findBoundary(service.remoteId, payloadString)
  396. ,body = encodeWithBoundary(boundary, {
  397. "service_id": service.remoteId
  398. ,"payload": payloadString
  399. });
  400. var req = https.request({
  401. hostname: SLACK_HOSTNAME
  402. ,method: 'POST'
  403. ,path: SLACK_ENDPOINT_PATH +GETAPI.sendAction +"?token=" +this.token
  404. ,headers: {
  405. "Content-Type": "multipart/form-data; boundary=" +boundary,
  406. "Content-Length": body.length
  407. }
  408. }, (res) => {
  409. if (callback) {
  410. var resp = [];
  411. res.on("data", (chunk) => { resp.push(chunk); });
  412. res.once("end", () => {
  413. resp = Buffer.concat(resp).toString();
  414. try {
  415. resp = JSON.parse(resp);
  416. } catch (e) {
  417. resp = null;
  418. }
  419. callback(resp && resp.ok ? resp : false);
  420. });
  421. }
  422. });
  423. req.end(body);
  424. return true;
  425. }
  426. return false;
  427. };
  428. /**
  429. * @param {SlackChan|SlackGroup|SlackIms} channel
  430. * @param {string} contentType
  431. * @param {function(string|null)} callback
  432. **/
  433. Slack.prototype.openUploadFileStream = function(channel, contentType, callback) {
  434. var req = https.request({
  435. hostname: SLACK_HOSTNAME
  436. ,method: 'POST'
  437. ,path: SLACK_ENDPOINT_PATH +GETAPI.postFile
  438. +"?token=" +this.token
  439. +"&channels=" +channel.remoteId
  440. ,headers: {
  441. "Content-Type": contentType
  442. }
  443. }, (res) => {
  444. var errorJson;
  445. res.on("data", (chunk) => {
  446. errorJson = errorJson ? Buffer.concat([errorJson, chunk], errorJson.length +chunk.length) : Buffer.from(chunk);
  447. });
  448. res.once("end", () => {
  449. if (res.statusCode === 200) {
  450. callback(null);
  451. } else {
  452. try {
  453. errorJson = JSON.parse(errorJson.toString());
  454. } catch(e) {
  455. callback("error");
  456. return;
  457. }
  458. callback(errorJson["error"] || "error");
  459. }
  460. });
  461. });
  462. return req;
  463. };
  464. /**
  465. * @param {SlackChan|SlackGroup|SlackIms} channel
  466. * @param {string} msgId
  467. * @param {string} reaction
  468. **/
  469. Slack.prototype.addReaction = function(channel, msgId, reaction) {
  470. httpsRequest(SLACK_ENDPOINT +GETAPI.addReaction
  471. +"?token=" +this.token
  472. +"&name=" +reaction
  473. +"&channel="+channel.remoteId
  474. +"&timestamp="+msgId);
  475. }
  476. /**
  477. * @param {SlackChan|SlackGroup|SlackIms} channel
  478. * @param {string} msgId
  479. * @param {string} reaction
  480. **/
  481. Slack.prototype.removeReaction = function(channel, msgId, reaction) {
  482. httpsRequest(SLACK_ENDPOINT +GETAPI.removeReaction
  483. +"?token=" +this.token
  484. +"&name=" +reaction
  485. +"&channel="+channel.remoteId
  486. +"&timestamp="+msgId);
  487. }
  488. /**
  489. * @param {SlackChan|SlackGroup|SlackIms} channel
  490. * @param {Array.<string>} text
  491. **/
  492. Slack.prototype.sendMeMsg = function(channel, text) {
  493. httpsRequest(SLACK_ENDPOINT +GETAPI.postMeMsg
  494. +"?token=" +this.token
  495. +"&channel=" +channel.remoteId
  496. +"&text=" +text.join("\n")
  497. +"&as_user=true");
  498. };
  499. /**
  500. * @param {SlackChan|SlackGroup|SlackIms} channel
  501. **/
  502. Slack.prototype.starChannel = function(channel) {
  503. httpsRequest(SLACK_ENDPOINT +GETAPI.starChannel
  504. +"?token=" +this.token
  505. +"&channel=" +channel.remoteId);
  506. };
  507. /**
  508. * @param {SlackChan|SlackGroup|SlackIms} channel
  509. **/
  510. Slack.prototype.unstarChannel = function(channel) {
  511. httpsRequest(SLACK_ENDPOINT +GETAPI.unstarChannel
  512. +"?token=" +this.token
  513. +"&channel=" +channel.remoteId);
  514. };
  515. /**
  516. * @param {SlackChan|SlackGroup|SlackIms} channel
  517. * @param {Array.<string>} text
  518. * @param {Array.<Object>=} attachments
  519. **/
  520. Slack.prototype.sendMsg = function(channel, text, attachments) {
  521. if (attachments) {
  522. attachments.forEach((attachmentObj) => {
  523. if (attachmentObj["ts"])
  524. attachmentObj["ts"] /= 1000;
  525. });
  526. httpsRequest(SLACK_ENDPOINT +GETAPI.postMsg
  527. +"?token=" +this.token
  528. +"&channel=" +channel.remoteId
  529. +"&text=" +text.join("\n")
  530. + (attachments ? ("&attachments=" +encodeURIComponent(JSON.stringify(attachments))) : "")
  531. +"&as_user=true");
  532. } else {
  533. var decodedText = [];
  534. text.forEach(function(i) {
  535. decodedText.push(decodeURIComponent(i));
  536. });
  537. var fullDecodedText = decodedText.join("\n");
  538. this.pendingRtm[this.rtmId] = {
  539. type: 'message',
  540. channel: channel.remoteId,
  541. user: this.data.self.remoteId,
  542. text: fullDecodedText
  543. };
  544. this.rtm.send('{"id":' +this.rtmId++ +',"type":"message","channel":"' +channel.remoteId +'", "text":' +JSON.stringify(fullDecodedText) +'}');
  545. }
  546. };
  547. /**
  548. * @param {SlackChan|SlackGroup|SlackIms} channel
  549. * @param {string} msgId
  550. **/
  551. Slack.prototype.removeMsg = function(channel, msgId) {
  552. httpsRequest(SLACK_ENDPOINT +GETAPI.removeMsg
  553. +"?token=" +this.token
  554. +"&channel=" +channel.remoteId
  555. +"&ts=" +msgId
  556. +"&as_user=true");
  557. };
  558. /**
  559. * @param {SlackChan|SlackGroup|SlackIms} channel
  560. * @param {string} msgId
  561. * @param {string} text
  562. **/
  563. Slack.prototype.editMsg = function(channel, msgId, text) {
  564. httpsRequest(SLACK_ENDPOINT +GETAPI.editMsg
  565. +"?token=" +this.token
  566. +"&channel=" +channel.remoteId
  567. +"&ts=" +msgId
  568. +"&text=" +text.join("\n")
  569. +"&as_user=true");
  570. };
  571. /**
  572. * @param {SlackChan|SlackGroup|SlackIms} target
  573. **/
  574. Slack.prototype.fetchHistory = function(target, cb, count, firstMsgId) {
  575. var _this = this
  576. ,baseUrl = ""
  577. ,targetId = target.remoteId;
  578. if (targetId[0] === 'D') {
  579. baseUrl = SLACK_ENDPOINT +GETAPI.directHistory;
  580. } else if (targetId[0] === 'C') {
  581. baseUrl = SLACK_ENDPOINT +GETAPI.channelHistory;
  582. } else if (targetId[0] === 'G') {
  583. baseUrl = SLACK_ENDPOINT +GETAPI.groupHistory;
  584. }
  585. httpsRequest(baseUrl
  586. +"?token="+this.token
  587. +"&channel=" +targetId
  588. +(firstMsgId ? ("&inclusive=true&latest=" +firstMsgId) : "")
  589. +"&count=" +(count || 100),
  590. (status, resp) => {
  591. var history = [];
  592. if (status === 200 && resp && resp.ok) {
  593. var histo = this.history[target.id];
  594. if (!histo)
  595. histo = this.history[target.id] = new SlackHistory(_this, target.remoteId, target.id, this.data.team.id +'|', HISTORY_LENGTH, HISTORY_MAX_AGE);
  596. resp.messages.forEach((respMsg) => {
  597. respMsg["id"] = respMsg["ts"];
  598. history.push(histo.messageFactory(histo.prepareMessage(respMsg)));
  599. });
  600. }
  601. cb(history);
  602. });
  603. };
  604. Slack.prototype.getChatContext = function() {
  605. return this.data;
  606. };
  607. module.exports.Slack = Slack;