Formatting our messages

We can add some style to our messages, from text properties to links and buttons, so that they can become something more than a simple bunch of text. Actually, modifying by hand all the payloads to check what combinations of attributes best suits your messages can be too much hassle, but Slack helps us with an online Message Builder that lets you customize and preview your messages without the need to post it anywhere. Just head to your browser at https://api.slack.com/docs/messages/builder, and let's start having fun:

The payload editor lets you try your message before sending it.

Inside the upper box, we can forge our payload as we wish and preview it in the lower box, so let's see some of the more interesting bits that we can add to our messages:

  • Bold: Well, this is a classic. You can turn any strings of text in bold simply by wrapping it between two asterisks, try out this payload:
{
"text": "This is a *bold string* and this a *bold* word"
}

Here is how our bold lines appear in the Message Builder.

  • Italics: This stresses the importance of a word or a sentence, and you can get this effect simply by wrapping a string between the two underlines:
{
"text": "This is a _string in italics_."
}
  • Code: If you are writing some text that belongs to a command line or a some kind of code, you can enclose it between to back tickles and have it outstanding:
{
"text": "We can use the code attribute to write some code
like:n`x=y+1`"
}

Here is how your code is shown in the channel.

  • Code block: But how do you enclose a multi line code block? Let's see:
{
"text": "We can use the block code attribute to write some
multi line code like:n```y=1nx=y+1nx=2```"
}

A multi line code block appears a little different compare to a single line code string.

  • URL linking: You can insert clickable links in a message by enclosing the URL in <>. You can use two different ways to insert a link:
    • Just put the link itself surrounded by <>: <http://www.packtpub.com>
    • Add to the previous syntax "|linked" to make the linked string referring to the URL called <http://www.zarrelli.org|this>"

So, our payload with both syntaxes could well be this:

{
"text": "This is a web link <http://www.packtpub.com> and
<http://www.zarrelli.org|this too>"
}

We have two different ways to link a URL:

  • Email address linking: In a way similar to URL linking, just surround the email link and a "|linked" by <>:
{
"text": "Just email the <mail-
to:[email protected]|author>."
}

Click on the highlighted word, and your email client will fire up with the email address that is filled in as the recipient.

  • Date: We can format a date in your message using a Unix epoch timestamp and some selector to modify its appearance. We can optionally link a URL, but we always have to provide some fallback text to be displayed to older clients in case the time token conversion fails. The keyword to you in this case is <!date>, but with a tad more complex syntax:
{
"text": "<!date^unix_timestamp^ Some optional text {date_selector}|Fallback text>"
}

We have a bunch of different selectors available to modify how dates and times will appear in the channel:

  • {date}: Your date will appear as classic March 26th, so try out the following payload:
{
"text": "<!date^1490531695^ {date}|Fallback>"
}

So, our cURL line would be:

cURL -X POST -H 'Content-type: application/json' --data 
'{"
text":
"<!date^1490531695^ {date}|Fallback>"
}' https://hooks.slack.com/services/T4P7TPSP9/B4ND2E2E4/
lIzhH84lg21ZJ
0zdaeQHZ7ls

Here is how our messages will appear in the channel:

  • {date_short}: As the name states, this is an even more compact "Mar 26"
  • {date_long}: This gives you an extended date called "Sunday, March 26th"
  • {date_pretty}: This displays the date as {date}, but it uses "yesterday, "today" or "tomorrow" when it fits
  • {date_short_pretty}: This displays the date as {date_short}, but it uses "yesterday, "today" , or "tomorrow" when it fits
  • {date_long_pretty}: This displays the date as {date_long}, but it uses "yesterday, "today" , or "tomorrow" when it fits
  • {time}: This displays the time in a 12-hour format, and in our example, it is 1:34 PM; but if the client is set on a 24-hour format, it would be displayed as 13:34
  • {time_secs}: This displays the time to the seconds bit in a 12-hour format 1:34:55 PM; but if the client is set on a 24-hour format, it would be displayed as 13:34:55

We can also add a URL to the date so that when you click on the date/time, you will be brought the website pointed as follows:

{
"text":
"<!date^1490531695^{date}^http://www.packtpub.com|Fallback>"
}

Obviously, you can mix the formatter to have a more meaningful message as follows:

{
"text": "<!date^1490531695^Let's meet on {date} at
{time}|Meeting info>"
}

And the complete cURL line would be as follows:

cURL -X POST -H 'Content-type: application/json' --data '{"text": 
"<!date^1490531695^Let's meet on {date} at {time}|Meeting info>"}' https://hooks.slack.com/services/T4P7TPSP9/B4ND2E2E4/lIzhH84lg21ZJ
0zdaeQHZ7ls

Apart from the dates, we can use some special commands in your messages to get our audience to head up and pay attention to us:

  • <!here>: This will notify all our team members in the channel who are active:
{
"text": "<!here><!date^1490531695^{date}^http://www.packtpub.com|Fallback>"
}
  • <!channel>: This will notify all our team members in the channel regardless of their status. A notify icon will appear close to the channel name.
  • <!group>: This is a synonym of <!channel> and both can be used inside a channel or a group.
  • <!everyone>: This notifies all of our team members. This can be used in the team wide channel, which is usually called #general.

Using one of the notification tags will cause a notification icon to appear closer to the channel name.

If we want to have the attention of our audience, we can make use of the classical tools that are popular in the social networks: the so-called emojis. Slack will let us display any emoji we like, so let's just go to https://unicodey.com/emoji-data/table.htm, and choose the little drawings you like the most.

Once we have chosen our emojis, we can then forge a payload like this:

{
"text": "Guys, read carefully the examples:bangbang:, take
your time :hourglass:, be :b:rave and love the :shell:"
}

And cURL it:

cURL -X POST -H 'Content-type: application/json' --data '{ "text": "Guys, read carefully the examples:bangbang:, take your :hourglass:, be :b:rave and love the :shell:"}' https://hooks.slack.com/services/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

The results, in the following screenshot, are really nice: they attract attention in a fancy way, so our team members will not be haunted by our messages!


A fancy message can nicely convey the urgency of a statement. Finally, we can address a single user just using <@user| Optional handle> , and we can also override the channel we want to send the message to using this:

"channel": "#name_of_channel"

So, we could address the user called Giorgio who is in the channel called #general, asking him to join the test channel:

{ 
"text": "Hey <@giorgio|Giorgio> did you join the
<#test|Test> channel?", "channel": "#general"
}'

So, the complete cURL line would be as follows:

cURL -X POST -H 'Content-type: application/json' --data '{ "text": "Hey <@giorgio|Giorgio> did you join the <#test|Test> channel?", "channel": "#general"}' https://hooks.slack.com/services/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

With this message, the user Giorgio will receive a notification in the #general channel; and the content will be brought to his attention.

You do not need to convert in HTML the entire text of your message, but there are three characters that must be necessarily turned into HTML entities:
& must be replaced with &amp
< must be replaced with &lt
> must be replaced with &gt

What we did so far was pretty--all you can do with a plain JSON without any hurdles, but if we want to further spice up our messages, we have to reply to message attachments, which will enable us to send images, attach buttons, and much more. So, our next step will be the message attachments to see not only how to embellish our messages, but how to make them more useful and effective.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.165.246