LoginSignup
6
4

More than 5 years have passed since last update.

Ruboty | Ruboty を魔改造してみる #ruboty

Last updated at Posted at 2014-12-17

Ruboty | Ruboty を 魔改造 してみる

概要

Ruboty の設定を変更したり基本機能を拡張したりしてみます

初級編: Ruboty の 名前を Hubot に変更する

環境変数 RUBOTY_NAME でボットの名前を hubot に変更します

  • .env ファイルに環境変数を設定
RUBOTY_NAME=hubot
  • shell adapter で動作確認 ruboty に反応せず、 hubot に反応するようになりました
$ bundle exec ruboty --dotenv
> ruboty ping
> hubot ping
pong

中級編: ruboty コマンドの load オプションを利用して一時的に ruboty 本体の処理を変える

Ruboty::Adapters::Shell#say の動作を無理やり変更した pseudo_shell_adapter.rb を用意し、
挙動を変更してみます。

  • Ruboty::Adapters::Shell#say の元々の処理
def say(message)
  Ruboty.logger.info(message[:body])
end
  • pseudo_shell_adapter.rb オープンクラスで処理内容を変更し、 say の直前に message の中身を inspect してみます
pseudo_shell_adapter.rb
module Ruboty
  module Adapters
    class Shell < Base
      def say(message)
        Ruboty.logger.info(message.inspect)
        Ruboty.logger.info(message[:body])
      end
    end
  end
end
  • 試行 pong の直前に message インスタンスの中身が表示されるようになりました
$ bundle exec ruboty --load pseudo_shell_adapter.rb
> ruboty ping
{:body=>"pong", :from=>nil, :to=>nil, :original=>{:body=>"ruboty ping", :source=
>"shell", :robot=>#<Ruboty::Robot:0x2953950 @options={:dotenv=>nil, :generate=>n
il, :load=>"pseudo_shell_adapter.rb", :help=>nil}, @memoized_table={:env=>"devel
opment", :brain=>#<Ruboty::Brains::Memory:0x35bdee0>, :handlers=>[#<Ruboty::Hand
lers::Help:0x35bdd78 @robot=#<Ruboty::Robot:0x2953950 ...>>, #<Ruboty::Handlers:
:Ping:0x35bdd48 @robot=#<Ruboty::Robot:0x2953950 ...>>, #<Ruboty::Handlers::Whoa
mi:0x35bdd18 @robot=#<Ruboty::Robot:0x2953950 ...>>, #<Ruboty::Handlers::Wareki:
0x35bdcd0 @robot=#<Ruboty::Robot:0x2953950 ...>>], :adapter=>#<Ruboty::Adapters:
:Shell:0x35bdc10 @robot=#<Ruboty::Robot:0x2953950 ...>, @history_pathname=#<Path
name:/path/to/your/ruboty_history/.ruboty_history>, @history_file=#<File:/path/t
o/your/ruboty_history/.ruboty_history>>}>}}
pong

上級編: gem を作成せずに ruboty handler plugin の動作を確認する

load オプションの一番の用途って、この使い方を想定しているのかもしれないので
魔改造でもなんでもないかもしれないが、この記事の中で一番実用性があるので上級編にした。

Handler Plugin のコード

hoge に対して hage を返却するのみ。

pseudo_handler_plugin.rb
module Ruboty
  module Handlers
    class Hoge < Base
      on /hoge\z/i, name: "hoge", description: "Return hage"

      def hoge(message)
        message.reply("hage")
      end
    end
  end
end

実行

未公開の gem を handler のソースコードのみで動作確認できました。

$ bundle exec ruboty --load pseudo_handler_plugin.rb
> ruboty hoge
hage
6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4