Database (MySQL) conncectivity for custom actions

Rasa_NLU version: 0.14.1 Rasa_Core version: 0.13.0

I am trying to fetch data for user queries using custom actions, but it is not working.

Below is my custom action:

class ActionDB(Action): def name(self): return ‘action_db’

def run(self, dispatcher, tracker, domain):
	import MySQLdb
	db = MySQLdb.connect("localhost","root","Abcd_1234","DBforChatbot")
	cursor = db.cursor()
	PersonID = tracker.get_slot('personid')
	q = "select * from Persons where PersonID='{0}' limit 1".format(PersonID)
	result = db.query(q)
	return [SlotSet("matches", result if result is not None else [])]

and I am getting error as below:

Failed to run custom action ‘action_db’. Action server responded with a non 200 status code of 404. Make sure your action server properly runs actions and returns a 200 once the action is executed. Error: 404 Client Error: NOT FOUND for url: http://localhost:5055/webhook/ 2019-02-28 13:03:08 ERROR rasa_core.processor - Encountered an exception while running action ‘action_db’. Bot will continue, but the actions events are lost. Make sure to fix the exception in your custom code. 2019-02-28 13:03:08 DEBUG rasa_core.processor - Failed to execute custom action.

2 Likes

@Ujjwalsas9 run “python -m rasa_core_sdk.endpoint --actions actions” directory where actions.py is located

Already tried it. It is not working. Thanks

can you share yml and actions.py

action.py

from future import absolute_import from future import division from future import unicode_literals

from rasa_core_sdk import Action from rasa_core_sdk.events import SlotSet

class ActionDB(Action): def name(self): return ‘action_db’

    def run(self, dispatcher, tracker, domain):
	import MySQLdb
	db = MySQLdb.connect("localhost","root","Abcd_1234","DBforChatbot")
	cursor = db.cursor()
	PersonID = tracker.get_slot('personid')
	q = "select * from Persons where PersonID='{0}' limit 1".format(PersonID)
	result = db.query(q)
	return [SlotSet("matches", result if result is not None else [])]

testbot_domain.yml

slots: personid: type: text stockid: type: text

intents:

  • greet
  • bye
  • affirmative
  • negative
  • personid
  • stockid
  • inform

entities:

  • personid
  • stockid

templates: utter_greet: - ‘Hello! How can I help?’ utter_goodbye: - ‘Talk to you later.’ - ‘Bye bye :(’ utter_ask_personid: - ‘Tell me your id please’ - ‘May I know your id please?’ utter_ask_stock: - ‘Tell me the stock name please’ - ‘Could you please tell me the stock name?’

actions:

  • utter_greet
  • utter_goodbye
  • utter_ask_stock
  • utter_ask_personid
  • action_db

Sorry, I was using online markup editor but it did not work well. So the format of the code is pretty much messed up.

@Ujjwalsas9 try replacing “result = db.query(q)” with “result = cursor.execute(q)” if it doesn’t work try to connect to db using python script. As per my knowledge you are having issue with the db. Make sure you have installed MySQL-python and MySQL-python-connector packages

@mohan Thanks, I tried it but it is not working. Below is the code which is working on python. But when I am trying to replicate the same with rasa custom actions, it is not working. I think, I am doing something wrong in actions code. Please let me know if you could find the error in my actions code. Thanks for your help.

@mohan It does not work. Anyways, thanks.

Problem loading actions on 0.11.2 might help

Surround your code with ‘try catch’ then print the exception message. It will help you find more info.

try:
	# your code here
except Exception as exc:
	print(str(exc))

Thanks will try it.

I am getting an error as below:

2019-03-07 17:24:16 ERROR rasa_core.actions.action - Failed to run custom action ‘action_db’. Action server responded with a non 200 status code of 404. Make sure your action server properly runs actions and returns a 200 once the action is executed. Error: 404 Client Error: NOT FOUND for url: http://localhost:5055/webhook/

2019-03-07 17:24:16 ERROR rasa_core.processor - Encountered an exception while running action ‘action_db’. Bot will continue, but the actions events are lost. Make sure to fix the exception in your custom code.

2019-03-07 17:24:16 DEBUG rasa_core.processor - Failed to execute custom action.

Any ideas on how to get rid of it. FYI: my database is running on port 3306, Rasa action server on 5005 and rasa endpoint is showing 5505.

Let’s try changing your class name from “ActionDB” to “ActionDb”.

Thanks, but it does not help.

i am also facing same issue for custom response

Hi @nitz, try the following:

  1. Check if you have this in your credentials.py file.

  1. Check if you are running your action server by “rasa run actions”.

  2. Check if you have added your custom action name to the domain file under “actions”

image

1 Like

Thx i will try it , but it will be helpful if u share ur ation.py file.

Sure @nitz Here is the file : actions.py (9.8 KB)

@varunsapre10 can u please help me with this