Bot crash after a successfull query

Hi guys, I have a problem with my bot. I connected it with a DB for a simple query. The query’s purpose is to show a casual result after matching 2 slots (rasa) with 2 fields of table in db. The problem is that in the terminal where I run “rasa run actions”, I see the occurence found , while in the terminal where I run “rasa shell” the bot crashes for asyncio.exceptions.TimeoutError :

If I stop the terminal with CTRL+C , an other message appears:

for message_part in text.strip().split("\n\n"):

AttributeError: ‘list’ object has no attribute ‘strip’

Can you help me, please?

This is the code:

class ActionMatchEvents(Action):
    def name(self):
        return "action_match_events" # Be careful, did you mean action_fetch_data?
    def run(self, dispatcher, tracker, domain):
        luogo = tracker.get_slot("luogo")
        preferenza = tracker.get_slot("preferenza")

        mydb = mysql.connector.connect(
        host="localhost", 
        user="root", 
        password="", 
        database="itourist")

        mycursor = mydb.cursor() 
        sql = f'SELECT Risultato FROM itourist.riscontro WHERE Luogo="{luogo}" AND Preferenza="{preferenza}" ORDER BY RAND() LIMIT 1;'

        try:
            #Execute the SQL Query
            mycursor.execute(sql) 
            #result = mycursor.query(sql)
            result = mycursor.fetchall()

            #Now print fetched data
            print("OCCURRENCE FOUND")
            print(result)
            dispatcher.utter_message(text=result)
            return []
            

        except:
            dispatcher.utter_message("Error : Unable to fetch data.")
            return []

                ```

SOLVED: if you are interested about this problem , I changed:

dispatcher.utter_message(text=result)

to

dispatcher.utter_message(text=str(result))
1 Like

When you do print(result), you get [('Museo 1 Napoli',)], a list of a single tuple of a single string.

If you do str(result), you will get [('Museo 1 Napoli',)]. To get Museo 1 Napoli instead, do result[0][0]:

dispatcher.utter_message(text=result[0][0])

thank you so much Chris, the result is so clean now! Just a question: How can I verify with an “if…else” the content of “result”? I wish to show throught dispatcher.utter_message :

  1. the result, if the query produce an occurence
  2. a message like “No occurrence found” if the query doesn’t produce it

Solved in this way:

mydb = mysql.connector.connect(
        host="localhost", 
        user="root", 
        password="", 
        database="itourist")

        mycursor = mydb.cursor() 
        sql = f'SELECT Risultato FROM itourist.riscontro WHERE Luogo="{luogo}" AND Preferenza="{preferenza}" ORDER BY RAND() LIMIT 1;'

        try:
            #Eseguo la query
            mycursor.execute(sql) 
            #Cerco l'occorrenza
            result = mycursor.fetchone()
            #Il bot comunica l'occorrenza all'utente
            if (result==None):
                dispatcher.utter_message("Purtroppo non è stata trovata alcuna occorrenza.")
                mycursor.close()
                mydb.close()
                return []    
            else:
                dispatcher.utter_message("Ciò che potrebbe fare al caso tuo è:")
                #dispatcher.utter_message(text=result[0][0])
                dispatcher.utter_message(result[0])
                mycursor.close()
                mydb.close()
                return []

thank a lor for the support!

1 Like

Hi Chris! Sorry for disturbing you and for the off topic. Could you help me in this thread Question about Rasa and Mirador - Rasa Open Source - Rasa Community Forum ? I ask you because you were very helpfull for my previous doubts