In the first section of this series we developed an idea and a high level spec of our bot functionality. We also bootstrapped our rails app and created a dummy “hello world” bot, that was basically a skeleton to keep moving. Today, we will make our bot a more useful by adding the most common functionalities, according to our high-level spec that we developed. Here’s what we are actually going to do:

First, we need to define our rails models that will give us a manageable persistence layer for our bot, e.g. will help us to store settings and daily notes for future storing and querying.

The next thing that is needed is to develop UI, based on conversational flow using existing facebook messenger api goodies, such as quick replies, postbacks, and persistent menus.

Defining Our Data Models

Referring to our high level spec, at first we need to store the state of our main actor e.g. User, so we will introduce a user model. The next thing that we would need is our main actor state after he or she received a “question” from the bot, so here is where the next entity comes in, let’s call it an Update.

In order to make sure that our bot sends a “question” to the correct recipient we need to store. Fortunately Facebook Messenger webhook provides a bunch of metadata that gives information about our current user, in our case all we need is to have is an existing facebook_id. Also our bot “needs” to know when exactly it should “ask” a question, for that purpose let’s introduce a notification_time field. And that’s it for User model. Note: to simplify our logic for this tutorial, let’s assume that new user is that one who hasn’t setup notification_time yet.

Ok, we have now brainstormed what kind of field we need for the user, so what about an update model? Let’s think about this entity on a higher level:

First the bot “asks” about a user’s mood, and the user will receive a prompt of 3 emojis illustrating corresponding mood(good, normal or bad).

Next, the bot may prompt a user to describe days in several sentences.

Later on we would like to have weekly and monthly summaries from the bot.

So obviously according to this high level spec our Update model should have the following fields:

mood — which is integer in our case, because in this case we can leverage the awesomeness of built-in rails ActiveRecord::Enum api.

— which is integer in our case, because in this case we can leverage the awesomeness of built-in rails ActiveRecord::Enum api. message — just a simple text field that will store text message with user’s daily update

So according to this information we can define following models in our rails app:

I think you know how to generate migrations in rails, but just in case you should run the following for each corresponding model:

for user rails generate migration CreateUsers , after that we need to add proper fields:

for updates rails generate migration Create Updates

Building Persistent Menu and Adding a “Get Started” Button

The Facebook Messenger api offers a feature of persistent menus for having settings and providing proper onboarding routines for new users. We need to define when we should be notified, and also we would like to give our user some kind of reset settings functionality in order to remove or change our notification. As this thing should be triggered only once we write a simple rake task that handles this routine for us using Facebook Messenger gem functionality.

lib/taks/bot_setup.rake

It Can Talk!

After we finish preparations (hope you are not tired yet), we can finally get our hands dirty into bot code.

We have already added on-boarding controls to our Facebook bot, but how can we handle it? Fortunately for us the Facebook Messenger libary gives us a functionality of handling postback messages, so let’s write a simple handler.

Open app/bot/messenger_bot.rb , that we already created after first section and add the following lines.

In this code we simply handled the getting started button and added a dummy handler of our menu item that we will implement in the next part of this tutorial.

After you successfully added this, you can test your controls in Facebook Messenger by clicking the “Getting Started” button. You should see a greeting message like in screenshot below.

We have sent pretty dummy messages, but we need to start setup procedure, so let’s slightly change our greeting message and control that will lead user to continue dialog with our bot.

As you may have noticed, we have added a quick reply item into our ‘SETUP_BOT’ handler code that will show a button with quick reply message to our user. In order to test this code, first remove all conversation history and click ‘get started’ button again and you should see the following.

Great! We have already have a button that will lead us to setup procedure what should we do next?

First of all we need to create a user record in our database after user has clicked the button.

The second is we need to prompt our user when it should be notified, in order to simplify this part let’s introduce two autocomplete options: ‘morning’ and ‘evening’.

After user gave all the details to the bot, we send some outro message and finish our conversation.

As you see in quick_reply property that we were sending to facebook api was a field payload, the purpose of this field looks very reasonable to use to determine what step user did in our bot ui, let’s leverage this data and handle this payload in our message handler.

We were also added two more additional payloads that we will handle in next part of our tutorial series.

Now let’s test our bot, after you did everything as I described below you should see the flow above.

Recap