Can someone help me to create buttons with the result of MySQL query in custom actions

Hey everyone. I’m trying to query MySQL database and with the result, I’m creating buttons with title and payloads.

However, the buttons are not showing up. I am using : dispatcher.utter_message(“choose one:”,buttons)

I later found that the query result is null, hence I guess buttons are also null. Correct me if I’m wrong.

But the same query is executed successfully in MySQL workbench.

Can someone tell me where I’m going wrong?

Does it have something to do with async operations??

Hi @PraneethVasarla,

Welcome to Rasa Community.

Can you post your actions.py.

Thanks for the response @MuraliChandran14 :hugs:. I figured out the previous problem. I was mistakenly using the test database (silly of me, I know :man_facepalming:) in which there was no record for the query. I fixed that but now I got an error saying:

2020-06-08 12:31:02 ERROR asyncio - Task exception was never retrieved future: <Task finished coro=<configure_app..run_cmdline_io() done, defined at c:\users\praneeth\appdata\local\programs\python\python36\lib\site-packages\rasa\core\run.py:128> exception=TypeError(‘must be str, not list’,)> Traceback (most recent call last): File “c:\users\praneeth\appdata\local\programs\python\python36\lib\site-packages\rasa\core\run.py”, line 134, in run_cmdline_io sender_id=conversation_id, File “c:\users\praneeth\appdata\local\programs\python\python36\lib\site-packages\rasa\core\channels\console.py”, line 152, in record_messages button_question = print_bot_output(response) File “c:\users\praneeth\appdata\local\programs\python\python36\lib\site-packages\rasa\core\channels\console.py”, line 34, in print_bot_output cli_utils.print_color("Image: " + message.get(“image”), color=color) TypeError: must be str, not list

This is my code in actions.py

import mysql.connector as sql

from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher

database = sql.connect(

host = hostname,

user = username,

passwd = password,

database = database

)

mycursor = database.cursor()

query = <MySQL query>
mycursor.execute(query)

result = mycursor.fetchall()

"""

Some code here to create the buttons dictionary from fetched result as "mybuttons"

the structure of mybuutons at the end of code is:

[{'title': title_name, 'payload': '/inform{slotname: slotvalue}'}, {'title2': title_name2, 'payload': '/inform{slotname2: slotvalue2}'}, {'title3': title_name3, 'payload': '/inform{slotname3: slotvalue3}'}, {'title4': title_name4, 'payload': '/inform{slotname4: slotvalue4}'}]     

I tried printing this "mybuttons" variable and it runs as expected.
the next piece of code is as below


"""

dispatcher.utter_message("These are the eligible orders for return: ",mybuttons)

 return []

Where am I doing this wrong? The buttons argument in utter_message should be a list but the error says must be a str not a list.

Can you please help me with a solution or at least point me to the right direction? Thanks @MuraliChandran14

@PraneethVasarla

change it to:

dispatcher.utter_message(text="These are the eligible orders for return: ", buttons=mybuttons)

2 Likes

It worked!! Thanks @MuraliChandran14 :blush::blush:

you are welcome :slight_smile:

when i execute my sql query of two columns it appear with commas…and i dont want the comma to appear between this results ,i just want a space and how can i do that? that happens in buttons…have you encounter that before