LoginSignup
6
7

More than 5 years have passed since last update.

lita-slackでIDのわかってるユーザー / チャンネルにBotから話しかける

Posted at

Ruby製のBot lita でSlack Botを作り始めた。

handlerからSlack上のIDがわかってるチャンネル / ユーザーにBotから話しかける方法を調べたのでメモしておく。

手順

ユーザーを探す

litaの機能でIDを調べておく。

lita users find #{USER_NAME}

# => #{SCREEN_NAME} (ID: #{USER_ID}, Mention name: #{USER_NAME})

handlerのコンテキストでユーザーを取得する。

user = User.find_by_id("#{USER_ID}")

チャンネルを探す

Slack appなどからチャンネルのIDを調べておく。

Add a Service Integration ...

などを開くと、URLにチャンネルのID文字列がついてるので控えておく。

handlerのコンテキストでチャンネルを取得する。

room = Lita::Room.find_by_id("#{CHANNEL_ID")

メッセージを送る

取得したuser / channel から Lita::Source を作り、 Lita::Robot#send_message を叩く。 Lita::Robot はヘルパーメソッド robot で取得できる。


source = Lita::Source.new(user: user)

# or

source = Lita::Source.new(room: room)

robot.send_message(source, 'hai!')
6
7
1

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
7