I am working on a rasa chatbot where the bot filters data from a database and returns the result based on user intent.
The user can ask 3 types of questions.
- question based on date
- question based on count
- other general questions
here is an example story
*greet
- utter_greet
- utter_question
*query_count{"Status":"pending"}
- slot{"Status":"pending"}
- utter_askUserId
*give_userId{"UserID":"1234"}
- slot{"UserID":"1234"}
- action_getName
- action_getDetails
from the above story, the user asks a count based question. at that point the latest intent was query_count
. But then the bot prompts for the UserID, and when the user gives the userID, the latest intent then becomes give_userId
.
My custom action works based on the intent provided. If the intent is query_count
run some code else if the intent is query_date
run some other code and so on.
I get the latest intent with this code tracker.latest_message['intent'].get('name')
. But according to my story, the latest intent is always give_userId
and hence the action never runs the way it should.
here is my actions.py
class ActionGetDetails(Action):
def name(self) -> Text:
return "action_getDetails"
def run(self, dispatcher, tracker, domain):
intent = []
if not tracker.latest_message['intent'].get('name') == 'give_userId':
intent.append(tracker.latest_message['intent'].get('name'))
user_id = tracker.get_slot('UserID')
status = tracker.get_slot('Status')
mode = tracker.get_slot('Mode')
dispatcher.utter_message(intent)
if intent[-1] == 'query_count':
query = 'SELECT COUNT(*) FROM sales WHERE UserID=? AND Status=?'
conn = sqlite3.connect("shipment.db")
cursor = conn.cursor()
cursor.execute(query, (user_id, status))
count_items = cursor.fetchall()[0][0]
msg = f"You have {count_items} {status} orders"
dispatcher.utter_message(msg)
elif intent[-1] == 'query_date':
if mode == 'deliver':
query = 'SELECT Delivery_date FROM sales WHERE UserID=?'
elif mode == 'ship':
query = 'SELECT Ship_date FROM sales WHERE UserID=?'
else:
query = 'SELECT Dispatch_date FROM sales WHERE UserID=?'
conn = sqlite3.connect("shipment.db")
cursor = conn.cursor()
cursor.execute(query, (user_id,))
items_date = cursor.fetchall()
msg = f"{items_date}"
dispatcher.utter_message(msg)
In this code the intent query_count
is not appended to the list as that question was asked before the action even runs. And since, I am not appending give_userId
to the list, the program returns a list index out of range
error as the list is empty
Is there anyway to append all the intents given by the user into the intent list
in my the custom action? Or is there a better way to run the above action?