Skip to content

[FEATURE] Use unicode characters in template fileΒ #359

@jverger

Description

@jverger

Description

UnicodeEncodeError is throwing when trying to send unicode characters with teams.j2 template

Why is this needed?

Using unicode characters emoji helps to create more attractive messages

Additional Information

I'm trying to send unicode characters in my teams.j2 template.
Unfortunately it does not work ans i can see the following message in the output log:

UnicodeEncodeError: 'latin-1' codec can't encode character '\U0001f525' in position 769: Body ('πŸ”₯') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

Here is my teams.j2 template file:

{
  "type": "message",
  "attachments": [
    {
      "contentType": "application/vnd.microsoft.card.adaptive",
      "content": {
        "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
        "type": "AdaptiveCard",
        "version": "1.4",
        "msteams": {
          "width": "Full"
        },
        "body": [
          {
            "type": "ColumnSet",
            "style": "{% if status=='firing' %}attention{% elif status=='resolved' %}good{% else %}warning{% endif %}",
            "columns": [
              {
                "type": "Column",
                "width": "stretch",
                "items": [
                  {
                    "type": "TextBlock",
                    "weight": "Bolder",
                    "size": "ExtraLarge",
                    "color": "{% if status=='firing' %}attention{% elif status=='resolved' %}good{% else %}warning{% endif %}",
                    "text": "{% if status=='firing' %}πŸ”₯{% elif status=='resolved' %}βœ…{% else %}⚠️{% endif %} Prometheus alert {% if status=='resolved' %}(Resolved){% elif status=='firing' %}(Firing){% elif status=='unknown' %}(Unknown){% else %}(Warning){% endif %}"
                  },
                  {
                    "type": "TextBlock",
                    "weight": "Bolder",
                    "size": "ExtraLarge",
                    "text": "{% if status=='resolved' %}(Resolved) {% endif %}{{ msg_text.summary }}",
                    "wrap": true
                  },
                ]
              }
            ]
          },
          {
            "type": "FactSet",
            "facts": [
              { "title": "Status", "value": "{{ msg_text.status }} {% if status=='firing' %}πŸ”₯{% elif status=='resolved' %}βœ…{% else %}⚠️{% endif %}" }
              {% if msg_text.name %} , { "title": "Alert", "value": "{{ msg_text.name }}" } {% endif %}
              {% if msg_text.instance %} , { "title": "In host", "value": "{{ msg_text.instance }}" } {% endif %}
              {% if msg_text.severity %} , { "title": "Severity", "value": "{{ msg_text.severity }} {% if msg_text.severity=='critical' %}❌{% elif msg_text.severity=='error' %}❗️{% elif msg_text.severity=='warning' %}⚠️{% elif msg_text.severity=='info' %}ℹ️{% else %}❓{% endif %}" } {% endif %}
              {% if msg_text.description %} , { "title": "Description", "value": "{{ msg_text.description }}" } {% endif %}
              {% if msg_text.extra_labels %}
                {% for key in msg_text.extra_labels %}
                  , { "title": "{{ key }}", "value": "{{ msg_text.extra_labels[key] }}" }
                {% endfor %}
              {% endif %}
              {% if msg_text.extra_annotations %}
                {% for key in msg_text.extra_annotations %}
                  , { "title": "{{ key }}", "value": "{{ msg_text.extra_annotations[key] }}" }
                {% endfor %}
              {% endif %}
            ]
          }
        ]
        {% if msg_text.runbook_url %}
        ,
        "actions": [
            {
                "type": "Action.OpenUrl",
                "title": "View details",
                "url": "{{ msg_text.runbook_url }}"
            }
        ]
        {% endif %}
      }
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions