6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Slackメッセージをesaリアクションでesaにぶん投げるやつ

Posted at

こういうの。

out.gif

esaっていうカスタム絵文字を追加して、↓みたいなHubot scriptを書いた。

# Description:
#   特定のリアクションがついたメッセージをesaに投稿する
#
# Notes:
#   Slackだと流れていってしまう作業メモなどをesaに書きとめておく
#   環境変数にesaのアクセストークン,チーム名,投稿カテゴリを設定しておく
#     `heroku config:set ESA_ACCESS_TOKEN=xxx`
#     `heroku config:set ESA_TEAM_NAME=yyy`
#     `heroku config:set ESA_BOT_CATEGORY=zzz`
#

decode = require 'decode-html'

handleReaction = (res) ->

  message = res.message
  item = message.item
  reaction = message.reaction
  reaction_type = message.type
  return unless reaction_type is 'added'

  if reaction is 'esa'
    res.robot.adapter.client.web.channels.history item.channel, {oldest: item.ts.split('.')[0], count: 1}, (err, data) ->
      if err
        res.send "Failed to post :( #{err}"
      else
        if data.messages.length > 0
          text = decode data.messages[0].text
          data = JSON.stringify {
            post: {
              name: text.split(/\n/)[0],
              body_md: text,
              category: process.env.ESA_BOT_CATEGORY,
              user: 'esa_bot',
              message: 'Add post from slack',
            }
          }
          res.robot.http("https://api.esa.io/v1/teams/#{process.env.ESA_TEAM_NAME}/posts")
            .header('Content-Type', 'application/json')
            .header('Authorization', "Bearer #{process.env.ESA_ACCESS_TOKEN}")
            .post(data) (err, r, body) ->
              if err
                res.send "Failed to post :( #{err}"
              else
                res.send "Post to #{(JSON.parse body).url}"

module.exports = (robot) ->
  robot.react handleReaction

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?