Send Automated Emails using Rasa chatbot
Rasa is a python module used to create custom AI chatbots. You can easily send automated emails to your users using the rasa chatbot. Rasa is a tool to build custom AI chatbots using Python and natural language understanding (NLU). Rasa provides a framework for developing AI chatbots that uses natural language understanding (NLU). It also allows the user to train the model and add custom actions.
In this article, we are going to see how to send emails using Rasa.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Creating a Rasa chatbot
First, you will need a rasa chatbot through which you can send automated emails.
To install the rasa module type the below command in the terminal (requires Python 3.6, 3.7, or 3.8).
pip3 install -U pip pip3 install rasa
To create a new project with example training data type the below command in the terminal.
Now your rasa chatbot is ready. You can talk to your chatbot using the below command.
Now, In the next step, we will add some more training data to this chatbot.
Adding Intents, Responses, and Stories
Now our chatbot can respond to basic user inputs, but we have to add more intents, responses, and stories to take email ID and Name from the user. For an in-depth explanation of intents, responses and stories refer to this article.
Here we need two new intents for name and email id. Add the below lines in your nlu.yml file.
- intent: email_id examples: | - [firstname.lastname@example.org](email) - [abc@yourdomain](email) - @gmail.com - [email@example.com](email) - intent: user_name examples: | - [YOURNAME](name) - [RANDOM_PERSON_NAME](name)
Here we are also creating two slots ( email and name) in which we will store the user data so in your domain.yml file add the new slots and intents.
Now we have to add two new responses to our chatbot.
- To ask the user to enter name.
- To ask the user to enter an email id.
For this add the below lines in the responses section in the domain.yml file.
utter_askname: - text: Please enter your name. utter_askemail: - text: Please enter email id to receive updates.
Now we have to add some relevant stories in our stories.yml file so remove all the stories in that file and add the below lines ( you can add more stories according to your need).
- story: GeekforGeek story steps: - intent: greet - action: utter_askname - intent: user_name - slot_was_set: - name: "YOURNAME" - action: utter_askemail - slot_was_set: - email: 'firstname.lastname@example.org' - intent: email_id - action: action_email
Here ‘action_email’ doesn’t exist so we have to create this action to send emails but before that go to the endpoints.yml file and uncomment the lines below.
action_endpoint: url: "http://localhost:5055/webhook"
Adding Custom Email Action
Now we will add a new class in our actions.py file to send automated emails. Add the below codes in the actions.py file.
Explanation: First import rasa and smtplib module. Then creating a python class that will return an action named ‘action_email’ and get the username and email id from the slot and storing them in separate variables. Creating connection using SMTP method of smtplib module. And then Make the connection secured using the starttls() method of the smtplib module. And then Logging in to the Gmail account using the login() function that will take email id and password as parameters. Storing the message in a new variable named ‘message’. And then send the Email using the Sendmail function that will take the sender’s email id, receiver’s email id, and message as its parameter. Then close the connection using the quit() function.
You also have to allow access to less secure apps in your Gmail account.
Log in to your Gmail account, and go to “Manage your Google Account”.
Then click on the “Security” tab.
Then turn on the “Less secure App access”:
After this add the below lines in the domain.yml file:
actions: - action_email
Now everything is ready we just have to train our chatbot. For this type the below command is in the terminal:
After training is complete you can talk to your chatbot by typing the below commands in the terminal. To run action server:
rasa run actions
To run the trained model: