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:
export PAGE_ACCESS_TOKEN=”YOUR PAGE ACCESS TOKEN”export VERIFY_TOKEN=”YOUR SECRET”
In new terminal session, issue the following command to start the HTTP server:
go run *.go
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 :
ngrok http 5000
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:
So, install DialogFlow Golang library:
go get github.com/mlabouardy/dialogflow-go-client
Go back to DialogFlow dashboard and copy the Client Access Token:
Set it as environment variable:
export DIALOG_FLOW_TOKEN=”YOUR TOKEN”
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 ?
In the upcoming tutorial, I will show you how to create a Serverless Messenger Bot with Lambda & API Gateway.