├── .babelrc
├── .gitignore
├── README.md
├── app
├── application.js
├── handlebars-helpers.js
├── slack-message-builder.js
└── templates
│ ├── attachment.hbs
│ ├── attachment_author.hbs
│ ├── attachment_field.hbs
│ ├── attachment_fields.hbs
│ ├── attachment_title.hbs
│ └── message.hbs
├── build.js
├── dist
├── application.js
└── style.css
├── index.html
├── module_wrappers
├── wrap-end.frag.js
└── wrap-start.frag.js
├── package.json
└── style
├── _attachment.scss
├── _attachment_field.scss
├── _message.scss
└── style.scss
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["es2015"]
3 | }
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | npm-debug.log
3 | .sass-cache
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Slack Message Builder
2 |
3 | **Slack now have a tool that does this https://api.slack.com/docs/messages/builder**
4 |
5 | See what the payload you send to a [Slack](https://slack.com) `chat.postMessage` will be displayed like.
6 | Slack Formatting documentation: https://api.slack.com/docs/formatting
7 |
8 | Give it a go:
9 | http://davestevens.github.io/slack-message-builder
10 |
11 | ## Development
12 |
13 | Written in ES6 (using Browserify) and Sass.
14 |
15 | ### Building
16 |
17 | ```npm install```
18 | Install all required dependencies.
19 |
20 | ```npm run build```
21 | Builds JS and CSS from `app` directory into `dist` directory.
22 |
23 | ```npm start```
24 | Runs a local server.
25 |
--------------------------------------------------------------------------------
/app/application.js:
--------------------------------------------------------------------------------
1 | var $ = require("jquery");
2 | var SlackMessageBuilder = require("./slack-message-builder");
3 |
4 | var builder = new SlackMessageBuilder({
5 | $input: $(".input"),
6 | $output: $(".output")
7 | });
8 | builder.bind_events();
9 |
--------------------------------------------------------------------------------
/app/handlebars-helpers.js:
--------------------------------------------------------------------------------
1 | var Handlebars = require("handlebars/runtime")["default"];
2 | var $ = require("jquery");
3 |
4 | module.exports = Handlebars;
5 |
6 | Handlebars.registerPartial({
7 | message: require("./templates/message.hbs"),
8 | attachment: require("./templates/attachment.hbs"),
9 | attachment_author: require("./templates/attachment_author.hbs"),
10 | attachment_title: require("./templates/attachment_title.hbs"),
11 | attachment_fields: require("./templates/attachment_fields.hbs"),
12 | attachment_field: require("./templates/attachment_field.hbs")
13 | });
14 |
15 | Handlebars.registerHelper("attachment_format", (name, attachment) => {
16 | var string = attachment[name];
17 | var markdown = $.inArray(name, attachment.mrkdwn_in) > -1;
18 | return Handlebars.helpers.format(string, markdown);
19 | });
20 |
21 | Handlebars.registerHelper("member_image", (item) => {
22 | // TODO: emoji_url || icon_url
23 | return item.icon_url;
24 | });
25 |
26 | Handlebars.registerHelper("timestamp", () => {
27 | var date = new Date()
28 | var hours = date.getHours()
29 | var minutes = date.getMinutes()
30 | var meridiem = (hours > 11) ? "PM" : "AM"
31 | return `${hours % 12}:${minutes} ${meridiem}`
32 | });
33 |
34 | Handlebars.registerHelper("format", (string, markdown) => {
35 | if (!string) { return ""; }
36 | // links
37 | string = string.replace(/<(.*?)>/g, "$1");
38 |
39 | // replace newlines
40 | string = string.replace("\n", "
");
41 | // replace spaces
42 | string = string.replace(/\s/g, " ");
43 |
44 | if (markdown) {
45 | // markdown
46 | //// bold *
47 | string = string.replace(/\*(.*?)\*/g, "$1");
48 | //// italic _
49 | string = string.replace(/_(.*?)_/g, "$1");
50 | //// pre ```
51 | string = string.replace(/```(.*?)```/g, "
$1"); 52 | //// code ` 53 | string = string.replace(/`(.*?)`/g, "
$1
");
54 | }
55 |
56 | return new Handlebars.SafeString(string);
57 | });
58 |
59 | Handlebars.registerHelper("wrap_link", (item, href) => {
60 | if (!href) { return item; }
61 | return new Handlebars.SafeString("#{item}");
62 | });
63 |
--------------------------------------------------------------------------------
/app/slack-message-builder.js:
--------------------------------------------------------------------------------
1 | var $ = require("jquery");
2 | var Handlebars = require("./handlebars-helpers");
3 |
4 | class SlackMessageBuilder {
5 | constructor(options) {
6 | this.$input = options.$input;
7 | this.$output = options.$output;
8 |
9 | this.defaults = {
10 | payload: {
11 | username: "incoming-webhook",
12 | icon_url: "https://avatars1.githubusercontent.com/u/287677?v=3&s=40",
13 | mrkdwn: true,
14 | attachments: []
15 | },
16 | attachment: {
17 | color: "#E3E4E6",
18 | mrkdwn_in: []
19 | }
20 | };
21 | }
22 |
23 | bind_events() {
24 | this.$input
25 | .find(".js-build")
26 | .on("click", this._parse_message.bind(this));
27 | }
28 |
29 | // Private
30 |
31 | _parse_message(event) {
32 | event.preventDefault();
33 |
34 | var $payload = this.$input.find(".js-payload"),
35 | payload = this._parse($payload.val());
36 | if (!payload) { return; }
37 | $payload.val(JSON.stringify(payload, null, 2));
38 |
39 | payload = $.extend({}, this.defaults.payload, payload);
40 | payload.attachments = this._extend_attachments(payload.attachments);
41 |
42 | var html = Handlebars.partials.message(payload);
43 |
44 | this.$output.append(html);
45 | this.$output.animate({ scrollTop: this.$output.get(0).scrollHeight }, 800);
46 | }
47 |
48 | _parse(input) {
49 | try {
50 | return JSON.parse(input);
51 | }
52 | catch(error) {
53 | // TODO: display error inline
54 | alert(`Error parsing JSON: ${error.message}`);
55 | }
56 | }
57 |
58 | _extend_attachments(attachments) {
59 | var defaults = this.defaults.attachment;
60 | return attachments.map(attachment => {
61 | return $.extend({}, defaults, attachment);
62 | });
63 | }
64 | };
65 |
66 | module.exports = SlackMessageBuilder;
67 |
--------------------------------------------------------------------------------
/app/templates/attachment.hbs:
--------------------------------------------------------------------------------
1 |
13 |
--------------------------------------------------------------------------------
/app/templates/attachment_author.hbs:
--------------------------------------------------------------------------------
1 | {{#if author_name}}
2 |
15 | {{/if}}
16 |
--------------------------------------------------------------------------------
/app/templates/attachment_field.hbs:
--------------------------------------------------------------------------------
1 |