Save Payload Value to List Slot - Getting Started with Rasa / Tutorials, Resources & Videos - Rasa Community Forum
👋 Introducing the Rasa Playground
Save Payload Value to List Slot
post by IsraaMohamedHamid on Apr 5, 2021
I am creating a restaurant bot in which the user is asked to pay or order more, when the user picks order more it repeats part of the story that displays the restaurant menu category and then chooses the dish he/she wants. My problem is that the button should set two slots; a list and a normal slot. This is my code:
######ACTION######
i=1
buttons = []
for restaurant_menu_category_en in all_restaurant_menu_categories_en:
title = str(i) + "- "+ restaurant_menu_category_en[1] +"\n"
payload = "/ask_chosen_restaurant_menu_category_en{\"chosen_restaurant_menu_category_en\":\"" + restaurant_menu_category_en[1] + "\",\"chosen_restaurant_menu_category_list_en\":\"" + tracker.get_slot('chosen_restaurant_menu_category_list_en').append(restaurant_menu_category_en[1]) + "}\"}"
i+=1
buttons.append({"title": title,"payload": payload})
dispatcher.utter_message(buttons=buttons)
######DOMAIN######
slots###
chosen_restaurant_menu_category_en:
type: text
influence_conversation: false
chosen_restaurant_menu_category_list_en:
type: list
initial_value: []
influence_conversation: true
#####STORY#####
- intent: ask_chosen_restaurant_menu_category_en entities:
- chosen_restaurant_menu_category_en: sandwich
- slot_was_set:
- chosen_restaurant_menu_category_en: sandwich
- slot_was_set:
- chosen_restaurant_menu_category_list_en:
My problem is that when I run the code I get the following
error: TypeError: can only concatenate str (not “NoneType”) to str
for this line: payload =
“/ask_chosen_restaurant_menu_category_en{"chosen_restaurant_menu_category_en":"” + restaurant_menu_category_en[1] + “","chosen_restaurant_menu_category_list_en":"” + tracker.get_slot(‘chosen_restaurant_menu_category_list_en’).append(restaurant_menu_category_en[1]) + “}"}”
How can I append the payload value to a list?
post by m.vielkind on Apr 5, 2021
Hi @IsraaMohamedHamid instead of handling this all at once could you do this with a custom action? In your payload you can set the value for chosen_restaurant_menu_category_en and then have a custom action to process the input to append to a list. This is probably the cleanest way.
I think the reason you’re getting your error is this segment is evaluating to None tracker.get_slot('chosen_restaurant_menu_category_list_en').append(restaurant_menu_category_en[1]).
A workaround would be to construct your payload first and then use json.dumps to attach it to your button. Something like this, but the custom action to process to the user response is probably cleaner.
new_list = tracker.get_slot('chosen_restaurant_menu_category_list_en').copy()
new_list.append(restaurant_menu_category_en[1])
d = {
"chosen_restaurant_menu_category_en": restaurant_menu_category_en[1],
"chosen_restaurant_menu_category_list_en": new_list
}
payload = f"/ask_chosen_restaurant_menu_category_en{json.dumps(d)}"
post by IsraaMohamedHamid on Apr 7, 2021
@m.vielkind thank you so much. I have managed to solve the problem by adding a new custom action: class ActionSaveCategoryToList_en(Action):
def name(self):
return 'action_save_category_to_list_en'
def run(self, dispatcher, tracker, domain):
chosen_category_list_en = tracker.get_slot('chosen_restaurant_menu_category_list_en')
##print(chosen_category_list_en) ####### Check to see if code prints correctly #######
current_chosen_category_en = tracker.get_slot('chosen_restaurant_menu_category_en')
##print(current_chosen_category_en) ####### Check to see if code prints correctly #######
chosen_category_list_en.append(current_chosen_category_en)
##print(chosen_category_list_en) ####### Check to see if code prints correctly #######
return [SlotSet("chosen_restaurant_menu_category_list_en", chosen_category_list_en)]
I just call it in my story after setting the slots and it works perfectly. I just wanted to make sure that there isn’t another way to write less code but since it’s not broken and all. Again thank you very much you’ve helped a lot. Sorry tI just have one last question and it happened more than once, when it comes to adding a number like quantity or phone number and I use the shell debugger to check it does not fill the slot, have you ever come accross a problem like this?