# Buggy code
def interpolate_text(template, values):
    if isinstance(template, str):
        # transforming template tags from
        # "{tag_name}" to "{0[tag_name]}"
        # as described here:
        # https://stackoverflow.com/questions/7934620/python-dots-in-the-name-of-variable-in-a-format-string#comment9695339_7934969
        # assuming that slot_name do not contain newline character here
        try:
            text = re.sub(r"{([^\n]+?)}", r"{0[\1]}", template)
            return text.format(values)
        except KeyError as e:
            logger.exception(
                "Failed to fill utterance template '{}'. "
                "Tried to replace '{}' but could not find "
                "a value for it. There is no slot with this "
                "name nor did you pass the value explicitly "
                "when calling the template. Return template "
                "without filling the template. "
                "".format(template, e.args[0])
            )
            return template
    return template


# Traceback (KeyError)
2019-07-01 12:15:57 ERROR    rasa.core.nlg.template  - Failed to fill utterance template '{'text': '`system`:bot-terminate `body`:Payment of R{amount} for your order was received successfully. {{Your order number is #{order_id}}}.'}'. Tried to replace '{Your order number is #{order_id' but could not find a value for it. There is no slot with this name nor did you pass the value explicitly when calling the template. Return template without filling the template.
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/rasa/core/nlg/template.py", line 115, in _fill_template_text
template["text"] = text.format(template_vars)
KeyError: '{Your order number is #{order_id'

# Fixed code
def interpolate_text(template, values):
    if isinstance(template, str):
        # transforming template tags from
        # "{tag_name}" to "{0[tag_name]}"
        # as described here:
        # https://stackoverflow.com/questions/7934620/python-dots-in-the-name-of-variable-in-a-format-string#comment9695339_7934969
        # black list character and make sure to not to allow
        # (a) newline in slot name
        # (b) { or } in slot name
        try:
            text = re.sub(r"{([^\n{}]+?)}", r"{0[\1]}", template)
            text = text.format(values)
            if "0[" in text:
                # regex replaced tag but format did not replace
                # likely cause would be that tag name was enclosed
                # in double curly and format func simply escaped it.
                # we don't want to return {0[SLOTNAME]} thus
                # restoring original value with { being escaped.
                return template.format({})

            return text
        except KeyError as e:
            logger.exception(
                "Failed to fill utterance template '{}'. "
                "Tried to replace '{}' but could not find "
                "a value for it. There is no slot with this "
                "name nor did you pass the value explicitly "
                "when calling the template. Return template "
                "without filling the template. "
                "".format(template, e.args[0])
            )
            return template
    return template