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:



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.

Flow Chart:

Similar Reads

Creating a Rasa chatbot

First, you will need a rasa chatbot through which you can send automated emails....

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 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....