How to connect chatbot with Database (MySQL)

Hi ,

I have build a chatbot which is fetching the user queries response from a Json file but I want to connect my chatbot with Database(MySQL or any). like - if user asked any question then the response should be get from the database.

please help on this.

1 Like

In the Action class you can do whatever you want before sending the response using dispatcher.utter_message

you can query your database and get the response and then pass it to the dispatcher.

1 Like

You can do this using Custom actions. In file create an action like this:

from rasa_sdk import Action

class ActionUserOrder(Action):
    def name(self):
        return "action_response"

    def run(self, dispatcher, tracker, domain):
        q = "SELECT <reponse> FROM <table> where <user_question> LIKE <user asked question> "
       result = db.query(q)
       return []

Note: You need to install rasa_sdk using this command pip install rasa-sdk

and then use the custom action name action_response in your stories and don’t forget to add this custom action in your domain file.

To use custom actions you will need to run action server and you can run this by rasa run actions command and follow these docs for more details

1 Like

hey, where should I store the db in? Do I need store in the directory which contains other chatbot file like domain etc?

You’ll need to setup a database server.

how can i do that?