Jinja snippets to render a conversation transcript produced by the chat history v3 webhook
Often we want to pass a chat history as a part of a support ticket. For that, we do have a Certainly Get Conversation History v3 Webhook.
Below are examples of a Jinja code to produce HTML and text versions of the chat history.
HTML version
This version of the chat transcript has all users' messages highlighted in bold making it easier to read. When a human clicks on the suggested reply button, its text is enclosed in square brackets The transcript also includes cards shown during the conversation.
HTML version looks like this:
Use the Jinja code below in the Custom Variable mapping of the Webhook:
{# Chat history as HTML -#}
{% for m in messages -%}
{% if m.get("text") and m.text != "…"-%}
{% set txt = m.text|striptags -%}
{% set txt = ("["~txt~"]") if m.isSuggestedReply else txt -%}
{% set txt = "<b>"~txt~"</b>" if "HUMAN" in m.from.upper() else txt -%}
{{ m.from.upper() }}: {{ txt }}<br>
{% endif -%}{# print message -#}
{% for c in m.get("cards", []) -%}
{% set msg="Bot card" ~ loop.index ~ ": " -%}
{{ (msg ~ c.title) if c.title.strip() }}<br>
{{ (msg ~ c.text) if c.text.strip() }}<br>
{% for b in c.buttons -%}
{{ msg }}{% if b.type in ["web_url_tab","postback"] %}[{{ b.title }}] {{ b.options.newtab_url }}<br>
{% endif -%}
{% endfor -%}{# buttons -#}
{% endfor -%}{# cards -#}
{% endfor -%}
Text version
This version includes cards' titles and buttons and looks like:
Use the Jinja code below in the Custom Variable mapping of the Webhook:
{# Chat history as Text -#}
{% for m in messages -%}
{% if m.get("text") and m.text != "…"-%}
{% set txt = m.text|striptags|safe -%}
{{ m.from |upper }}: {{ "["~txt~"]" if m.isSuggestedReply else txt }}
{% endif -%}{# print message -#}
{% for c in m.get("cards", []) -%}
{% set msg="Bot card" ~ loop.index ~ ": " -%}
{{ msg ~ c.title if c.title.strip() }} {{ msg ~ c.text if c.text.strip() }}
{% for b in c.buttons -%}
{{ msg }}{% if b.type in ["web_url_tab","postback"] %}[{{ b.title }}] {{ b.options.newtab_url }}
{% endif -%}
{% endfor -%}{# buttons -#}
{% endfor -%}{# cards -#}
{% endfor -%}