@nu-art/storm
Version:
Storm - Express & Typescript based backend framework
81 lines (80 loc) • 3.86 kB
JavaScript
;
/*
* Storm contains a list of utility functions.. this project
* might be broken down into more smaller projects in the future.
*
* Copyright (C) 2020 Adam van der Kruk aka TacB0sS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModuleBE_Slack = exports.ModuleBE_Slack_Class = void 0;
/**
* Created by AlanBen on 29/08/2019.
*/
const ts_common_1 = require("@nu-art/ts-common");
const web_api_1 = require("@slack/web-api");
class ModuleBE_Slack_Class extends ts_common_1.Module {
constructor() {
super('slack');
this.messageMap = {};
}
init() {
if (!this.config.token)
return;
// throw new ImplementationMissingException('Missing config token for ModuleBE_Slack. Please add it');
this.web = new web_api_1.WebClient(this.config.token, Object.assign({ rejectRateLimitedCalls: true }, this.config.slackConfig));
}
postMessage(slackMessage, messageId) {
return __awaiter(this, void 0, void 0, function* () {
const message = typeof slackMessage === 'string' ? { text: slackMessage, channel: this.config.defaultChannel } : slackMessage;
const time = this.messageMap[message.text];
if (time && (0, ts_common_1.currentTimeMillis)() - time < (this.config.throttlingTime || ts_common_1.Minute))
return;
try {
return yield this.postMessageImpl(message, messageId);
}
catch (e) {
this.logError(`Error while sending a message to channel: ${message.channel}\n`, e);
}
});
}
postMessageImpl(message, threadPointer) {
return __awaiter(this, void 0, void 0, function* () {
let res;
if (threadPointer) {
const reply = Object.assign(Object.assign({}, message), { thread_ts: threadPointer.ts });
this.logDebug('sending slack reply: ', reply);
res = (yield this.web.chat.postMessage(reply));
}
else {
this.logDebug('sending slack message: ', message);
res = (yield this.web.chat.postMessage(message));
}
this.messageMap[message.text] = (0, ts_common_1.currentTimeMillis)();
this.logDebug(`A message was posted to channel: ${message.channel} with message id ${res.ts} which contains the message ${message.text}`);
return { ts: res.ts, channel: res.channel };
});
}
}
exports.ModuleBE_Slack_Class = ModuleBE_Slack_Class;
exports.ModuleBE_Slack = new ModuleBE_Slack_Class();