munar-adapter-slack
Version:
Slack.com adapter for Munar
122 lines (94 loc) • 4.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _client = require("@slack/client");
var _munarCore = require("munar-core");
var _Channel = _interopRequireDefault(require("./Channel"));
var _Message = _interopRequireDefault(require("./Message"));
var _User = _interopRequireDefault(require("./User"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const debug = require('debug')('munar:adapter:slack');
class Slack extends _munarCore.Adapter {
constructor(bot, options) {
super(bot);
this.store = new _client.MemoryDataStore();
this.onMessage = slackMessage => {
debug([slackMessage.type, slackMessage.subtype].filter(Boolean).join(':'), _objectSpread({
user: slackMessage.user
}, slackMessage));
if (slackMessage.type === 'message') {
if (slackMessage.subtype && slackMessage.subtype !== 'me_message') {
return;
}
const channel = this.getChannel(slackMessage.channel);
this.receive('message', new _Message.default(channel, this.normalizeMessage(slackMessage.text), slackMessage));
}
};
this.options = options;
}
async connect() {
return new Promise((resolve, reject) => {
this.web = new _client.WebClient(this.options.token);
this.client = new _client.RtmClient(this.options.token, {
dataStore: this.store,
autoReconnect: true,
autoMark: true
});
this.client.on(_client.CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, self => {
const team = this.store.getTeamById(this.client.activeTeamId);
const user = this.store.getUserById(this.client.activeUserId);
debug('connected', team.name, user.name);
this.self = new _User.default(this, user);
resolve();
});
this.client.on('message', this.onMessage);
this.client.on('error', reject);
this.webClient = new _client.WebClient(this.options.token);
this.client.login();
});
}
disconnect() {
this.client.disconnect();
this.client = null;
}
getSelf() {
return this.self;
}
getUsers() {
const users = this.store.users;
return Object.keys(users).map(id => {
return new _User.default(this, this.store.getUserById(id));
});
}
getChannels() {
const channels = this.store.channels;
return Object.keys(channels).map(id => {
return new _Channel.default(this, this.store.getChannelById(id));
});
}
getChannel(id) {
const channel = this.store.getChannelGroupOrDMById(id);
return channel ? new _Channel.default(this, channel) : null;
}
getChannelByName(name) {
let channel;
if (name[0] === '@') {
channel = this.store.getDMByName(name.slice(1));
if (!channel) {
channel = this.store.getBotByName(name.slice(1));
}
} else {
channel = this.store.getChannelByName(name);
}
return channel ? new _Channel.default(this, channel) : null;
}
normalizeMessage(text = '') {
return text.trim().replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/<!channel>/g, '@channel').replace(/<!group>/g, '@group').replace(/<!everyone>/g, '@everyone').replace(/<#(C\w+)\|?(\w+)?>/g, (_, channelId) => `#${this.getChannel(channelId).name}`).replace(/<@(U\w+)\|?(\w+)?>/g, (_, userId) => `@${this.getUser(userId).name}`).replace(/<(?!!)(\S+)>/g, (_, link) => link).replace(/<!(\w+)\|?(\w+)?>/g, (_, command, label) => `<${label || command}>`);
}
}
exports.default = Slack;
Slack.adapterName = 'slack';