Robots in Disguise: Intro to Ruby Chatbots with Stealth (Part 3)

Rahul K
4 min readJul 2, 2020

--

This is a 3-part series on creating your first Ruby Chatbot using the Stealth gem. In this part we craft our replies and deploy to Heroku. Here is a link to Part 1 and Part 2. You can see the repo here.

You’ve Got Replies

Now the fun part — we can craft replies. Each reply is an individual interaction with the bot, and can include not just text but also images, links, buttons and suggested responses. For each state defined above in our hello and goodbye controllers, we need a matching set of replies. We can look at the default Catch All reply to see how they are constructed:

Catch All — Level 1

Replies are YAML files with a reply_type and other relevant details specified for each type (e.g. text for replies, duration for delays). The full specifications can be found in the Stealth documentation. In this case we are merely sending text content back to the user when something goes wrong. Let's see more examples via the hello and goodbye replies:

Hellos

We can add embedded ruby (erb) to our YAML by adding .erb to our YML file name and wrapping any ruby with with <%= RUBY HERE=>. Our ‘say hello’ reply uses this:

We can continue to write replies this way:

Goodbyes

Loads of goodbye replies as well. Of note, is the ask_restart.yml which creates a button in the reply that has a payload. Payloads are specific to Facebook, that allow us to return a “short” version of the response for use in our controller.

If the actual response is a longer string “I would like to restart, yes.”, we can assign the button press a payload (in this case “restart pressed”) that is returned in the response and check for that instead. As a refresher, I’ve included the controller code below to show how this is handled.

Our Goodbye Replies
How the Payload is Handled

As you can see, there are many options in terms of how we construct these replies. Let’s see it in action! Assuming we haven’t stopped our Redis server and ngrok tunnel, we should restart our Stealth server with ctrl + c then stealth s. Head to the Facebook Page and hit Send Message to get started.

Oh Hello There!

In your stealth server terminal you can see the jobs running in sidekiq and any webhooks received. The logs are tagged with [previous_session], [session], [facebook], [incoming], and [catchall]. Logs tagged with session will provide useful information to debug broken flows or states that you may have.

You can also use localhost:4040 to see the ngrok interface and the responses (in json) that Facebook sends back to your Stealth server.

If anything further upstream breaks, you'll get Ruby/Rails-style stack errors. Stealth generates less verbose and helpful errors than Rails unfortunately, but if you’re used to Rails debugging, it will be very familiar!

GIT PUSH HEROKU MASTER

If you’d like to deploy you chatbot server to production, you can easily do that with Stealth given that it’s a Rack-based app. We can deploy to Heroku in this example since Rack ♥️ Heroku.

Firstly, Create a production Procfile that contains instructions for Heroku to run when deployed:

You can deploy the app to Heroku via the dashboard (you can link your GitHub repository) or CLI (install the Heroku CLI to do so). Once deployed, make sure:

  1. to enable both the Heroku Postgres (if you use a database) and Heroku Redis addons in your Heroku dashboard
  2. the web and sidekiq dynos are spun up
  3. add your tokens from the .env file as config vars in Heroku
  4. to update the Callback URL in Messenger settings in the Facebook App: https://developers.facebook.com/apps/<app id>/messenger/settings/
  5. you run heroku run stealth setup facebook in your CLI to configure the messaging service

🎉 And that’s it — you’ve done it! Your first FB Messenger Chatbot. Congratulations! 🎉

You can see the full repository here. Feel free to fork and/or improve it.

--

--