Jinja snippets - Chat history

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 Webhook. By default, it produces a basic text transcript of the conversation.

Below are examples of an alternative Jinja code.

HTML version

This version of the chat transcript has all users' messages highlighted in bold making it easier to read. It also includes cards

 

HTML version looks like:

Use Jinja code below in the Custom Variable mapping of the Webhook:

{# Chat history as HTML -#}
{% for response in wh.response if response.message.actions -%}
{% for a in response.message.actions -%}
{% if a.get("text") and a.text != "…"-%}
{% set text = a.text|striptags -%} 
{% set text = ("["~text~"]") if a.isSuggestedReply else text -%}
{% set text = "<b>"~text~"</b>" if "HUMAN" in response.message.from.upper() else text -%}
{{ response.message.from.upper() }}: {{ text }}<br>
{% endif -%}{# print message -#}
{% if a.get("cards") -%}
{% for c in a.cards -%}
{% set msg="Bot card" ~ loop.index ~ ": " -%}
{% if c.title.strip() %}{{ msg }}{{ c.title }}<br>
{% endif -%}
{% if c.text.strip() %}{{ msg }}{{ c.text }}<br>
{% endif -%}
{% for b in c.buttons -%}
{{ msg }}{% if b.type in ["web_url_tab","postback"] %}[{{ b.title }}] {{ b.options.newtab_url }}<br>
{% endif -%}
{% endfor -%}
{% endfor -%}
{% endif -%}
{% endfor %}{% endfor %}

 

Text version

This version includes cards' titles and buttons and looks like:

Use Jinja code below in the Custom Variable mapping of the Webhook:

{% for response in wh.response if response.message.actions -%}
{% for a in response.message.actions -%}
{% if a.get("text") and a.text != "…"-%}
{{ response.message.from.upper() ~ ": " }} 
{%- if a.isSuggestedReply == True -%}
[{{ a.text|striptags|safe -}}]
{%- else -%} 
{{ a.text|striptags|safe -}}
{% endif -%}{# suggested reply #}
{% endif -%}{# print message -#}
{% if a.get("cards") -%}
{% for c in a.cards -%}
{% set msg="Bot card" ~ loop.index ~ ": " -%}
{% if c.title.strip() %}{{ msg }}{{ c.title }}
{% endif -%}
{% if c.text.strip() %}{{ msg }}{{ c.text }}
{% endif -%}
{% for b in c.buttons -%}
{{ msg }}{% if b.type in ["web_url_tab","postback"] %}[{{ b.title }}] {{ b.options.newtab_url }}
{% endif -%}
{% endfor -%}
{% endfor -%}
{% endif -%}
{% endfor %}{% endfor %}