LoginSignup
18
14

More than 5 years have passed since last update.

hubot-slack で特定ユーザーに DM を送る方法

Posted at

対象

  • hubot-slack ver 3.4.2
  • node-slack-client ver 1.4.0

事象

hubot-slack では、room にユーザー名(@hoge の場合 "hoge")を渡してやると、特定ユーザーに DM を送信することができる。

robot.send {room: userName}, message
=> userName に 内容が message の DM が送信される。

ただし、hubot-slack の ver 3.4.2(2015/12/15 時点の最新)では、
DMインスタンスが生成されていない場合は、ユーザーにDMを送信することができない。

robot.send {room: userName}, message
=> userName の DM がオープンされていない場合は送信されない。

原因

ユーザーのDMインスタンスが生成されていない。

解決方法

robot.adapter.client.openDM(userId, callback) により、送信前に DM をオープンしておく。

sendDM = (userName, message) ->
  # userName は slack のユーザー名(@hoge の場合は "hoge")
  # slack の userID を取得
  userId = robot.adapter.client.getUserByName(userName)?.id
  return unless userId?

  robot.adapter.client.openDM userId, (data) ->
    robot.send {room: userName}, message
18
14
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
18
14