Blog

Documentation

Pricing

Log In
Sign Up
All Posts
Courier Live Header
COURIER LIVE

Courier Live: Designing Slack Notifications using Block Kit and Jsonnet

Aydrian Howard

September 02, 2020

Earlier this summer, Riley Napier from our engineering team joined me for our June 24th Courier Live to help me build a Slack Slash Command to display estimated departure times for BART Stations. We created a Glitch ExpressJS app to accept the commands and Courier to handle the responses. We designed the messages by dynamically generating Block Kit using Jsonnet.

Check out the video below to watch us:

  • Walk through setting up a Glitch App that handles Slack Slash Commands

  • Create a notification for Slack

  • Use the Jsonnet Block to dynamically create Block Kit elements

Find the full project code on the Courier Slack Slash Bart Glitch App.

Be sure to Like the video and Subscribe to our YouTube channel.

What is Jsonnet?

Jsonnet is a powerful data templating language for JSON. It has a Python-like syntax that allows you to build JSON output using variables, functions, conditionals, etc. This comes in handy when dynamically creating Block Kit elements for Slack. Courier provides the following functions that allow you to grab data and profile information.

1 2 3 4 5 # Grab values by JSON path from data passed during send local data = data("path", "default"); # Grab values by JSON path from merged recipient profile local profile =  profile("path", "default");

For an overview of the basics of Jsonnet syntax, check out Learn Jsonnet in Y minutes.

Displaying BART Station Departure Times

To display the Departure Times passed to the notification, we used variables, functions, and list comprehensions to generate the resulting Block Kit sections. We started with the following data originating from the BART API:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 {   "data": {     "date": "09/01/2020",     "time": "06:50:01 AM PDT",     "station": {       "name": "Powell St.",       "abbr": "POWL",       "etd": [         {           "destination": "Antioch",           "abbreviation": "ANTC",           "limited": "0",           "estimate": [             {               "minutes": "7",               "platform": "2",               "direction": "North",               "length": "10",               "color": "YELLOW",               "hexcolor": "#ffff33",               "bikeflag": "1",               "delay": "0"             },             {               "minutes": "37",               "platform": "2",               "direction": "North",               "length": "10",               "color": "YELLOW",               "hexcolor": "#ffff33",               "bikeflag": "1",               "delay": "0"             },             {               "minutes": "66",               "platform": "2",               "direction": "North",               "length": "10",               "color": "YELLOW",               "hexcolor": "#ffff33",               "bikeflag": "1",               "delay": "0"             }           ]         },         {           "destination": "Berryessa",           "abbreviation": "BERY",           "limited": "0",           "estimate": [             {               "minutes": "9",               "platform": "2",               "direction": "North",               "length": "10",               "color": "GREEN",               "hexcolor": "#339933",               "bikeflag": "1",               "delay": "0"             },             {               "minutes": "39",               "platform": "2",               "direction": "North",               "length": "10",               "color": "GREEN",               "hexcolor": "#339933",               "bikeflag": "1",               "delay": "0"             }           ]         }       ]     },     "message": ""   }  }

Using a Jsonnet block, we used the following Jsonnet code to render the Block Kit Sections

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 local station = data("station"); local get_image(color, direction) =  "https://dummyimage.com/100x100/%s/000000.png&text=%s" % [color[1:], direction]; local get_cars(length) = std.join("", [ ":train:" for x in std.range(1, std.parseInt(length))]); local is_bike(flag) =  if flag == "1" then "\n:bike:" else ""; local get_estimate(estimate) =  {      "type": "section",         "text": {          "type": "mrkdwn",             "text": ":clock4: *%s min*\nPlatform %s\n%s %s" % [estimate.minutes, estimate.platform, get_cars(estimate.length), is_bike(estimate.bikeflag)]         },         "accessory": {          "type": "image",             "image_url": get_image(estimate.hexcolor, estimate.direction),             "alt_text": "%s %s" % [estimate.direction, estimate.color]         }     }; local get_estimates(estimates) =  [      get_estimate(estimate)     for estimate in estimates]; local get_destinations(etd) =  [       { "type": "section", "text": { "type": "mrkdwn", "text": "*%s*" % etd.destination } }     ] + get_estimates(etd.estimate) +     [         {         "type": "divider"         }     ]; std.flattenArrays([ get_destinations(etd) for etd in station.etd ])

This results in Block Kit that looks like the following:

Bart API Slack Blocks

Feel free to remix our Glitch app and create your own Slack Slash Command. Be sure to let us know what you create.

Is there something you’d like to see us do using Courier? Let us know and it might be the subject of our next Courier Live. We stream a new Courier Live every Wednesday at noon Pacific. Follow us on Twitch to be notified when we go live.

-Aydrian

Subscribe

More from Courier Live

April 2021 Courier Live Release Notes Thumbnail
COURIER LIVE

Courier April Product Release Updates: In-app Inbox, Preferences, and Automations

In this update, Aydrian and Nate (Head of Customer Success) as cohosts cover what is new with the in-app...

Aydrian Howard

May 20, 2021

Courier Live Thumbnail
COURIER LIVE

Courier Live: Content, Notifications, and IoT

On February 2nd and 3rd, Shy Ruparel joined Aydrian for another Courier and Contentful crossover stream....

Aydrian Howard

February 10, 2021

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Email & push notification

Product

© 2021 Courier. All rights reserved.