Building your Conversational Bot with Go and Api.ai

With all the hype of conversational bots making the rounds, its good to know what is involved and how much effort it takes in creating a simple conversational bot. Here lets build a bot and integrate it with messenger.

To keep it simple, we will build FruitBot which orders food and lets you check on its delivery status.

Architecture

Creating the Bot in Api.ai

Login to Api.ai and create a New Agent.

Before we begin lets draft out some conversation flow and understand what we are going to build

me: hi
bot: hi, welcome to fruit bot! what fruits do you feel like eating today?
me: I would like apples today
bot: i’ve added apples to your cart. do you want to checkout?
me: yes
bot: thank you for the order. your order will be delivered on Thursday by 9:00 pm.
me: what is the status of my order
bot: your order will be delivered by tomorrow 9:00 pm

Not the greatest of bots, but this will do for this tutorial.

Intent

Bots are mainly composed of 3 components. Intents, Entities and Contexts. Lets build our intents. looking at the conversation we can create 3+2 intents

Greeting: Hi, Hello, Hey
AddFruit: I would like apples today
Checkout(Yes/No): yes
Status: what is the status of my order

Greeting intent seems straight forward

To build AddFruit though we need to identify the fruits so we need to create an entity which can recognize the Fruits

Entity

Create an entity Fruits and have some fruits listed with it

fruits: Mango, Apple, Banana, Jackfruit, Kiwi, Avocado, Strawberry, Peach…

Context

In the conversation where we say “yes” for checkout the bot has to have some context of which operation was being performed. so we split this intent into two contexts and it connected with a context_out from the previous intent.

Now the bot should be able to perform basic conversation . try it out in the console.

Go Server webhooks for Messenger

Time to connect it to messenger.

Messenger guide explains this quite nicely, but uses node.js. Here is the equivalent using golang.

Setup

  1. Register an App

2. Setup a basic server with golang which responds to the webhook verify request from facebook.

Lets use ngrok to quickly expose this server to facebook. Run the server locally and then run ngrok to get a secure https url for our server.

3. Register the webhook in facebook messenger.

4. Get a Page Access Token

5. Subscribe the App to the Page

6. Receive Messages

We need to handle POST requests which receive the messages. The json details is documented well. Lets implement receiving messages

Now time to modify this to reply to a message. replying is POST a message to a rest endpoint

Connecting GoServer with Api.Ai bot

Now that we’ve got a chat working with our server, we need to call REST endpoints of Api.ai when we receive a message and reply back to the bot. Its quite trivial to make another rest call here is the main crust of the work.

Try your new bot!

If you would like to see the full source code. you can check this github link.

Developed at Innovation Labs @ Y Media Labs