Rasa MySQL data error

Hi could someone help me in the code for verifying data coming from the chatbot user and checking whether that specific data is in the My SQL database eg checking whether the name the user has typed is present in the MySQL database or not. Here is the code i have written , this code works perfectly in an ordinary python program but it does not work as expected in the rasa custom actions file could anyone help me in modifying this code

If it works normally, it should work in a Custom Action as well.

Please provide what the error or wrong output is, as well as the Action logs.


Anyway, you have an error here:

In the mycusror.execute() function, you have not specified the table name in your query.

You did SELECT * FROM instead of SELECT * FROM <table_name>.


Another error at the same line:

You are using f-strings and .format() at the same time.

Either do f"select * from table where name = {mod_name}"

or "select * from table where name = {}".format(mod_name).

2 Likes

Hi yes so i saw my mistake there but i got this again

Hi @ChrisRahme I am also getting this error at the same time 2021-09-03 18:30:55 ERROR asyncio - Task exception was never retrieved future: <Task finished name=‘Task-2’ coro=<configure_app..run_cmdline_io() done, defined at C:\Users\Syed Mohammed Bilal\anaconda3\envs\rasa_install_demo\lib\site-packages\rasa\core\run.py:131> exception=TimeoutError()> Traceback (most recent call last): File “C:\Users\Syed Mohammed Bilal\anaconda3\envs\rasa_install_demo\lib\site-packages\rasa\core\run.py”, line 135, in run_cmdline_io await console.record_messages( File “C:\Users\Syed Mohammed Bilal\anaconda3\envs\rasa_install_demo\lib\site-packages\rasa\core\channels\console.py”, line 182, in record_messages async for response in bot_responses: File “C:\Users\Syed Mohammed Bilal\anaconda3\envs\rasa_install_demo\lib\site-packages\rasa\core\channels\console.py”, line 137, in _send_message_receive_stream async for line in resp.content: File “C:\Users\Syed Mohammed Bilal\anaconda3\envs\rasa_install_demo\lib\site-packages\aiohttp\streams.py”, line 39, in anext rv = await self.read_func() File “C:\Users\Syed Mohammed Bilal\anaconda3\envs\rasa_install_demo\lib\site-packages\aiohttp\streams.py”, line 338, in readline await self._wait(“readline”) File “C:\Users\Syed Mohammed Bilal\anaconda3\envs\rasa_install_demo\lib\site-packages\aiohttp\streams.py”, line 306, in _wait await waiter File “C:\Users\Syed Mohammed Bilal\anaconda3\envs\rasa_install_demo\lib\site-packages\aiohttp\helpers.py”, line 656, in exit raise asyncio.TimeoutError from None asyncio.exceptions.TimeoutError

@SyedBilalHasan share the screenshot of this error or update above post please.

Did you start the Action Server? If yes, see its logs.

I saw the logs but i am not able to understand anything in them could you please help me in this

I also saw this error

Looks like you’re sending a list as a message instead of a text.

Can you show your whole Custom Action? Try to paste it as text, not image. And format it with 3 backticks (```) before and after the code so it looks readable

```
like this
```

    def name(self) -> Text:
        return "action_hello_world"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        logger.debug("Some log message")
        name = tracker.get_slot("name")
        mod_name = "'"+ name +"'"
        mydb = mysql.connector.connect(host="localhost", user="root", passwd="b1i2l3a4l5", database="Students")
        mycursor = mydb.cursor()
        mycursor.execute(f"select * from student_info where name = {mod_name}")
        for i in mycursor:
            print(i)
            if name in i:
                message = "Yes he is there"
            else:
                message = "No he is not"

        dispatcher.utter_message(text=message)

        return []

    def name(self) -> Text:
        return "action_Gr_number"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        logger.debug("Some log message")
        name = tracker.get_slot("name")
        mod_name = "'"+ name +"'"
        mydb = mysql.connector.connect(host="localhost", user="root", passwd="b1i2l3a4l5", database="Students")
        mycursor = mydb.cursor()
        mod_name = "'" + name + "'"
        mycursor.execute(f"select name from student_info where name = {mod_name} ")
        for i in mycursor:
            print(i)
            if name in i:
                mycursor.execute(f"select GrNumber from student_info where name = {mod_name}")
                for m in mycursor:
                    message = m
            elif not (name in i):
                message = "nope"

        dispatcher.utter_message(text=message)

        return []

This is my second custom action where the error i appearing

Do print(message) and see if it is a list, which it probably is because the error said so.

I think it is if I remember correctly. In you case I think it’s a list of one element that represents GrNumber.

So try message = m[0] instead of message = m.

So @ChrisRahme your right about message being a list although now that i put message = m[0] it prints the response in the terminal where my server is running not in the rasa shell , here is a pic for the issue

print() will print in the Action terminal. utter_message() will be printed by the bot.

I told you to print above so that you can see in the action logs, since print() can print anything, and utter_message() can only print text.

i tried this code but the error still persists could you please tell me where exactly should put this code . There might be a problem with that

What’s the error here?

In the picture above, which line represents print(message)?

Looking at your code again, you haven’t defined message in the same block as utter_message(). You should also close your connection and your cursor. Does this work now? If no, show me the Action logs after you execute it.

def run(self, dispatcher, tracker, domain):
    mod_name = "'" + tracker.get_slot("name") + "'"
    message = "nope"

    mydb = mysql.connector.connect(host="localhost", user="root", passwd="b1i2l3a4l5", database="Students")
    mycursor = mydb.cursor()
    mycursor.execute(f"SELECT name FROM student_info WHERE name = {mod_name}")

    for i in mycursor:
        if name in i:
            mycursor.execute(f"SELECT GrNumber FROM student_info WHERE name = {mod_name}")
            for m in mycursor:
                message = m[0]
                print(i)
                break
            break

    mycursor.close()
    mydb.close()

    dispatcher.utter_message(text=message)

    return []

So the error is that the details which i asked for are being printed in the terminal where my rasa server is running and not in the rasa shell Here are the pictures of it

This is the rasa shell

This is the terminal where my server is running

You haven’t defined message in the same block as utter_message(), so it cannot find it.

Please try my code above and tell me if it works.

By the way, I don’t see the point of the first query. Why not do this?

def run(self, dispatcher, tracker, domain):
    mod_name = "'" + tracker.get_slot("name") + "'"
    message = "nope"

    mydb = mysql.connector.connect(host="localhost", user="root", passwd="b1i2l3a4l5", database="Students")
    mycursor = mydb.cursor()
    mycursor.execute(f"SELECT GrNumber FROM student_info WHERE name = {mod_name}")

    for m in mycursor:
        message = m[0]
        break

    mycursor.close()
    mydb.close()

    dispatcher.utter_message(text=message)

Oh thanks for the code i would try this its shorter than mine