/fbbot

Facebook/Wit.ai bot for Ruby Hack Night

Primary LanguageRuby

FBBot - the Ruby Hack Night Facebook Bot

Slides and assets for the FBBot workshop [first presented at Toronto Ruby Hack Night, June 30, 2016]
Workshop for learning Chatbots, Facebook messaging and Wit.ai
Created by David Andrews and Jason Schweier

There are no slides for this workshop. All of the materials are contained in this readme.
Here is what we're going to build:
Facebook Bot design

###Introduction This project is a simple Ruby project. The workshop comes in four parts:

  1. create a Facebook app and host it on a page
  2. create a Wit.ai bot
  3. connect Facebook and Wit.ai
  4. customize your bot for a particular purpose

###Setup

Here are the steps to get you started with the repo.

  1. For this workshop, you will need a laptop with the following:
  • Ruby 2.x
  • A Github account

Note: We have included a .ruby-version file locked to 2.2.3, which you can change to any Ruby 2.x version if you don't have 2.2.3 installed
More detailed instructions for each platform are included in the footer. Refer there if you are having issues.

  1. Fork the repo (optional, recommended): From the page https://github.com/k00ka/fbbot, click the Fork button in the top-right corner. Copy the new repo address (in a box just below the thick red line) into your clipboard. Detailed instructions on forking a repo can be found here: https://help.github.com/articles/fork-a-repo/

  2. At Ryatta Group we use rbenv, and so we've included some optional elements - just skip them if you're using rvm or are not versioning your Ruby. If you forked the repo above, your repo_address will be in your clipboard. If not, you should use my repo_address git@github.com:k00ka/fbbot.git

% git clone <paste repo_address>
% cd fbbot
% gem install bundler
Fetching: bundler-1.7.4.gem (100%)
Successfully installed bundler-1.7.4
1 gem installed
% bundle
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Installing rake 10.3.2
...
Using bundler 1.7.4
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

Note: if you use rbenv...

% rbenv rehash

You are (almost) there!

How to set up a conversation with your own Facebook bot

  1. Do the above
  2. Create an ngrok account - http://ngrok.com
  3. Download and install ngrok
  4. Start ngrok
  5. Sign into Facebook
  6. Create a page - community - use your name and check “add to favorites”
  7. Go here https://developers.facebook.com/quickstarts/?platform=web
  8. Create a new app (I called mine k00kabot)
  9. Paste your ngrok URL into Site URL (check this)
  10. In your project space (shell) type: rackup -p 3000 (don’t hit return)
  11. Back in the browser go to your app using top-right menu
  12. Click dashboard
  13. Click Show beside App Secret
  14. Copy the app secret token to the end of your command line
  15. Add Messenger on the left menu
  16. Choose your new page from the page dropdown
  17. Accept the warnings
  18. Copy the page access to the end of your command line
  19. Click setup web hooks
  20. Copy ngrok HTTPS token into the callback URL
  21. Set your verify token to something you’ll remember
  22. Add the verify token to the end of your command line
  23. Click at least the “messages” checkbox
  24. Add the text “config.ru” to the end of your command line
  25. Hit enter on your command line to run the bot (WEBrick starts)
  26. Click verify and save on the application page (should verify)
  27. Surf back to your new page
  28. Click the “. . .” in the header and choose “View as page visitor”
  29. Click “Message” in the header
  30. Send a message - you should see “Hello, human”
  31. Type ctrl-c to stop your script (don’t kill ngrok!)
  32. Echo your command line into the file run1.sh (note: it’s in .gitignore, so you can't check it in with your private deets)
  33. Chmod +x run1.sh
  34. CELEBRATE BRIEFLY - you have integrated with Facebook successfully - you're 1/3 of the way there

How to integrate with Wit.ai

  1. Create an account on wit.ai
  2. In your Wit.ai app, click settings
  3. Click the "spinny" icon beside Client Access Token to create one
  4. Copy the access to token
  5. Paste the access token into the command-line you created earlier (before config1.ru)
  6. Change config1.ru to config2.ru and "echo" the resulting command line to run2.sh
  7. Chmod +x run2.sh
  8. In Wit.ai add a story to your app
  9. Create a User Says node, and type "tell me a joke about technology"
  10. Highlight "technology" and click "add a new entity"
  11. Type "category" and make sure the value says "technology", hit enter
  12. Create a Bot Executes node and type "merge"
  13. Create a Bot Executes node and type "fetch_joke"
  14. Create a Bot Says node and type "{response}"
  15. Create a Bot Executes node and type "clear_context"
  16. This above node is the main story, providing a joke to the user
  17. Click Understanding
  18. Think of many different ways of asking for a joke, like: "I like jokes about cars"
  19. For each ensure the category, if any, is highlight and correctly identified. Fix it if not.
  20. After creating 4 or more examples (some may not include the category), click Stories
  21. Continue with a new User Says node, typ4e "What are the categories of jokes?"
  22. Highlight the phrase "categories of jokes" phrase and click "add a new entity"
  23. Name the entity "meta" and ensure it has the value "categories of jokes"
  24. Create a Bot Executes node and type "fetch_categories"
  25. Create a Bot Says node and type "{response}"
  26. Create a Bot Executes node and type "clear_context"
  27. Click "understanding"
  28. Click on meta in the lower list and remove "trait"
  29. Click "understanding" to go back
  30. This of many differetnt ways of asking for categories
  31. For each, ensure the query is interpreted correctly (i.e. meta is assigned "categories of jokes")
  32. Extend your story with a new User Says node
  33. Type the phrase "That was funny"
  34. Click "add a new entity" and name it "Sentiment", set the value to "positive"
  35. Add a Bot Executes node and type "merge"
  36. In "updates context keys with" put "sentiment"
  37. Add a Bot Says node and type "Glad you liked it"
  38. Add another User Says node
  39. Type the text "That's stupid"
  40. Click "add a new entity" and name it "Sentiment", set the value to "negative"
  41. Add a Bot Executes node to run the "merge" function
  42. In "updates context keys with" put "sentiment"
  43. Add a Bot Says node and type "There's no pleasing some people."
  44. Click "understanding" and train up these two interactions - ensuring each has either positive or negative values for sentiment
  45. Run your bot by typiing "./run2.sh" at the command-line
  46. Go into your Facebook chat and give it a try!
  47. CELEBRATE MORE - you now have FB hooked to your Wit.ai back-end

Train your AI

  1. Play, train and revise using the Understanding tab in Wit.ai
  2. The Wit.ai Stories tab is in beta as I write this - save and opy your storied often or risk losing your work!

Congratulations, you've created a Facebook bot with an AI back-end!