Message Templates

Message templates in MsgGO allow you to customize the content and format of notifications sent to various delivery targets when an event is received. Using templates, you can create dynamic messages that include data from the received event, making your notifications more informative and contextually relevant.

Understanding Message Templates

Message templates define how your notifications appear when delivered to destinations like Slack, email, or SMS. With MsgGO's templating system, you can:

  • Create multiple templates for a single event definition
  • Define different templates for different delivery targets
  • Include dynamic content using event data variables
  • Create conditional content using Twig templating language

Each delivery definition must have a message template assigned to it to determine what content will be sent when an event is received.

Creating Message Templates

You can create message templates in two ways:

Method 1: From the Events List

  1. Navigate to the Events section in the left sidebar
  2. Find the event you want to create a template for
  3. Click the Add delivery button next to the event
  4. In the modal dialog, select a delivery target
  5. Fill in all required fields for your selected delivery target (e.g., for Slack, select workspace and channel first)
  6. After filling required fields, the Message template option will appear
  7. Choose New message template from the dropdown
  8. Enter a name for your template and create your message content
  9. Click Save to create both the delivery target and the template

Method 2: From Event Editing

  1. Navigate to the event definition you want to create templates for
  2. Scroll down to the "Message templates" section
  3. Click the New button to create a new template
  4. Enter a name for your template
  5. Select which delivery target this template applies to
  6. Create your message content
  7. Click Save to store your template

Using Twig in Templates

MsgGO now uses Twig as its templating language. Twig is a flexible, secure, and feature-rich template engine that provides powerful features for creating dynamic content.

Basic Twig Syntax

Variables: Access data with double curly braces:

{{ event.name }}

Conditionals: Create different outputs based on conditions:

{% if data.status == 'success' %}
  Success!
{% else %}
  Failure!
{% endif %}

Loops: Iterate through data collections:

{% for item in data.items %}
  - {{ item.name }}
{% endfor %}

Set: Create new variables or modify existing ones:

{% set greeting = 'Hello' %}
{% set name = data.user.firstname %}
{{ greeting }}, {{ name }}!

Filters: Modify variables and expressions:

{{ data.message|upper }}

For more details on filters, see the "Available Filters" section below.

Available Variables

When creating message templates, you can use the following variables to insert dynamic content:

Variable Description
event.name Event name
event.receivedAt Date of receiving the event (format: YYYY/MM/DD HH:MM:SS)
data Object containing all data delivered with the event. To see all data in serialized form, use the json_encode filter: {{ data | json_encode }}
data.keyName Access to a specific key in the data

To insert these variables into your template, place them between double curly braces as per Twig syntax. For example:

New event received: {{ event.name }}
Time: {{ event.receivedAt }}
Status: {{ data.status }}

Filters

Filters allow you to modify variables and expressions. They are separated from the variable by a pipe symbol (|). Multiple filters can be chained, with the output of one filter applied to the next.

Basic Filter Syntax:

{{ name|title }}

Filters with Arguments:

{{ list|json_ncode(', ') }}

Important: When filtering complex expressions, use parentheses as the filter operator has the highest precedence:

{{ (1..5)|json_encode }}
{{ ('HELLO' ~ 'WORLD')|lower }}

Available Filters

MsgGO provides several useful filters that you can use to format your message content. Here's a complete list with examples:

capitalize

Capitalizes the first letter of each word in a string.

{{ "hello world"|capitalize }}
{# Output: Hello World #}
default

Provides a default value if the variable is empty or undefined.

{{ data.author|default('Anonymous') }}
{# If data.author is empty, outputs: Anonymous #}
date

Formats dates and timestamps into human-readable formats. This filter is highly flexible and accepts various date input types.

{{ data.created_at|date('dd.MM.yyyy') }}
{# Output example: 15.04.2023 #}

{{ data.timestamp|date('HH:mm:ss', 'Europe/Warsaw') }}
{# Output example: 14:30:45 (in Warsaw timezone) #}

Input Types:

  • String timestamp (e.g., "1617183600")
  • Numeric timestamp
  • ISO 8601 date string (e.g., "2023-04-15T14:30:45Z")
  • RFC 2822 date string (e.g., "Sat, 15 Apr 2023 14:30:45 +0000")

Parameters:

  • Format string (default: 'dd.mm.yyyy')
  • Timezone (optional, e.g., 'Europe/Warsaw', 'America/New_York')

Format Options:

The format parameter accepts two types of values:

Presets - Predefined format strings:
Preset Description Example in en_US
DATE_SHORT short date 10/14/1983
DATE_MED abbreviated date Oct 14, 1983
DATE_MED_WITH_WEEKDAY abbreviated date with abbreviated weekday Fri, Oct 14, 1983
DATE_FULL full date October 14, 1983
DATE_HUGE full date with weekday Friday, October 14, 1983
TIME_SIMPLE time 1:30 PM
TIME_WITH_SECONDS time with seconds 1:30:23 PM
TIME_WITH_SHORT_OFFSET time with seconds and abbreviated named offset 1:30:23 PM EDT
TIME_WITH_LONG_OFFSET time with seconds and full named offset 1:30:23 PM Eastern Daylight Time
TIME_24_SIMPLE 24-hour time 13:30
TIME_24_WITH_SECONDS 24-hour time with seconds 13:30:23
TIME_24_WITH_SHORT_OFFSET 24-hour time with seconds and abbreviated named offset 13:30:23 EDT
TIME_24_WITH_LONG_OFFSET 24-hour time with seconds and full named offset 13:30:23 Eastern Daylight Time
DATETIME_SHORT short date & time 10/14/1983, 1:30 PM
DATETIME_MED abbreviated date & time Oct 14, 1983, 1:30 PM
DATETIME_MED_WITH_WEEKDAY abbreviated date & time with abbreviated weekday Fri, Oct 14, 1983, 1:30 PM
DATETIME_FULL full date and time with abbreviated named offset October 14, 1983 at 1:30 PM EDT
DATETIME_HUGE full date and time with weekday and full named offset Friday, October 14, 1983 at 1:30 PM Eastern Daylight Time
DATETIME_SHORT_WITH_SECONDS short date & time with seconds 10/14/1983, 1:30:23 PM
DATETIME_MED_WITH_SECONDS abbreviated date & time with seconds Oct 14, 1983, 1:30:23 PM
DATETIME_FULL_WITH_SECONDS full date and time with abbreviated named offset with seconds October 14, 1983 at 1:30:23 PM EDT
DATETIME_HUGE_WITH_SECONDS full date and time with weekday and full named offset with seconds Friday, October 14, 1983 at 1:30:23 PM Eastern Daylight Time

Example using a preset:

{{ data.timestamp|date('DATETIME_FULL') }}
{# Output: October 14, 1983 at 1:30 PM EDT #}
Format Tokens - Individual tokens for custom formats:
Standalone token Format token Description Example
S millisecond, no padding 54
SSS millisecond, padded to 3 054
u fractional seconds, functionally identical to SSS 054
s second, no padding 4
ss second, padded to 2 padding 04
m minute, no padding 7
mm minute, padded to 2 07
h hour in 12-hour time, no padding 1
hh hour in 12-hour time, padded to 2 01
H hour in 24-hour time, no padding 9
HH hour in 24-hour time, padded to 2 13
d day of the month, no padding 6
dd day of the month, padded to 2 06
L M month as an unpadded number 8
LL MM month as a padded number 08
LLL MMM month as an abbreviated localized string Aug
LLLL MMMM month as an unabbreviated localized string August
y year, unpadded 2014
yy two-digit year 14
yyyy four- to six- digit year, pads to 4 2014
Z narrow offset +5
ZZ short offset +05:00
ZZZ techie offset +0500
ZZZZ abbreviated named offset EST
z IANA zone America/New_York
a meridiem AM
ccc EEE day of the week, as an abbreviate localized string Wed
cccc EEEE day of the week, as an unabbreviated localized string Wednesday

And many more tokens for formatting date and time components. Some additional useful tokens:

Token Description Example
X unix timestamp in seconds 1407287224
x unix timestamp in milliseconds 1407287224054
D localized numeric date 9/4/2017
T localized 24-hour time 13:07
f short localized date and time 8/6/2014, 1:07 PM

Format Token Examples:

{{ data.timestamp|date('yyyy-MM-dd') }}         {# 2023-04-15 #}
{{ data.timestamp|date('HH:mm:ss') }}           {# 14:30:45 #}
{{ data.timestamp|date('dd MMM yyyy') }}        {# 15 Apr 2023 #}
{{ data.timestamp|date('EEEE, MMMM d, yyyy') }} {# Saturday, April 15, 2023 #}
{{ data.timestamp|date('dd.MM.yyyy HH:mm:ss') }} {# 15.04.2023 14:30:45 #}

With Timezone:

{{ data.timestamp|date('HH:mm:ss', 'UTC') }}
{{ data.timestamp|date('yyyy-MM-dd HH:mm:ss', 'America/Los_Angeles') }}

If the input cannot be parsed as a valid date, the filter will output [invalid date].

lower

Converts a string to lowercase.

{{ "HELLO"|lower }}
{# Output: hello #}
merge

Merges arrays or objects.

{% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}
{% set new_items = items|merge({ 'car': 'vehicle' }) %}
{# new_items now contains: { 'apple': 'fruit', 'orange': 'fruit', 'car': 'vehicle' } #}
number_format

Formats numbers with grouped thousands and specified decimal points.

{{ 1234.5678|number_format(2, '.', ',') }}
{# Output: 1,234.57 #}

Parameters:

  • Number of decimal places (2 in example)
  • Decimal point character ('.' in example)
  • Thousands separator character (',' in example)
replace

Replaces strings within a string.

{{ "Hello World"|replace({'Hello': 'Hi', 'World': 'Everyone'}) }}
{# Output: Hi Everyone #}
reverse

Reverses a string or an array.

{{ "Hello"|reverse }}
{# Output: olleH #}

{% set array = [1, 2, 3] %}
{% set reversed = array|reverse %}
{# reversed contains: [3, 2, 1] #
round

Rounds a number to a specified precision.

{{ 42.123|round }}
{# Output: 42 #}

{{ 42.123|round(2, 'floor') }}
{# Output: 42.12 #}

{{ 42.123|round(2, 'ceil') }}
{# Output: 42.13 #}

Parameters:

  • Precision (number of decimal places)
  • Method ('common', 'ceil', or 'floor')
slice

Extracts a slice of a sequence or string.

{{ "Hello World"|slice(6, 5) }}
{# Output: World #}

{% set array = [1, 2, 3, 4, 5] %}
{{ array|slice(1, 3)|json_encode }}
{# Output: 2, 3, 4 #}

Parameters:

  • Start position
  • Length
  • (Optional) Preserve keys (for arrays)
title

Returns a title-cased string (first letter of each word capitalized).

{{ "hello world"|title }}
{# Output: Hello World #}
trim

Trims whitespace (or other characters) from the beginning and/or end of a string.

{{ "  Hello  "|trim }}
{# Output: Hello #}

{{ "!!!Hello!!!"|trim('!') }}
{# Output: Hello #}

{{ "  Hello  "|trim(null, 'left') }}
{# Output: Hello   #}

Parameters:

  • Character mask (optional, defaults to whitespace)
  • Side ('both', 'left', or 'right')
upper

Converts a string to uppercase.

{{ "hello"|upper }}
{# Output: HELLO #}
url_encode

URL encodes a string or array according to RFC 3986.

{{ "hello world"|url_encode }}
{# Output: hello%20world #}

{% set params = {'key': 'value', 'foo': 'bar'} %}
{{ params|url_encode }}
{# Output: key=value&foo=bar #}

Delivery Target-Specific Formatting

Each delivery target in MsgGO has its own formatting capabilities and limitations. The message editor will automatically adapt based on the delivery target you select.

Slack

  • Uses Slack Markdown for formatting
  • Supports bold, italic, code blocks, and other Slack-specific formatting
  • For more details on Slack formatting, see Slack's formatting documentation

Email

  • Uses standard Markdown for formatting
  • Supports headings, lists, links, and other common markdown features
  • Maximum message length: 40,000 characters
  • For more details on Email formatting, see Markdown Guide

SMS

  • Uses plain text only (no formatting)
  • Does not support diacritical marks, emoji, or UTF-8 special characters
  • Any special characters will be automatically converted to ASCII equivalents or removed
  • Maximum message length varies by region, generally limited to 160 characters

URL (Webhook)

Delivery Target Limitations

Different delivery targets have specific limitations that you should be aware of:

  • SMS: Cannot contain diacritical marks, emoji, or UTF-8 characters. These will be automatically converted to ASCII equivalents or removed. Limited to 160 characters per message.
  • Email: Maximum content length of 40,000 characters. HTML elements may be stripped depending on the receiving email client.
  • Slack: Maximum of 4,000 characters per message. Some advanced formatting may not display correctly in all Slack clients.

Best Practices for Message Templates

  • Keep templates concise: Focus on the most important information, especially for SMS and chat platforms
  • Use variables effectively: You don't need to worry about missing data - if a variable doesn't exist, it simply won't be rendered
  • Test your templates: Send test events to verify your templates work as expected
  • Consider the context: Design templates with the delivery channel in mind
  • Use descriptive names: Give your templates clear names that indicate their purpose

By effectively using message templates in MsgGO, you can ensure that the right information reaches the right people in the most appropriate format for each communication channel.