LoginSignup
10
8

More than 5 years have passed since last update.

RubyのFaaSでslackbotをつくる

Last updated at Posted at 2018-10-18

最近自分の中で話題のサービスFaaStRubyを使ってSlack Botを作成します
FaaStRubyって何?って方はこちらをどうぞ

環境

faastruby : 0.2.1

slack側の設定

webhookの登録

今回はOutgoing Webhooksを使ってbotを作成するので、その設定を行う

https://teamname.slack.com/apps/manage/custom-integrations
ここからoutgoing webhooksのintegrationを作成する

Screenshot from 2018-10-18 21-59-22.png

outgoing webhooksが登録されていない場合は、https://teamname.slack.com/apps の検索窓から
「outgoing webhooks」で検索し作成する

Screenshot from 2018-10-18 22-02-29.png

Screenshot from 2018-10-18 22-05-28.png

Screenshot from 2018-10-18 22-05-39.png

作成したらIntegration SettingsのchannelとURLに、それぞれBotを入れたいチャンネルとfaastrubyで作ったエンドポイントを入力する
※ エンドポイントについては後述

Screenshot from 2018-10-18 22-13-15.png

FaaStRuby側の設定

こちら側ではbotの中身を作っていく
今回は簡単にユーザー名とユーザーが入力した言葉を返すbotを作る

Gemのインストール

$ gem install faastruby

ワークスペースの作成

今回はslackbotという名前でワークスペースを作成した
恐らく重複しなければ好きな名前のものが作成できる

$ faastruby create-workspace slackbot

Functionの作成

こちらもslackbotで作成している

$ faastruby new slackbot

上のコマンドを実行すると、以下のファイルが自動生成される

workspace(slackbot)
   ├─ function(slackbot)
       ├─ Gemfile
       ├─ handler.rb 
       ├─ faastruby.yml
       └─ spec
           ├─ handler_spec.rb
           ├─ spec_helper.rb
           └─ helper
               └─ faastruby.rb

この中のhandler.rbがfunctionの中身

botの実装はこんな感じ

handler.rb
require 'json'

def handler event
  data = event.body ? JSON.parse(event.body) : {}
  response = {}
  if data['user_name'] != 'slackbot'
    response['text'] = "#{data['user_name']} : #{data['text']}"
  end
  respond_with response
end

eventはリクエスト情報を持つもので、event.bodyにリクエストの中身がstringで入っている
outgoing webhookのbotでは、以下のようなデータが渡されてくる

token=*********************
team_id=T0001
team_domain=example
channel_id=C2147483705
channel_name=test
timestamp=1355517523.000005
user_id=U2147483697
user_name=Steve
text=googlebot: What is the air-speed velocity of an unladen swallow?
trigger_word=googlebot:

respond_with responseでレスポンスを返す
以下のJSONを返すと、botがチャンネルの中でtextの中身をつぶやいてくれる

{
  "text": "African or European?"
}

デプロイ

最後にFaaStRubyのサーバーにデプロイをするが、その前にfaastruby.ymlを少し書き換えておく

faastruby.yml
---
name: slackbot  # function名
test_command: rspec    # テストコマンド
abort_build_when_tests_fail: false    # テストが失敗したときにビルドをやめるか
abort_deploy_when_tests_fail: false    # テストが失敗したときにデプロイをやめるか

そのままだと、後半2つの設定をfalseにしておかないとテストで落ちてデプロイできない
spec/handler_spec.rbのテストを、今回用に書き直せばtrueでもデプロイできる

デプロイコマンド

$ cd slackbot
$ faastruby deploy-to slackbot

デプロイが終わると以下のようなエンドポイントが作成される

https://api.faastruby.io/slackbot/slackbot

workspaceとfunctionの名前同じにしたの失敗した...
どっちがどっちかわからない:thinking:
エンドポイントはこういう形になる

https://api.faastruby.io/workspace-name/function-name

このエンドポイントを、上のSlack Webhooksの設定に入れることでbotを動かすことができる

デモ

スクリーンショット 2018-10-19 11.36.04.png

問題なく動いた

結論

FaaStRubyはいいぞ

参考URL

SlackのOutgoing Webhooksを使って投稿に反応するbotを作る

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