DialogFlow (API.AI) Golang SDK

DialogFlow (formerly API.AI) gives users new ways to interact with your bot by building engaging voice and text-based conversational interfaces powered by AI.

DialogFlow offers many SDKs in different programming languages:

But unfortunately, there’s no SDK for Golang

But dont be sad, I made an SDK to integrate DialogFlow with Golang:

Résultat de recherche d'images pour "thank you sir meme"

This library allows integrating agents from the DialogFlow natural language processing service with your Golang application.

Issue the following command to install the library:

The example below, display list of entities:

Note: for more details about the available methods, check the project Github repository.

For a real world example on how to use this library, check my previous tutorial, on how to create a Messenger Bot in Golang to show list of movies playing in cinema, and tv shows airing on TV:

Messenger Bot with DialogFlow & Golang

This post is part of “ChatOps” series. In this part, I will show you how to create a Messenger Bot in Golang with DialogFlow (formerly API.AI) to show list of movies playing today in cinema.

Note: all the code used in this demo can be found on my Github.

Start with an HTTP server exposing 2 endpoints:

1 – GET /webhook

Handles Facebook challenge verification. It simply looks for the Verify Token and responds with the challenge sent in the verification request.

2 – POST /webhook

Handles messages coming from Messenger:

It calls the ProcessMessage method which uses Facebook Graph API to send a GIF image to the user:

Note: for more in depth details check my tutorial Build a Facebook Messenger bot with Go and Messenger API

Create a Facebook page. It will be “identity ” of your bot:

Then create a Facebook application. It will be the middleware that connects the server and your public page.

Click Add Product from the left menu, then choose Messenger:

At the Token Generation, choose the page you just created from the dropdown menu, and it will generate a token:

Once you’ve gotten your PAGE_ACCESS_TOKEN and VERIFY_TOKEN, make sure you add those two as environment variables for the server:

In new terminal session, issue the following command to start the HTTP server:

In order to make our server publically accessible, I will use a tool called ngrok. It basically creates a secure tunnel on your local machine along with a public URL you can use for browsing your local server.

Note: Keep in mind, to use your bot in production, you need to use a :

  • IaaS like AWSGCP, Azure …
  • PaaS like Heroku, Clever Cloud …

Then, at the Webhooks section, click the Setup Webhooks button:

After you’ve configured your webhook, you will need to subscribe to the page you created earlier:

Go to the Facebook Page you created and and click on “Message” button, next to the “Like” button near the top of the page. Start sending your Page messages and the bot should reply with a GIF !

By default, the bot should respond to everything with a GIF image.

Now lets make it smarter, for that we will use an NLP (Natural Language Processing) backend like DialogFlow (formerly API.AI):

So after signing up to Dialogflow, create a new Agent:

Give it a name and fill out the required fields:

Once created, lets use Small Talk feature of DialogFlow to give our bot the ability to have simple conversations:

Enable the Small Talk checkbox. With this feature enabled we imported a lot of predefined answers for simple questions and phrases. You can easily change the responses to the questions if you don’t like them:

To test it out, you can use the Console at the right hand side:

Now lets use this feature in out bot. DialogFlow offers many SDKs in different programming languages:

But unfortunately, there’s no SDK for Golang

But dont be sad, I made an SDK to integrate DialogFlow with Golang:

Résultat de recherche d'images pour "thank you sir meme"

So, install DialogFlow Golang library:

Go back to DialogFlow dashboard and copy the Client Access Token:

Set it as environment variable:

Create a new function that takes the message sent from a user via Messenger as an argument, and pass it to DialogFlow Client to get the appropriate response:

Go to the Facebook Page and click on Message to start chatting :

But that’s not enough, lets take this further and make our bot tell us about the movies playing today in cinema, and series airing today on TV.

Create an entity, to store the type of the show (movie or series) the user is asking about:

Then, create a new intent, which represents a mapping between what a user says and what action should be taken:

Create some more questions:

Finally, update the ProcessMessage method to respond with a list of shows if the intent name is shows. The method is using the moviedb library to get the list of shows.

Let’s test the bot from Messenger:

Wow !! you have created your first chatbot in Golang with DialogFlow ! It was easy, wasn’t it ?

Image associée

In the upcoming tutorial, I will show you how to create a Serverless Messenger Bot with Lambda & API Gateway.

Build a Facebook Messenger bot with Go and Messenger API

In this first tutorial of the “ChatOps” series , I will show you quickly how to create a Facebook Messenger Bot  in Golang. All the code used in this demo can be found on my Github

1 – Messenger bot

We will start by creating a dummy web server with one endpoint to print a hello world message ✌. I’ll use “gorilla/mux” package to do that. I found it much easier to setup routes for our server rather than using the go standard library.

We first need to install “gorilla/mux” library:

Then, create a file called app.go with the following content:

So basically, we have a main function which create a new route “/”, and associate to this path a HomeEndpoint function that use ResponseWrite reference to print a message. Then it start a HTTP server on port 8080.

To verify that things are working, start your local server with:

Then in your browser, visit http://localhost:8080 and you should see:

hello-world

Now that you’ve got your first endpoint working, we will need to expose 2 endpoints to make it work with Facebook platform:

1.1 – Verification Endpoint: (GET /webhook)

It simply looks for the Verify Token and responds with the challenge sent in the verification request.

1.2 – Messages Handler Endpoint: (POST /webhook)

It serialize the request body into Callback object , then it parse it and fetch the message object and pass it as an argument to ProcessMessage function that will use Facebook Graph API to send the response to the user (in this case we will send an image):

Our local server url (http://localhost:8080) is not available to all the other people in the internet  and doesn’t support HTTPS which is necessary for Facebook Messenger bot. Therefore, we need to expose it to the public.

2 – Deployment

Note: You could also use a tool like ngrok. It basically creates a secure tunnel on your local machine along with a public URL you can use for browsing your local server. Keep in mind, to use your bot in production, you need to use a real IaaS like AWS, Heroku, Clever Cloud …etc

In this tutorial I will choose CleverCloud as IaaS provider, it deploys your Go application for free and offer you extra add ons like monitoring, logs, scaling, continuous delivery …

In order to deploy to CleverCloud you’ll need a CleverCloud user account. Signup is free and instant. After signing up:

dashboard

We click on “Add an application”, then you can either upload your app code from local repository or from Github:

Next, We choose GO as our server side language. Then, we click on “Next

We left all fields as default, and we click on “Create

Our server does not use any external resources (MySQL, Redis …) so we will simply skip this part by clicking on “I don’t need any add-on

Congratulations !   You have successfully deployed your server.

app

Note: The app URL is :

https://ID.cleverapps.io

ID is the string on the bottom right of the dashboard,

3 – Facebook Setup

3.1 – Create Facebook Page

If you don’t already have one, you need to create a facebook page that we will use to connect our bot to.

Just give it a name, and that’s it now you have created a Facebook page for you Bot 

3.2 – Create Facebook App

Once the page was created, we will create a facebook app which will be connected your webhook server and your public page:  which works as middleware that connects your Webhook (APP URL) and your public page.

You need to give your app a name then click on “Skip and Create App ID

After creating an app, we need to add Messenger as a platform. So we click on “Add Product” then select Messenger as a product

Now you’re in the Messenger settings. There are few things in here you’ll need to fill out in order to get your chatbot  wired up to the server endpoint we set up earlier.

3.2.1 – Generate a Page Access Token

Using the page we created earlier, you’ll get a random “Page Access Token

You need to copy the token to your clipboard. We’ll need it as an environment variable (PAGE_ACCESS_TOKEN) for our server.

3.2.2  – Setup subscription

Next, we click on “Setup webhook” button on “Webhooks” section, a new popup will show up:

  • Callback URL : Clever Cloud we set up earlier
  • Verify Token: A secret token that will be sent to your bot, in order to verify the request is coming from Facebook, Make sure to remember the value because we will need it as a second environment variable (VERIFY_TOKEN) for the server
  • Subscription Fields: represents which events you want Facebook to notify your webhook about, in this case, we will choose “messages

After you’ve configure your subscription, you’ll need to subscribe to the specific page, you want to receive message notification for

3.2.3 – Set environment variables

Once you’ve gotten your PAGE_ACCESS_TOKEN and VERIFY_TOKEN, make sure you add those two as environment variables for the server on clever cloud dashboard

Then restart the application and you should be good to go ! 

4 – Test the Bot

Go to your Facebook Page and send a message to it. You should see a gif back to you 

5 – Customize your Bot’s behavior

In this quick tutorial I showed you how to build a simple & dumb bot for facebook messenger, to make it smarter  and have more interactions with the user. We need to use a NLP backend like api.ai (Google), wit.ai (Facebook) or motion.ai. And that will be the subject of my upcoming tutorial ❤