Open In App

Send Automated Emails using Rasa chatbot

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Flow Chart:

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.

rasa init

Now your rasa chatbot is ready. You can talk to your chatbot using the below command.

rasa shell

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.

Adding Intents:

Here we need two new intents for name and email id. Add the below lines in your nlu.yml file.

- intent: email_id
  examples: |
    - [abc@gmail.com](email)
    - [abc@yourdomain](email)
    - @gmail.com
    - [xyz@gmail.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.

Adding Responses:

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.

Adding Stories:

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: 'abc@gmail.com'
  - 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.

Python3




# Importing required modules
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
import smtplib
  
# Creating new class to send emails.
class ActionEmail(Action):
  
    def name(self) -> Text:
        
          # Name of the action
        return "action_email"
  
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
  
        # Getting the data stored in the
        # slots and storing them in variables.
        user_name = tracker.get_slot("name")
        email_id = tracker.get_slot("email")
          
        # Code to send email
        # Creating connection using smtplib module
        s = smtplib.SMTP('smtp.gmail.com',587)
          
        # Making connection secured
        s.starttls() 
          
        # Authentication
        s.login("SENDER_EMAILID", "PASSWORD")
          
        # Message to be sent
        message = "Hello {} , This is a demo message".format(user_name)
          
        # Sending the mail
        s.sendmail("SENDER_EMAIL_ID",email_id, message)
          
        # Closing the connection
        s.quit()
          
        # Confirmation message
        dispatcher.utter_message(text="Email has been sent.")
        return []


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:

rasa train

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:

rasa shell

Output:

Video Output:



Last Updated : 31 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads