# Buggy code
def interpolate_text(template: Text, values: Dict[Text, Text]) -> Text:
    # 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


# Traceback (TypeError)
Encountered an exception while running action 'utter_sifre'. Bot will continue, but the actions events are lost. Please check the logs of your action server for more information.
2020-03-20 07:37:14 DEBUG    rasa.core.processor  - expected string or bytes-like object
Traceback (most recent call last):
File "/build/lib/python3.6/site-packages/rasa/core/processor.py", line 564, in _run_action
events = await action.run(output_channel, nlg, tracker, self.domain)
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/actions/action.py", line 253, in run
message = await nlg.generate(self.template_name, tracker, output_channel.name())
File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
return self.gen.send(None)
File "/build/lib/python3.6/site-packages/rasa/core/nlg/template.py", line 77, in generate
template_name, filled_slots, output_channel, **kwargs
File "/build/lib/python3.6/site-packages/rasa/core/nlg/template.py", line 93, in generate_from_slots
return self._fill_template(r, filled_slots, **kwargs)
File "/build/lib/python3.6/site-packages/rasa/core/nlg/template.py", line 119, in _fill_template
template[key] = interpolate(template[key], template_vars)
File "/build/lib/python3.6/site-packages/rasa/core/nlg/interpolator.py", line 51, in interpolate
template[k] = [interpolate(i, values) for i in v]
File "/build/lib/python3.6/site-packages/rasa/core/nlg/interpolator.py", line 51, in <listcomp>
template[k] = [interpolate(i, values) for i in v]
File "/build/lib/python3.6/site-packages/rasa/core/nlg/interpolator.py", line 53, in interpolate
template[k] = interpolate_text(v, values)
File "/build/lib/python3.6/site-packages/rasa/core/nlg/interpolator.py", line 17, in interpolate_text
text = re.sub(r"{([^\n{}]+?)}", r"{0[\1]}", template)
File "/usr/local/lib/python3.6/re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object
2020-03-20 07:37:14 DEBUG    rasa.core.processor  - Action 'utter_sifre' ended with events '[]'.

# Fixed code
def interpolate_text(template: Text, values: Dict[Text, Text]) -> Text:
    # 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]}", str(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