Action.py text not displaying

[Split from Multiple py files in actions]

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 = 'SELECT Risultato FROM itourist.riscontro WHERE Luogo="+luogo+" AND Preferenza="+preferenza+" ORDER BY RAND() LIMIT 1;'.format(luogo, preferenza)

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

            return dispatcher.utter_message(text=result)
            

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

when I run this code, the bot doesn t show me the text of “result”, and in the terminal where I run rasa actions appear 2 closing parenthesis " [] "

How can I solve this?

This is wrong. Please read the docs to see how to do it: Dispatcher

dispatcher.utter_message(text=result)
return []

Do the same in the except block of course.

Thank you so much for the reply, I modified those rows but I can’t see the text:

Both bot and terminal don’t show the text. The db’s field that I’m extracting is a VARCHAR . This could be the cause of the problem?

Can you do print(result) and see what appears? I think it doesn’t return anything because you probably made an error here:

sql = 'SELECT Risultato FROM itourist.riscontro WHERE Luogo="+luogo+" AND Preferenza="+preferenza+" ORDER BY RAND() LIMIT 1;'.format(luogo, preferenza)

The query will literally search for the words “luogo” and “preferenza”.

You probably meant this:

sql = 'SELECT Risultato FROM itourist.riscontro WHERE Luogo="'+luogo+'" AND Preferenza="'+preferenza+'" ORDER BY RAND() LIMIT 1;'

I recommend using f-strings for readability:

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

Yes of course, the terminal where I run the command “rasa run actions” show me 2 closing parenthesis :

OCCURRENCE FOUND ---> this is a print for test
[]         <----- this is the result of    print(result)

See, your query returns nothing.

When in doubt, do print(sql) and try it on your actual SQL server :slight_smile:

thank you so much Chris, you helped me and I don’t know how to thank you <3 Now in terminal I can see the text while bot crashed for asyncio.exceptions.TimeoutError

I’ll create new thread for this , still thank you!

1 Like

Glad to be of help and thanks for posting in a new thread :slight_smile: