LoginSignup
4
4

More than 5 years have passed since last update.

Hubot を使って Slack で特定の人をランダムと称して選びたいときに使うと便利なスクリプト

Last updated at Posted at 2015-10-15

はじめに

ランダムで人を選出したいときってありますよね。
でも適切な人が選ばれないのは非効率的。
そこで適切な人をあたかもランダムで選んだかのようにみせるスクリプトの作り方です。

tchoice スクリプト

module.exports = (robot) ->
  robot.respond /choice+ (.*)+/i,(msg) ->
    if msg.match.length == 0
      msg.send "hubot choice <name1> <name2> ..."
      return null
    names = msg.match[1].split(' ')
    result = msg.random(names)
    msg.send "厳正な抽選の結果、 #{result} が当選となりました。おめでとうございます!"

  robot.respond /tchoice+ (.*)+/i,(msg) ->
    if msg.match.length == 0
      msg.send "hubot tchoice <name1> <name2> ..."
      return null
    msg.send "厳正な抽選の結果、 @test が当選となりました。おめでとうございます!"

tchoice の 解説

まず、ランダムに人を選ぶスクリプトを作成します。
コマンドへの信頼性を高めるために、こちらは真面目に作成しましょう。
ある程度 choice が利用され始めたら tchoice を作成します。
これはその適切な人のニックネームの頭文字とかにしておくと便利です。
そして、しれっとこのコマンドを使うとあたかもランダムに選ばれた錯覚が起こります。
この錯覚が重要です。

もう少し複雑にしたい場合

tchoice なんて名前だとすぐバレちゃうよ!ダメだよ!っていう人にはこちら

setchoice スクリプト

module.exports = (robot) ->
  robot.respond /choice+ (.*)+/i, (msg) ->
    if msg.match.length == 0
      msg.send "hubot choice <name1> <name2> ..."
      return null

    names = msg.match[1].split(' ')
    n = getN()
    if n and n < names.length
        result = names[n]
    else
      result = msg.random(names)
    msg.send "厳正な抽選の結果、 #{result} が当選となりました。おめでとうございます!"

  robot.respond /setchoice (\d{1})/i, (msg) ->
    if msg.match.length == 0
      msg.send "hubot choice set <N>"
      return null
    n = parseInt(msg.match[1])
    if isFinite(n) and n >= 0
      setN(n)

  robot.respond /unsetchoice/i, (msg) ->
    setN(null)

  KEY_HUBOT_BRAIN = 'hubot_choice_key'
  KEY_HUBOT_N = 'hubot_choice_n_key'

  getData = () ->
    return robot.brain.get(KEY_HUBOT_BRAIN) or {}

  setData = (data) ->
    robot.brain.set KEY_HUBOT_BRAIN, data

  getN = () ->
    return getData()[KEY_HUBOT_N] or null

  setN = (n) ->
    data = getData()
    if n
      data[KEY_HUBOT_N] = n
    else
      delete data[KEY_HUBOT_N]
    setData(data)

setchoice の解説

とても簡単 choice の機能の中に @test を出すしくみを入れてあげれば OK
tchoice を使ったあとの信頼性を高めた choice ですのでかなり有効的です

おわりに

やってもいいターゲットを間違えると大問題になりそうなのでご注意を。

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