Problem with multiple dispatcher.utter_message in loop - Getting Started with Rasa / Tutorials, Resources & Videos - Rasa Community Forum
👋 Introducing the Rasa Playground
Problem with multiple dispatcher.utter_message in loop
I am having trouble sending multiple messages via dispatcher.utter_message(). I am sending requests to an API then the results I get I show them in a loop via dispatcher.utter_message(). The problem is that it only displays the first coincidence or the first element in the request result. If I have more than one coincidence I need to show it. Here is my code:
class ActionBuscarVacante(Action):
def name(self):
return "action_buscar_vacante"
def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
ubicacion = tracker.get_slot("ubicacion")
puesto = tracker.get_slot("puesto")
if ubicacion and puesto:
r = requests.get(url=url+"?filterByFormula=AND(%7BReg%7D%3D%22"+ubicacion+"%22%2C%7BStatus%7D%3D%22Activa%22%2C%7BPuesto%7D%3D%22"+puesto+"%22)", headers=h)
elif ubicacion and not puesto:
r = requests.get(url=url+"?filterByFormula=AND(%7BReg%7D%3D%22"+ubicacion+"%22%2C%7BStatus%7D%3D%22Activa%22)", headers=h)
elif puesto and not ubicacion:
r = requests.get(url=url+"?filterByFormula=AND(%7BPuesto%7D%3D%22"+puesto+"%22%2C%7BStatus%7D%3D%22Activa%22)", headers=h)
data = r.json()
rec = data['records']
if not rec:
dispatcher.utter_message(template="utter_no_encontrado")
return [AllSlotsReset()]
for i in range(len(rec)):
registro = rec[i]
f = registro['fields']
imagen = f['Imagen'][0]['url']
reg = ubicacion
info = f['info']
puesto = f['Puesto']
dispatcher.utter_message(
template="utter_info_vacante",
ubicacion=reg,
puesto=puesto,
informacion=info,
img=imagen,
)
return [FollowupAction('action_listen')]
Does this happen in Rasa X and Rasa Shell? Sometimes Rasa X fails to show messages but it always works well in Rasa Shell.
yes, in both
This is weird…
Are you sure it is going through the loop multiple times? You can put a print inside the loop and see it on the Action Server logs.
update, had to work around the loop, but now it only displays in Rasa shell, not in Rasa X
class ActionBuscarVacante(Action):
def name(self):
return "action_buscar_vacante"
def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
ubicacion = tracker.get_slot("ubicacion")
puesto = tracker.get_slot("puesto")
data = r.json()
rec = data['records']
if not rec:
dispatcher.utter_message(template="utter_no_encontrado")
return [AllSlotsReset()]
imagen = []
reg = []
info = []
puesto = []
for i in range(len(rec)):
registro = rec[i]
f = registro['fields']
imagen.append(f['Imagen'][0]['url'])
info.append(f['info'])
puesto.append(f['Puesto'])
reg.append(f['Reg'])
for i in range(len(reg)):
dispatcher.utter_message(
template="utter_info_vacante",
ubicacion=reg[i],
puesto=puesto[i],
informacion=info[i],
img=imagen[i]
)
return [FollowupAction('action_listen')]
Yeah Rasa X sometimes fails to print things in a loop… I have the same problem.