Add Dialogflow and the Vonage SMS API to your Flask App

Published July 30, 2020 by Adrian Francis

blog spotlight banner

Dialogflow is a natural language understanding (NLU) platform provided by Google. It is used to design and integrate conversational user interfaces into mobile and web apps, devices, bots, and interactive voice systems. In this tutorial, we’ll demonstrate how to integrate with the Dialogflow and Vonage SMS APIs to build a quiz bot that interacts with the end user via SMS. We won’t be covering every piece of the application, so if you’re starting from scratch check out the full project repo (or read one of our previous tutorials on Flask development from the ground up!)

Vonage API Account

To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.

This tutorial also uses a virtual phone number. To purchase one, go to Numbers > Buy Numbers and search for one that meets your needs.

Start building with Vonage

Create an Application

We will begin by using the Nexmo CLI mode to create an app. Install the Nexmo CLI on your machine as follows:

Create a directory for your application locally:

Inside the directory, run the following command:

In the interactive CLI mode:

The application should now be created. Visit the dashboard and navigate to Applications. Your shiny new app should be listed there.

Set Up the Dialogflow Agent

To set up the Dialogflow agent, visit https://console.cloud.google.com and create a new project:

New Dialogflow project

Take note of the PROJECT ID. Enable the Dialogflow API from the list of APIs:

Enable Dialogflow

Visit https://dialogflow.cloud.google.com to create a new agent:

Create Dialogflow agent

To make it easy to get your application up and running, I’ve included a zip file in the GitHub repo for this project. Navigate to the dialogflow directory, and under resources download the quizzie.zip file. This zip is exported from the Dialogflow settings page and it contains all the intents, contexts, and responses required to set up an agent on the Dialogflow dashboard.

On the Dialogflow dashboard, click Settings and navigate to Export/Import. Import the zip file we just downloaded:

Import settings

Our Dialogflow quiz agent is all set up and ready to go!

For the next steps, we need to create the logic that will handle the back and forth messaging between the end user and our Dialogflow agent.

Application Code

We want to write an interface for sending an SMS to the end user. First, make sure you have the Nexmo Python library installed:

The function for sending an SMS looks like this:

Next add the code that will notify our customer:

Once the end user receives the messages, we want them to reply. We need to create an incoming webhook endpoint to receive the reply. Remember, we set up the inbound sms URL while creating an app earlier. It is now time to add logic to our webhook:

We need to chain the responses we receive from the end user as input to our Dialogflow agent. For this, we need to use the Dialogflow REST agent.

Specifically we need to detect the user intent and pass it as input to the agent:

Conclusion

The basic setup is now complete. We’ve shown the logic necessary for the app to send and receive SMS messages, as well as how to connect to the Dialogflow agent. To see the full code for the app, check out the project repo: https://github.com/nexmo-community/Vonage-QnA-app. And for any further questions, feel free to shoot me an email.

Leave a Reply

Your email address will not be published.

Get the latest posts from Nexmo’s next-generation communications blog delivered to your inbox.

By signing up to our communications blog, you accept our privacy policy , which sets out how we use your data and the rights you have in respect of your data. You can opt out of receiving our updates by clicking the unsubscribe link in the email or by emailing us at [email protected].