Hi everyone,
I have below code to fetch data from MySQL database in rasa, which is working fine. I want to show fetched data to user.
e.g. after fetching the data if found, it should show user that your details are:
What’s your question? Everything seems correct for me - except for the fact that you are not showing the message to the user.
If this is your error, then you should know print() prints to the Action Server console, while dispatcher.utter_message() prints the text (or others, check the docs) in the chat.
db_connect.py
def DataFetch(phone):
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="db"
)
mycursor = mydb.cursor()
sql = 'SELECT UserName, UserPhone, UserEmail from rasa_db.Users where UserPhone="{0}";'.format(phone)
try:
#Execute the SQL Query
mycursor.execute(sql)
results = mycursor.fetchall()
UserName = results[0][0]
UserEmail = results[0][2]
#Now return fetched data
return f"User Name: {UserName}, User Email: {UserEmail}"
except:
return "Error : Unable to fetch data."
actions.py
class ActionFetchData(Action):
def name(self):
return "action_response" # Be careful, did you mean action_fetch_data?
def run(self, dispatcher, tracker, domain):
message = DataFetch(tracker.get_slot("user_phone"))
dispatcher.utter_message(message) # or dispatcher.utter_message(text = message)
return []
@Bobby doesn’t my code work? Just use dispatcher.utter_message() instead of print().
To not have to pass dispatcher as argument to the DataFetch() function, I made it return the message instead, to be printed in the run() method. But you can also do this:
def DataFetch(phone, dispatcher):
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="db"
)
mycursor = mydb.cursor()
sql = 'SELECT UserName, UserPhone, UserEmail from rasa_db.Users where UserPhone="{0}";'.format(phone)
try:
#Execute the SQL Query
mycursor.execute(sql)
results = mycursor.fetchall()
UserName = results[0][0]
UserEmail = results[0][2]
#Now print fetched data
dispatcher.utter_message(f"User Name: {UserName}, User Email: {UserEmail}")
except:
dispatcher.utter_message("Error : Unable to fetch data.")
class ActionFetchData(Action):
def name(self):
return "action_response" # Be careful, did you mean action_fetch_data?
def run(self, dispatcher, tracker, domain):
DataFetch(tracker.get_slot("user_phone"), dispatcher)
return []
Querying the database for the phone number through the DataFetch() function. The first line (user_phone = ...) is useless though.
I suggest you check if the user has given their phone number as well, otherwise the query won’t work. Of course, it will print the error message, but I think being a bit more precise is better for the end user
class ActionFetchData(Action):
def name(self):
return "action_response" # Be careful, did you mean action_fetch_data?
def run(self, dispatcher, tracker, domain):
user_phone = tracker.get_slot("user_phone")
if user_phone: # user_phone is not None
dispatcher.utter_message(DataFetch(user_phone))
else: # user_phone is None
dispatcher.utter_message("Please give me your phone number.")
return []
except:
dispatcher.utter_message("No record found.")
#dispatcher.utter_message(response = "utter_want_appointment") ##ask user it he wants to book a new appointment
If number is not found, the bot is not saying that No record found.