@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.