Please any one help me on this, I want to store my user email id and phone number in mysql database
this is my Actions.py file
This files contains your custom actions which can be used to run
custom Python code.
See this guide on how to implement these action:
Custom Actions
This is a simple example for a custom action which utters “Hello World!”
from typing import Dict, Text, Any, List, Union
from rasa_sdk import Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormAction
class UserInfoForm(FormAction):
"""Example of a custom form action."""
def name(self) -> Text:
"""Unique identifier of the form."""
return "userInfo_form"
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
"""A list of required slots that the form has to fill."""
return ["name", "email", "mobile"]
def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
"""A dictionary to map required slots to
- an extracted entity
- intent: value pairs
- a whole message
or a list of them, where a first match will be picked."""
return {
"name": self.from_entity(entity="Name"),
"email": self.from_entity(entity="emailID"),
"mobile": self.from_entity(entity="phoneNumber")
}
def request_next_slot(self, dispatcher, tracker, domain):
for slot in self.required_slots(tracker):
if self._should_request_slot(tracker, slot):
if slot == 'name':
dispatcher.utter_template('utter_ask_email', tracker)
if slot == 'email':
dispatcher.utter_template('utter_ask_mobile', tracker)
if slot == 'mobile':
dispatcher.utter_template('utter_submit', tracker)
return [SlotSet(REQUESTED_SLOT, slot)]
return None
def validate(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict]:
slot_to_fill = tracker.get_slot(REQUESTED_SLOT)
if slot_to_fill == 'name':
return [SlotSet(slot_to_fill, tracker.latest_message['text'])]
if slot_to_fill == 'email':
return [SlotSet(slot_to_fill, tracker.latest_message['text'])]
if slot_to_fill == 'mobile':
return [SlotSet(slot_to_fill, tracker.latest_message['text'])]
else:
slot_values = self.extract_other_slots(dispatcher, tracker, domain)
if slot_to_fill:
slot_values.update(self.extract_requested_slot(dispatcher, tracker, domain))
if not slot_values:
raise ActionExecutionRejection(
self.name(),
f"Failed to extract slot {slot_to_fill} with action {self.name()}",
)
logger.debug(f"Validating extracted slots: {slot_values}")
return self.validate_slots(slot_values, dispatcher, tracker, domain)
def insert(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict]:
db = mysql.connector.connect(
host = "localhost",
user ="root",
passwd = "mauvetix1@1",
database ="rasabot"
)
cursor = db.cursor()
userName = tracker.get_slot('name')
userEmail = tracker.get_slot('email')
userMobile = tracker.get_slot('mobile')
query = 'INSERT INTO uesrdetails(u_name, u_emailId, u_mobileNumber) VALUES ("{0}","{1}","{2}");'.format(userName,userEmail,userMobile)
cursor.execute(query)
db.commit()
print(cursor.rowcount, "record inserted.")
This is my story path
stories:
-
story: userinfo
steps:
-
intent: request_personalInfo
-
action: userInfo_form
-
active_loop: userInfo_form
-
active_loop: null
-
action: utter_submit
-
this is my slot setting
slots:
name:
type: text
auto_fill: false
influence_conversation: false
email:
type: text
auto_fill: false
influence_conversation: false
mobile:
type: any
auto_fill: false
influence_conversation: false
requested_slot:
type: text
influence_conversation: false
This is my form
forms:
userInfo_form:
name:
- entity: Name
type: from_entity
email:
- entity: emailID
type: from_entity
mobile:
- entity: phoneNumber
type: from_entity
i can able to get those values in utter_submit
utter_submit:
- text: Thanks for your valuable time. {name} {email}, {mobile}
i am getting the appropriate response as well, but it was not stored in database