EASY DATABASE CONNECTIVITY WITH RASA CHATBOT 2 STORE THE COLLECTED INFORMATION

mysql rasa DATABASE
0
0

Data… Data… Data…

All around you are have data(stored at different locations like database, excel, json, etc) in one or another form that helps you with the easy-going lifestyle. It’s not just the data instead it’s a life-changing step that helps you in every aspect. Like in present scenario data is helping as well as controlling many of our days to day life tasks which makes our lives comfortable and luxurious. Not only the present but the data is helping us to predict and analyze the future which is like a miracle for us.

The first step towards understanding data is to gather it and to further process it in the way it is required. That’s what you are going to learn in this blog that how to gather the data and to further store it into the database.

In this blog I will show you that how you can collect the data from the user and further store in into the MYSQL database to understand it better.

In this blog you will learn,

1. How to collect the data from the user with Rasa chatbot

2. How to store each data into the MYSQL database.

So, this project is divided into two parts. First is to collect the data from the user and second, is to store the data to the MYSQL database.

Firstly, let’s start with collecting the data from the user using the rasa chatbot.

Data gathering from the user

To understand this better let’s understand the scenario first. So in this scenario, we are going to collect the user feedback for the training session that he/she has attended in the past along with various other details like first name, last name, etc. Also, to collect this information we are not going to complicate the project instead to make it more simple and professional we will be using the forms that make it really simple and easy to understand. Also to make it look like as per the industrial standards. So let’s make changes to the bot as per our scenario. After changes, your files in the project will look like,

nlu.md

## intent:FirstName
- My name is [ashish](firstN)
- you can call me [rahul](firstN)
- It's [vikas](firstN)
- [awakash](firstN) this side

## intent:LastName
- saini
- kumar
- singh
- prasad

## intent:request_details
- I want to fill the feedback form
- fill the feedback form- how to fill the feedback form
- what to procedure to give feedback

## intent:affirm
- yes
- indeed
- of course
- that sounds good
- correct

## intent:deny
- no
- never
- I don't think so
- don't like that
- no way
- not really

stories.md

## happy path
* request_details
- form_info
- form{"name":"form_info"}
- form{"name":null}
> Check_feedback_details

## affirm path
> Check_feedback_details
* affirm
- action_submit

## deny path
> Check_feedback_details
* deny
- utter_thanks

domain.yml

intents:
- LastName
- FirstName
- Feedback
- request_details
- affirm
- deny
entities:
- firstN
slots:
feedback:
type: unfeaturized
auto_fill: false
firstN:
type: unfeaturized
auto_fill: false
lastN:
type: unfeaturized
auto_fill: false
templates:
utter_ask_firstN:
- text: Please provide your first name.
utter_ask_lastN:
- text: Please provide your last name.
utter_ask_feedback:
- text: Please provide your feedback.
utter_submit:
- text: "These are the values that you provided say yes or no to confirm:\n First\
\ name: {Fname}\n Last Name: {Lname}\n Feedback: {fdbk}"
utter_thanks:
- text: Thanks for your valuable time.
actions:
- action_last_name
- action_feedback
- action_submit
- utter_thanks
forms:
- form_info

actions.py


from database_connectivity import DataUpdate
class ActionFirstName(Action):
def name(self) -> Text:
"""Unique identifier of the form"""
return "action_last_name"

def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
dispatcher.utter_message(template="utter_last_name")
return [SlotSet('firstN',tracker.latest_message['text'])]
class ActionFeedback(Action):
def name(self) -> Text:
"""Unique identifier of the form"""
return "action_feedback"

def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
dispatcher.utter_message(template="utter_feedback")
return [SlotSet('lastN',tracker.latest_message['text'])]


class ActionSubmit(Action):
def name(self) -> Text:
"""Unique identifier of the form"""
return "action_submit"

def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
DataUpdate(tracker.get_slot("firstN"),
tracker.get_slot("lastN"),tracker.get_slot("feedback"))
dispatcher.utter_message("Thanks for the valuable feedback. ")
return []

class ActionFormInfo(FormAction):
def name(self) -> Text:
"""Unique identifier of the form"""
return "form_info"

@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
"""A list of required slots that the form has to fill"""
return ["firstN", "lastN","feedback"]
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 {
"firstN": [ self.from_entity( entity="firstN",
intent="FirstName"), ],
"lastN": [self.from_text()],
"feedback": [self.from_text()], }
def submit( self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any], ) -> List[Dict]:
"""Define what the form has to do after all required slots are filled""" # utter submit template

dispatcher.utter_message(template="utter_submit",
Fname=tracker.get_slot("firstN"),
Lname=tracker.get_slot("lastN"),
fdbk=tracker.get_slot("feedback"))
return []

You can also watch here:

Storing gathered data to MYSQL database

Now, it’s time to store the gathered data to the database. For that, there is a function which we are calling above in ActionSubmit which will call a function DataUpdate which will be used to store the data to the database and the program to store the data to the database is given below,

database_connectivity.py

import mysql.connector
def DataUpdate(FirstName,LastName,Feedback):
mydb = mysql.connector.connect( host="localhost", user="root",
passwd="root", database="Rasa_feedback")
mycursor = mydb.cursor()
sql='INSERT INTO FeedBack_rasa_date (firstName, lastName, feedback) VALUES ("{0}","{1}", "{2}");'.format(FirstName,LastName,Feedback)
mycursor.execute(sql)
mydb.commit()

Here, one package needs to be installed which can be installed with the given command,

pip install mysql-connector-python-rf

but before installing this you must have mysql installed on your system as per your opereating system.

You can also learn here

After, everything is added successfully you can train your rasa chatbot and then test it with rasa shell or rasa x as per your suitability. Then you will see your rasa chatbot in action.

output of database connectivity with rasa chatbot

Time to wrap up now. Hope you liked our content on How to integrate Rasa chatbot on database. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more Rasa Chatbot contents.

Also check out our other playlist Rasa Chatbot, Internet of things, Docker, Python Programming, etc.
Become a member of our social family on youtube here.

Ashish Saini
Ashish Saini

I am a software developer for Python/Machine Learning. Also, I’m Rasa Hero at Rasa. Also, I’m a youtuber where I regulary uploads the videos to help this generation learn about the technology just like they are playing games.

Leave a Reply