"Bot-end" Ruby framework to quickly build and launch a Facebook Messenger chatbot
"Bot-end" Ruby framework to quickly build and launch a Facebook Messenger chatbot
:warning: Hooray! Rubotnik is now a gem that can generate a project for you and serve it from localhost or Heroku. There's been some API changes since it has been a boilerplate. If you are looking for an old README and rubotnik-boilerplate's code you can find it here.
Tiny "bot-end" Ruby framework with intuitive DSL to quickly build, test and deploy a Messenger chatbot .
Only minimal Ruby knowledge is required. Perfect for pet projects, classrooms and building bot MVPs from scratch while having a complete freedom over what your bot can do (ChatFuel is great, but what if you actually want to code?). Probably not the best solution for commercial projects at the moment—Rubotnik is yet to prove its worth in production.
This is how you greet your users with Rubotnik:
Rubotnik.route :message do
bind 'hi', 'hello', 'bonjour' do
say 'Hello from your new bot!'
end
endRubotnik is zero-configuration, you can have a conversation with your bot on Messenger in under 10 minutes (and most of that time you'll spend on Facebook and Facebook for Developers, creating a page and an "app" for the bot).
Rubotnik is built on top of an excellent facebook-messenger gem by Johannes Gorset that does all the heavy-lifting to actually interact with the Messenger Platform API. While facebook-messenger implements a client, rubotnik offers you a way to reason about your bot design without dozens of nested if and case statements. Rubotnik comes with a bare-bones architecture that ensures multiple connected users are served without delays or overlaps.
Exactly as with modern front-end, "bot-end" is a separate web application that can talk with your back-end through a (REST) API. Project generated with Rubotnik uses Puma as web server, contains its own config.ru and can be deployed to Heroku in a matter of minutes. And you can still use Heroku's free account, as "sleep time" is not a deciding factor in bot interactions.
Rubotnik currently can not be integrated directly inside the Rails project (although thoughts on that are welcome) and does not have a database adapter of its own on purpose, to keep the boilerplate to the very minimum and extract conversational logic to a separate layer.
In other words:
Rubotnik is perfect to consume existing APIs from a chatbot interface
NB: Rubotnik comes with httparty library to make HTTP requests, but you can use any other tool by including it in the project's Gemfile.
It is recommended to install Heroku CLI, Ngrok and Bundler before you start.
$ gem install rubotnikAnd then create your bot with:
$ rubotnik new PROJECT_NAMENow you can cd to PROJECT_NAME, open it in your favorite editing and start hacking!
To start playing with your bot directly from Messenger (after you've created an app in Facebook Developer Console):
$ bundle install
$ heroku localAnd in a new Terminal tab:
$ ngrok http 5000A project generated with rubotnik new will have a following simple structure:
…ruby
message.id # => 'mid.1457764197618:41d102a3e1ae206a38' message.sender # => { 'id' => '1008372609250235' } message.seq # => 73 message.sent_at # => 2016-04-22 21:30:36 +0200 message.text # => 'Hello, bot!' message.attachments # => [ { 'type' => 'image', 'payload' => { 'url' => 'https://www.example.com/1.jpg' } } ]
…ruby Rubotnik.route :message do bind 'how', 'do', 'you', all: true, to: :start_conversation, reply_with: { text: "I'm doing fine! You?", quick_replies: [['Good!', 'OK'], ['Not so well', 'NOT_OK']] } end
…ruby def start_conversation message.typing_on # simulate "typing" case message.quick_reply # switch on quick_reply property when 'OK' say "Glad you're doing well!" # end conversation, further input will be handled by top level bindings # defined under Rubotnik.route :message stop_thread when 'NOT_OK' say "Too bad. What happened?" next_command :appear_nice # pass the control further down the thread else say "" # it's always a good idea to have an else, quick replies don't # prevent user from typing any message in the dialogue stop_thread end message.typing_off end
def appear_nice message.typing_on case message.text when /job/i then say "We've all been there" when /family/i then say "That's just life" else say "It shall pass" end message.typing_off stop_thread # end conversation end
…ruby [ { content_type: "text", title: "", image_url: "http://example.com/img/red.png", payload: "" }, {...} ]
…ruby get_user_info(:first_name, :last_name) # => { first_name: "John", last_name: "Doe" }
* `next_command :command_name` and `stop_thread` are used to control conversation flow.
## Supported UI elements
### Button Template
`UI::FBButtonTemplate` takes two arguments: string for the text message and an array of hashes for buttons. See [types of buttons available](https://developers.facebook.com/docs/messenger-platform/send-api-reference/buttons) in Messenger Platform docs.
```ruby
TEXT = "Look, I'm a message and I have some buttons attached!"
BUTTONS = [
{
type: :web_url,
url: 'https://medium.com/@progapanda',
title: "Andy's Medium"
},
{
type: :postback,
payload: 'BUTTON_TEMPLATE_ACTION',
title: 'Useful Button'
}
]
# create template object and send it to connected user
template = UI::FBButtonTemplate.new(TEXT, BUTTONS)
show(template)
…
ruby
# A carousel with two items (platform supports up to 10)
CAROUSEL = [
{
title: 'Random image',
# Horizontal image should have 1.91:1 ratio
image_url: 'https://unsplash.it/760/400?random',
subtitle: "That's a first card in a carousel",
default_action: {
type: 'web_url',
url: 'https://unsplash.it'
},
buttons: [
{
type: :web_url,
url: 'https://unsplash.it',
title: 'Website'
}
]
},
{
title: 'Another random image',
# Horizontal image should have 1.91:1 ratio
image_url: 'https://unsplash.it/600/315?random',
subtitle: "And here's a second card. You can add up to 10!",
default_action: {
type: 'web_url',
url: 'https://unsplash.it'
},
buttons: [
{
type: :web_url,
url: 'https://unsplash.it',
title: 'Website'
}
]
}
]Then you can create an instance of UI::FBCarousel by passing your template to the constructor. Then you can send it to the user.
carousel = UI::FBCarousel.new(CAROUSEL)
show(carousel)Here is the the result:
You can send an image to the user. Note that image won't have any text, but you can send a regular message along with it.
img_url = 'https://unsplash.it/600/400?random'
image = UI::ImageAttachment.new(img_url)
show(image)The Open Graph template allows you to send a structured message with an Open Graph URL, plus an optional button. Currently, only sharing songs is supported. The song will appear in a bubble that allows the message recipient to see album art and preview the song.
song_url = 'https://open.spotify.com/track/5qqabIl2vWzo9ApSC317sa'
open_graph_element = UI::FBOpenGraphTemplate.new(song_url, [{title: 'Button', url: 'https://github.com/progapandist/rubotnik', type: 'web_url' }])
show(open_graph_element)here's the result:
Events other then message and postback are currently not supported.
Once you have designed your bot and tested in on localhost, it's time to send it to the cloud, so it live its life without being tethered to your machine. Assuming you already have a Heroku account and Heroku CLI tools installed, here's pretty much the whole process:
$ git init # if haven't done before
$ git add . && git commit -m "Ready to deploy!"
$ heroku create YOUR_APP_NAME
$ heroku config:set ACCESS_TOKEN=your_own_page_token
$ heroku config:set VERIFY_TOKEN=your_own_verify_token
$ git push heroku masterNow don't forget to go back to your Facebook developer console and change the address of your webhook from your ngrok's URL to the Heroku one. That's it!
More powerful DSL for parsing user input and binding it to commands.
NLP integration with Wit.AI (that allow for more things then the built-in NLP capabilities of Messenger platform, including a wrapper around Wit's interactive learning methods) is in the works and will be added to the gem some time in 2018...
Im a still a relatively new Ruby developer (I started coding in 2015 while looking for a break from my 10+ years career as a TV reporter) and this is my first attempt at OSS. Any contribution will be more than welcome! Rubotnik is still in the early stage of development, so it's your chance to make a difference!
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests (currently project has no tests, but you're welcome to land a helping hand). You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
The gem is available as open source under the terms of the MIT License.
No open issues yet, or sync has not completed.