LoginSignup
7
7

More than 5 years have passed since last update.

Ruboty | 自作 Handler gem の作成 #ruboty #chatops

Posted at

Ruboty | 自作 Handler gem の作成 #ruboty #chatops

概要

Ruboty の 自作 Handler gem の作成に関して。

※自作の Handler といいつつ、実際は Handler + Action のセットで
一つの振る舞いを実装することになるのですが、このワンセットを表す名前ってあるのかな?
概念を共有するうえで、名前重要。

【妄想】
ロボット的なメタファだと Parts とか Custom Parts とか?
自作側を Custom Parts とするなら、組み込み側は Core Parts とか?

資料

Handler と Action については下記記事を参照
Ruboty | Handlers と Actions

前提

  • qiita_scouter gem を利用します

サンプル

仕様

私の自作 gem qiita_scouter の機能をチャット経由で利用できるように
Handler を作成します。

qiita_scouter については下記記事を参照。
【N番煎じ】Ruby で Qiita 戦闘力を計測するQiitaScouter を作ってみた

  • 設定値仕様
key value
name analyze
pattern /qiita scouter (analyze|a) (?<id>.*?)\z/
description analyze qiita power

構成

$ tree
.
┣ Gemfile
┣ lib
┃ ┗ ruboty
┃     ┣ qiita_scouter
┃     ┃ ┣ actions
┃     ┃ ┃ ┗ analyze.rb
┃     ┃ ┗ version.rb
┃     ┣ qiita_scouter.rb
┃     ┗ handlers
┃         ┗ qiita_scouter.rb
┣ LICENSE.txt
┣ Rakefile
┣ README.md
┗ ruboty-qiita_scouter.gemspec

手順

※今回はテストを作りません。

$ bundle gem ruboty-qiita_scouter
$ cd ruboty-qiita_scouter
  • Gemfile の編集
source 'https://rubygems.org'

gemspec
gem 'qiita_scouter'
$ bundle install
  • gemfile, LICENSE.txt, README.md などを編集

  • ディレクトリの作成

$ mkdir ./lib/handlers
$ mkdir ./lib/ruboty/qiita_scouter
$ mkdir ./lib/ruboty/qiita_scouter/actions
  • lib/ruboty/qiita_scouter.rb
require 'qiita_scouter_core'

require "ruboty/qiita_scouter/actions/analyze"
require "ruboty/qiita_scouter/version"
require "ruboty/handlers/qiita_scouter"
  • lib/ruboty/handlers/qiita_scouter.rb
module Ruboty
  module Handlers
    class QiitaScouter < Base
      on(
        /qiita scouter (analyze|a) (?<id>.*?)\z/,
        name: 'analyze',
        description: 'analyze qiita power'
      )

      def analyze(message)
        Ruboty::QiitaScouter::Actions::Analyze.new(message).call
      end
    end
  end
end
  • lib/ruboty/qiita_scouter/actions/analyze.rb
module Ruboty
  module QiitaScouter
    module Actions
      class Analyze < Ruboty::Actions::Base
        def call
          power_levels = ::QiitaScouter::Core.new.analyze(id)
          msg = format("ユーザー名: %s 戦闘力: %s 攻撃力: %s 知力: %s すばやさ: %s\n", id, *power_levels)
          message.reply(msg)
        rescue => exception
          message.reply("Failed by #{exception.class}")
        end

        private

        def id
          message[:id]
        end
      end
    end
  end
end
  • release
$ rake build
# geminabox で作成した private gem server にアップ
$ gem inabox ./pkg/ruboty-qiita_scouter-0.0.1.gem

Ruboty からの呼び出し検証

$ cd "ボットを作成する任意のディレクトリ"
$ ruboty -g
  • Gemfile の編集
source "http://your private gem server or rubygems/"

gem "ruboty-qiita_scouter"
  • bundle install
$ bundle install
  • run
$ bundle exec ruboty
> ruboty help
ruboty /qiita scouter (analyze|a) (?<id>.*?)\z/ - analyze qiita power
ruboty /help( me)?\z/i                          - Show this help message
ruboty /ping\z/i                                - Return PONG to PING
ruboty /who am i\?/i                            - Answer who you are
> ruboty qiita scouter analyze tbpgr
ユーザー名: tbpgr 戦闘力: 200816 攻撃力: 21280 知力: 173040 すばやさ: 6496
> ruboty qiita scouter a tbpgr
ユーザー名: tbpgr 戦闘力: 200816 攻撃力: 21280 知力: 173040 すばやさ: 6496
> ruboty qiita scouter analyze invalid_user
Failed by NoMethodError

参照

Ruboty | 基本構成図解
Ruboty GitHub
Ruboty RubyGems
Qiita-rb GitHub
Ruboty GoogleImage GitHub
【N番煎じ】Ruby で Qiita 戦闘力を計測するQiitaScouter を作ってみた

7
7
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
7
7