reshuffle-slack-connector
Version:
A Reshuffle Slack connector
104 lines • 3.21 kB
JavaScript
;
exports.__esModule = true;
var ACTION_REGEX = /^[a-z0-9-]+$/;
var SlackMessage = /** @class */ (function () {
function SlackMessage(blocks) {
this.blocks = blocks || [];
this.actions = [];
}
SlackMessage.prototype.renderActions = function () {
if (this.actions.length > 0) {
this.blocks.push({
type: 'actions',
elements: this.actions
});
this.actions = [];
}
};
SlackMessage.prototype.button = function (text, action, payload, styleToApply) {
if (!ACTION_REGEX.test(action)) {
throw new Error("Invalid action: " + action);
}
var style = typeof styleToApply === 'string' ? styleToApply.toLowerCase() : styleToApply;
if (style !== undefined && style !== 'primary' && style !== 'danger') {
throw new Error("Invalid button style: " + styleToApply);
}
var btn = {
type: 'button',
text: {
type: 'plain_text',
text: text
},
value: JSON.stringify({ action: action, payload: payload }),
action_id: action + "_" + text,
style: style
};
this.actions.push(btn);
};
SlackMessage.prototype.link = function (text, url) {
var btn = {
type: 'button',
text: {
type: 'plain_text',
text: text
},
url: url
};
this.actions.push(btn);
};
SlackMessage.prototype.dangerButton = function (text, action, payload) {
this.button(text, action, payload, 'danger');
};
SlackMessage.prototype.primaryButton = function (text, action, payload) {
this.button(text, action, payload, 'primary');
};
SlackMessage.prototype.divider = function () {
this.renderActions();
this.blocks.push({
type: 'divider'
});
};
SlackMessage.prototype.fields = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
this.renderActions();
this.blocks.push({
type: 'section',
fields: args.map(function (e) { return ({ type: 'mrkdwn', text: e }); })
});
};
SlackMessage.prototype.image = function (url, alt, title) {
this.renderActions();
var img = {
type: 'image',
image_url: url,
alt_text: alt
};
if (title) {
img.title = {
type: 'plain_text',
text: title
};
}
this.blocks.push(img);
};
SlackMessage.prototype.text = function (text) {
this.renderActions();
this.blocks.push({
type: 'section',
text: {
type: 'plain_text',
text: text
}
});
};
SlackMessage.prototype.getBlocks = function () {
this.renderActions();
return this.blocks;
};
return SlackMessage;
}());
exports["default"] = SlackMessage;
//# sourceMappingURL=SlackMessage.js.map