On Github ryoe / upstate-ruby-lita-talk
A number of companies that are not world famous. (Yet?)
Currently, Clown Shoes Wrangler at RaiseMore.
Love 'em or hate 'em, you can't avoid 'em
$ lita new create lita create lita/Gemfile create lita/lita_config.rb:-D
Lita.configure do |config| # The name your robot will use. config.robot.name = "Johnny Five" end:-D
Lita.configure do |config| config.robot.adapter = :slack config.adapters.slack.token = "my-token" end
source "https://rubygems.org" gem "lita" gem "lita-slack"This example is Slack. There are several other config options, so always consult the docs for your chosen adapter. Add the gem, bundle install and lita start!
module Lita module Handlers class Ping < Handler route(/ping/, :pong, help: { "ping" => "replies back with PONG" }) def pong response response.reply "PONG" end Lita.register_handler(self) end end end
require './handlers/ping.rb' Lita.configure do |config| # my config end:-D
module Lita module Handlers class Ping < Handler route(/ping/, :pong, command: false, # defaults false help: { "ping" => "replies back with PONG" }) def pong response response.reply "PONG" end Lita.register_handler(self) end end end:-D
module Lita module Handlers class Gnip < Handler route(/gnip/, :gnop, command: true, # "respond" help: { "gnip" => "replies back with GNOP" }) def gnop response response.reply "GNOP" end Lita.register_handler(self) end end end
require './handlers/gnip.rb' # the rest...command: true makes it "respond" only
Lita > I say ping PONG Lita > lita ping PONG
Lita > I say gnip Lita > lita gnip GNOP"Hear" is basically the honey badger! It don't care!
module Lita module Handlers class Helpers < Handler route(/helper[s|z] (\d*){1}/i, :puts_helper, command: true) def puts_helper response puts "matches: #{response.matches}" puts "match_data: #{response.match_data}" puts "args #{response.args}" puts "message: #{response.message}" puts "user: #{response.user}" puts "extensions: #{response.extensions}" response.reply_privately "Shh! This is private!" response.reply "Hola!" end Lita.register_handler(self) end end end
Lita > lita helpers 42 matches: [["42"]] match_data: helpers 42 args ["42"] message: #<:message:0x007fbb91398da8> user: #<:user:0x007fbb91a362a0> extensions: {} Shh! This is private! Hola! Lita > lita helpers 42 21 matches: [["42"]] match_data: helpers 42 args ["42", "21"] message: #<:message:0x007fd06c333dc8> user: #<:user:0x007fd06c34a1e0> extensions: {} Shh! This is private! Hola!
module Lita module Handlers class Stars < Handler route(/star[s|z] (\d*){1}/i, :star_rating, command: true, help: { "stars <num>" => "replies with your star rating" }) def star_rating response starz = response.args.first.to_i if starz > 3 response.reply "Wow! #{starz} stars!\nYou must be an internationally recognized expert!" else response.reply "You are not your star count! We <3 you!" end end Lita.register_handler(self) end end end
Lita > lita stars 2 You are not your star count! We <3 you! Lita > lita stars 4 Wow! 4 stars! You must be an internationally recognized expert!:-D
module Lita module Handlers class Destroyer < Handler route(/destroy everything/, :destroy_it, command: true, restrict_to: [:authorized_destroyers]) def destroy_it response response.reply "Sweet! Let's break stuff!" end Lita.register_handler(self) end end endFor this work, you'll have to configure "Admins" in lita_config.rb. Then admins can add users to groups.
Lita.configure do |config| # add adapter-specific ids config.robot.admins = ["42", "21"] end
Lita > lita auth add Michelle to authorized_destroyers Michelle was added to authorized_destroyers Lita > lita auth remove Michelle to authorized_destroyers Michelle was removed to authorized_destroyers
module Lita module Handlers class Nerdbeers < Handler route(/nerdbeers/i, :nerdbeers, command: true, help: { "nerdbeers" => "replies with current nerdbeers agenda" }) def nerdbeers response http_response = http.get "http://nerdbeers.com/api/" agenda = MultiJson.load http_response.body message = [] agenda['pairings'].each {|a| message.push "Pairing ##{a['id']} => Topic: #{a['topic']} Beer: #{a['beer']}" } response.reply message.join "\n" end Lita.register_handler(self) end end endclass var http is actually a Faraday::Connection. So anything you can do with Faraday, you can do here.
Lita > lita nerdbeers Pairing #1 => Topic: everything should be https Beer: coop f5 Pairing #2 => Topic: Google IO Beer: native amber Pairing #3 => Topic: home automation in 2016 Beer: goose island ipa
module Lita module Handlers class UpstateHttp extend Lita::Handler::HTTPRouter http.get "/upstate", :ruby def ruby request, response response.body << "Ruby" end Lita.register_handler(UpstateHttp) end end endWhat kind of ChatOps discussion can this be with a "pinger" route? You don't have to inherit directly from Lita's Handler class. Notice this time we're extending HTTPRouter "Mixin" style.
module Lita module Handlers class StarsHttp < Handler http.get "/stars/:num", :star_rating def star_rating request, response starz = request.env["router.params"][:num].to_i message = starz < 4 ? "You are not your star count! We <3 you!" : "Wow! #{starz} stars!\nYou must be an internationally recognized expert!" body = { starz: starz, message: message } response.headers["Content-Type"] = "application/json" response.write MultiJson.dump(body) end Lita.register_handler(self) end end end
$ lita handler lita-openworks-status-handler Do you want to test your plugin on Travis CI? ("yes" or "no", default is "no") no Do you want to generate code coverage information with SimpleCov and Coveralls.io? ("yes" or "no", default is "no") no create lita-openworks-status-handler/lib/lita/handlers/openworks_status_handler.rb create lita-openworks-status-handler/lib/lita-openworks-status-handler.rb create lita-openworks-status-handler/spec/lita/handlers/openworks_status_handler_spec.rb create lita-openworks-status-handler/spec/spec_helper.rb create lita-openworks-status-handler/locales/en.yml create lita-openworks-status-handler/templates/.gitkeep create lita-openworks-status-handler/Gemfile create lita-openworks-status-handler/lita-openworks-status-handler.gemspec create lita-openworks-status-handler/.gitignore create lita-openworks-status-handler/Rakefile create lita-openworks-status-handler/README.md If you plan to release this plugin as open source software, consider adding a LICENSE file to the root of the repository. Common open source software licenses can be found at http://choosealicense.com/.Lita really encourages building handlers as RubyGems and testing with RSpec. Here's what the lita handler generator produces. Don't accidentally do this in your lita service project. :D
Sample Scriptshttps://github.com/ryoe/upstate-ruby-demo
Comments or Questions?@ryoe_ok on Twitterryoe on GitHub