# This files contains your custom actions which can be used to run
# custom Python code.
#
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/core/actions/#custom-actions/


# This is a simple example for a custom action which utters "Hello World!"

from typing import Any, Text, Dict, List

from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.events import SlotSet



import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  port="3306",
  database="aaditikc_rastm_paper"
)





class ActionFetchName(Action):

    def name(self) -> Text:
        return "action_fetch_name"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        
        id = tracker.get_slot("id")
        sql= "SELECT author_name FROM authors WHERE author_id = \""+ id +"\""
        print(sql)
        mycursor = mydb.cursor()
        mycursor.execute(sql)
        myresult = mycursor.fetchone()
        print(myresult[0])

        
        dispatcher.utter_message()

        return [SlotSet("name", myresult[0])]

