LoginSignup
23
23

More than 5 years have passed since last update.

HubotでSlackのEmoji Reactionを付ける

Last updated at Posted at 2015-12-31

こんばんは,2015年もあと1時間を切りましたね.
親戚宅で紅白を見ていたら,μ'sの登場で気不味くなった@hico_horiuchiです.

先日の記事で紹介しましたが,BotkitではSlackのEmoji Reactionを簡単に付けられます.

ふと思い付いたので,Hubotで同じことを出来ないか試してみました.

reactions.add method

Slack APIに,Emoji Reactionを付けるメソッドがありました.
Botkitでは,このAPIをそのまま呼び出しているようです.
(というか, botkit/lib/Slack_web_api.js をそのまま使った方が早そう….)

/api/reactions.add に,以下のパラメタをPOSTすれば良いみたい.

  • トークン( HUBOT_SLACK_TOKEN を使えそう)
  • Reactionを付けるメッセージのチャンネルとタイムスタンプ

hubot-scriptを書いてみる

Emoji Reactionを付けるhubot-scriptを書いてみます.
今回は「gj」「GJ」のメッセージに :thumbsup: のEmojiを付けます.

scripts/reactions.coffee
# Description                                                                                                           
#   SlackのEmoji Reactionを付与                                                                                         
#                                                                                                                       
# Configuration:                                                                                                        
#   HUBOT_SLACK_TOKEN 

request = require('request')

module.exports = (robot) ->
  addReactions = (msg, name) ->
    options = {
      url: 'https://slack.com/api/reactions.add'
      qs: {
        'token': process.env.HUBOT_SLACK_TOKEN
        'name': name                                                                                                    
        'channel': msg.message.rawMessage.channel
        'timestamp': msg.message.rawMessage.ts
      }
    }
    request.post options, (err, res, body) ->
      if err? or res.statusCode isnt 200
        robot.logger.error("Failed to add emoji reaction #{JSON.stringify(err)}")

  robot.hear /^gj|GJ$/i, (msg) ->
    addReactions(msg, 'thumbsup')

単純にrequestを使ってWeb APIを呼び出しています.
channeltimestamp は,Hubotが受け取った msg を解析して発見しました.
(それぞれ C12345678901234567890.123456 いう形式なので,注意が必要です.)

hubot_add-reactions.gif

このような感じで,HubotがEmoji Reactionを付けているのが分かります.

(比較) BotkitでEmoji Reactionを付ける

botkit/bot.jsより,一部改変して抜粋しています.

bot.js
controller.hears(['gj', 'GJ'], 'message_received', function(bot, message) {
  bot.api.reactions.add({
    timestamp: message.ts,
    channel: message.channel,
    name: 'thumbsup',
  }, function(err, res) {
    if (err) {
      bot.botkit.log('Failed to add emoji reaction ', JSON.stringify(err));
    }
  });
};

まとめ

HubotからもSlack APIを呼び出すことでEmoji Reactionを付けられました.
このようなSlack特有(Adapterのサポート外)の機能は,Botkitの方に分がありそうですね.

本当はGitHub Integrationの通知にEmoji Reactionを付けたかったのですが,
Hubotでは他のBotからの通知は robot.hear で取得できないようです….
(「Pull request closed:」とかに :thumbsup: を付けたかった.)

Botkitの登場で来年のChatOps界隈はどうなるのか,楽しみですね.
それではみなさん,良いお年を.

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