class ActionFetchQuota(Action):
def name(self) -> Text:
return 'action_fetch_quota'
def run(self, dispatcher, tracker, domain):
username = tracker.get_slot('username')
if username:
results = None
try:
db = DatabaseConnection('localhost', 'rasa', 'root', 'root')
results = db.query("SELECT Quota, Consumption, Speed "
"FROM `user_info` INNER JOIN `consumption` "
"ON `user_info`.`ID` = `consumption`.`UserID` "
f"WHERE username = '{username}'")
db.disconnect()
except Exception as e:
dispatcher.utter_message('Sorry, I couldn\'t connect to the database.')
return [SlotSet('username', None)]
if len(results) != 1:
dispatcher.utter_message(f'Sorry, {username} is not a registered username.')
return [SlotSet('username', None)]
try:
quota, consumption, speed = results[0]
ratio = consumption/quota * 100
dispatcher.utter_message(f'You spent {consumption} GB ({ratio}%) of your {quota} GB quota for this month.')
except Exception as e:
dispatcher.utter_message('Sorry, there was an error. Please try again')
return [SlotSet('username', None)]
else: # Not logged in
dispatcher.utter_message('You are not logged in. Do you want to log in?')
return []