How To Send Emails From Your Chatbot

Apps Feb 7, 2021

In this week's post, let's explore a few scenarios why you would want to have your chatbot send an email, and ways you can actually do that.

The Most Common Chatbot Emailing Scenarios

Chatbots are an easy way to automate interactions with human users for many businesses. With low-code tools, they are becoming more and more popular on internet, even for small businesses.

At Clevy (the team behind the open-source CSML programming language), one of the most common situations where we see chatbots sending out emails is often for alerting purposes:

  • Hey, some user just finished an order for product X, time to grab it from inventory
  • Hey, some visitor just made an appointment for X on date Y
  • Hey, someone just filled an information request form on the chatbot

In many cases, emails can affordably replace a full-blown CRM or alerting system, and are still extremely relevant in tons of situations.

How To Send an Email With CSML

CSML provides a few built-in apps to send emails in CSML Studio:

Some emailing solutions that CSML Studio lets you integrate with easily

In our example, we are going to use our Sendinblue integration. Sendinblue is one of the most popular providers for email marketing automation and their service is very easy to setup; there is even a free plan with enough emails to get started (we are in no way affiliated with them, so you are absolutely free to use whatever service you like - even use a custom SMTP integration).

Let's say that the goal of our chatbot is helping take information requests for a hotel. The conversation could go like this:

start:
  say "Hello, and welcome to The Stanley Hotel!"
  say Question(
    "What can I help you with?",
    buttons=[
      Button("Organizing my wedding"),
      Button("Planning a corporate retreat"),
      Button("Booking my next holiday"),
    ],
  )
  hold
  remember reason = event
  say "Got it!"
  
  say "May I know your phone number, so that we can get in touch?"
  hold
  
  remember phone_number = event
  say "Thanks!"
  
  say "Is there anything you want to add?"
  hold
  
  remember additional_details = event
  say "OK, thank you for your request! A representative will call you shortly 🤗"
  
  // We have all the information we need from the user
  // and should send an email to the reception so that 
  // they can call the user at their earliest convenience
  
  goto end

This flow is quite straightforward: ask the user for some basic info, then send this to a shared inbox, and close the conversation.

To add the Sendinblue app to your chatbot on CSML Studio, simply visit the Functions section in the left panel, search for Sendinblue, click Install and add your own API key which you can find in your Sendinblue account settings.

To send an email with this app from your flow, the code is very simple:

do Fn(
  "sendinblue",
  method="sendTransactionalEmail",
  sender={"name": "Reception Bot", "email": "bot@mydomain.com"},
  to=[{"email": "reception-inbox@mydomain.com"}],
  subject="New Chatbot Inquiry: {{reason}}",
  htmlContent="<p>I have just received an inquiry for: {{reason}}</p><p>phone: {{phone_number}}</p><p>additional details: {{additional_details}}</p>",
)  

Of course, don't forget to replace the email addresses with the correct values for you. This is only an example!

If we now replace the comments at the bottom of the flow with our code, this will send an email to the given email address!

start:
  say "Hello, and welcome to The Stanley Hotel!"
  say Question(
    "What can I help you with?",
    buttons=[
      Button("Organizing my wedding"),
      Button("Planning a corporate retreat"),
      Button("Booking my next holiday"),
    ],
  )
  hold
  remember reason = event
  say "Got it!"
  
  say "May I know your phone number, so that we can get in touch?"
  hold
  
  remember phone_number = event
  say "Thanks!"
  
  say "Is there anything you want to add?"
  hold
  
  remember additional_details = event
  say "OK, thank you for your request! A representative will call you shortly 🤗"
  
  do Fn(
    "sendinblue",
    method="sendTransactionalEmail",
    sender={"name": "Reception Bot", "email": "bot@mydomain.com"},
    to=[{"email": "reception-inbox@mydomain.com"}],
    subject="New Chatbot Inquiry: {{reason}}",
    htmlContent="<p>I have just received an inquiry for: {{reason}}</p><p>phone: {{phone_number}}</p><p>additional details: {{additional_details}}</p>",
  )  
  goto end

When we run the chatbot, we indeed receive an email a few moments later:

Ideas To Improve Your Chatbot

This example is obviously very simple. In a real-world situation you would probably want to get the visitor's first and last names, as well as validate their input to make sure that they are choosing valid options every time.

Perhaps you could also ask for their email address and send them a copy of their inquiry. This way, you can reassure them that you will get in touch shortly, and that they can also reply to that email to continue the conversation.

If you prefer using your own SMTP solution to send emails, please check out our SMTP integration.

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.