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

Rahul K
5 min readJul 2, 2020

--

This is a 3-part series on creating your first Ruby Chatbot using the Stealth gem. In this part we introduce Stealth and set up our configuration. Here is a link to Part 2 and Part 3. You can see the repo here.

What’s a Chatbot?

A Chatbot is another form of User Interface, connecting users to information in a meaningful and delightful way. They simulate the kinds of conversational experience people are already familiar with via messaging apps

A benefit of using them is “outsourcing” repetitive tasks like FAQs, the start of the sales funnel. At the top end of function however, they can enable deep productivity gains (e.g. Alexa, Google Now) and specific, impactful forms of human support (mental health bots).

Growth in AI and ML will empower chatbots to understand semantics and intention even better in the future — passing the Turing Test is the extreme end of this challenge

Photo by Jason Leung on Unsplash

And What is Stealth?

Stealth is an open source Ruby framework for creating text and voice chatbots 🤖. It’s design is inspired by Ruby on Rails’ philosophy of convention over configuration, so uses an MVC architecture with the slight difference that views 👀are aptly named replies ✉️.

So what kind of chatbot are we trying to create today? It’s a Facebook Messenger bot associated with a Facebook Page. You might have used one before. If not, here’s a demo.

Facebook’s Messenger Demo
Facebook’s Messenger Bot Demo

Getting started

As with any gem-based framework, getting started with Stealth is simple:

> gem install stealth # Install the gem if you haven't already
> stealth new facebook-bot # Create a new Stealth bot - this also runs bundle install for you
> cd facebook-bot

Let’s add the stealth-facebook integration to power our Messenger bot, and the dotenv gem to protect our keys.

Then bundle install , and you'll see it also install the stealth-facebook gem's [faraday](<https://github.com/lostisland/faraday>) dependency.

Faraday is an HTTP client library that provides a common interface over many adapters (such as Net::HTTP) and embraces the concept of Rack middleware when processing the request/response cycle. It’s like axios but for Rack apps.

Let’s add a .env file to our app root (touch .env ) and add it to our .gitignore along with dump.rdb (Redis dump file) to ensure they don't get pushed to GitHub. Let's also add the environment variables we need to the .env file. We can get these once we create a Facebook app (under basic settings). Add a Messenger bot to the App you've created. You'll also need to create a Facebook Page that hosts the Messenger bot. You can follow the initial steps (until "Build the Experience") of this guide to see how it's done.

Ignore This

We’ll also need to ensure our environment variables are loaded when the Stealth server starts:

We can now set up our services.yml file, which contains our Facebook App config variables and the initial framework for our Messenger app. This is a YAML file, which is a markup language (like XML but much easier to read and write) which recursively stands for YAML Ain't Markup Language.

We use a bit of embedded Ruby (erb) to add in our environment variables — no need to rename our file in this case.

This setup gives us some configuration to link the basic functionality of the Messenger app to our app - a custom greeting triggered by the user entering the conversation (greeting), a Get Started button (get_started) that triggers the chatbot flow and adds the user to the chatbot system and memory, and a persistent menu (persistent_menu) available to the user regardless of where they are in the chatbot flow, allowing them to go directly to a flow state. We also have access to locale for i18n purposes.

Our app folder structure should look like this now:

├── Gemfile
├── Procfile.dev
├── README.md
├── Rakefile
├── bot
│ ├── controllers
│ │ ├── bot_controller.rb
│ │ ├── catch_alls_controller.rb
│ │ ├── *concerns*
│ │ ├── goodbyes_controller.rb
│ │ └── hellos_controller.rb
│ ├── helpers
│ │ └── bot_helper.rb
│ ├── models
│ │ ├── bot_record.rb
│ │ └── concerns
│ └── replies # replaces views in typical Rails app
│ ├── catch_alls
│ │ └── level1.yml
│ ├── goodbyes
│ │ └── say_goodbye.yml
│ └── hellos
│ └── say_hello.yml
├── config
│ ├── boot.rb
│ ├── database.yml
│ ├── environment.rb
│ ├── flow_map.rb # replaces routes.rb
│ ├── initializers
│ ├── puma.rb
│ ├── services.yml # service config file
│ └── sidekiq.yml
├── .gitignore
├── config.ru
├──db
│ └── seeds.rb
└── .env # stores environment variables

Now let’s start our servers:

# Start the Redis server
> redis-server
# Start the Stealth server
> stealth server
# Use ngrok to create a public tunnel for incoming traffic. GUI for ngrok lives on localhost:4040. Make a note of the forwarding URL that looks like (always prefer HTTPS for callbacks)
> ngrok http 5000

Let’s register our bot’s callback URL on our App. Head to the Messenger → Settings section of our App and look for the Webhooks section. Add a Callback URL from your ngrok server, and append incoming/facebook to it. Also add in your FACEBOOK_VERIFY_TOKEN keyword that you created earlier. It should look like the example below:

Finding your Messenger Settings
Adding a Callback URL
Setting up your Callback URL

Hitting Verify and Save should trigger your bot being added to the list, with some activity on both your Stealth server log:

15:39:48 web.1 | [incoming] Received webhook from facebook

and your ngrok tunnel:

GET /incoming/facebook 200 OK

Once that’s done, hit Add Subscriptions next to your chatbot and select messages, messaging_referrals and messaging_postbacks .

Hit Save and you should see 3 fields next your chatbot name. Now we run stealth setup facebook in the terminal to register our settings (e.g. persistent menu) with the Facebook Messenger app. You'll need to run this every time you change the services.yml file to ensure the latest settings have propagated through.

All good so far, we’ve finished the app integration so let’s save our progress! We’ll push our initial setup to GitHub. git init , hub create and ga . && gc -m"initial setup" then gp origin master so we can populate our repository with our first commit.

If you run into an ActiveRecord error when you run stealth setup facebook or stealth console, add require 'active_record' to the top of your /bot/models/bot_record.rb file!

🏁 Checkpoint! 🏁

Well done so far, this is just to get your head around Stealth and how it differs from Rails. We’ll continue with configuring our Bot’s routing and controllers in Part 2.

--

--