How to display variables by dispatcher.utter_message()

Hi all,
I would like to know whether is possible to display the value of a variable by dispatcher.utter_message().
Below, my custom action:

class ActionRecap(Action):

def name(self) -> Text:
	return "action_recap"

def run(
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
) -> List[EventType]:

    pizza = tracker.get_slot("pizza_type")
    drinks = tracker.get_slot("drink_type")
    pizza_quantity = tracker.get_slot("pizza_quantity")
    drinks_quantity = tracker.get_slot("drinks_quantity")

    recap_pizza = zip(pizza_quantity, pizza)
    recap_drinks = zip(drinks_quantity, drinks)

    recap_text= "Recap: {recap_pizza}, {recap_drinks}"


    return []

Is it possible to output the values of the variables {recap_pizza}, {recap_drinks}?

It looks like you are trying to use f-formatting in Python. You can read up about it here:

So in your case, you probably need to do

recap_text= f"Recap: {recap_pizza}, {recap_drinks}"

I have tried, but it seems that we can not use f-formatting in dispatcher.utter_message()

Yes you can. Try using tuple(), list(), or dict():

recap_pizza = tuple(zip(pizza_quantity, pizza))
recap_drinks = tuple(zip(drinks_quantity, drinks))
recap_text = f"Recap: {recap_pizza}, {recap_drinks}"

Consider this:

a = ("John", "Charles", "Mike")
b = ("Jenny", "Christy", "Monica", "Vicky")

If you use tuple():

x = tuple(zip(a, b))
> (('John', 'Jenny'), ('Charles', 'Christy'), ('Mike', 'Monica'))

If you use list():

x = list(zip(a, b))
> [('John', 'Jenny'), ('Charles', 'Christy'), ('Mike', 'Monica')]

If you use dict():

x = dict(zip(a, b))
> {'John': 'Jenny', 'Charles': 'Christy', 'Mike': 'Monica'}

If you don’t use anything:

x = zip(a, b)
> <zip object at 0x2ac58cb9f0c8>

dispatcher.utter_message(text = ...) takes in a string. As long as you have a string, it works, no matter what method you used. You could also construct the string like this:

recap_text = "Recap: " + str(list(recap_pizza)) + ", " + str(list(recap_drinks))
dispatcher.utter_message(text = recap_text)
1 Like