LoginSignup
9
8

More than 5 years have passed since last update.

Hubot | 自作Script | Hubot で 「今日は何の日」API の結果を取得する

Last updated at Posted at 2014-08-28

Hubot | 自作Script | Hubot で 「今日は何の日」API の結果を取得する

概要

Hubot で 「今日は何の日」API の結果を取得します

機能

  • MMDD で日時を指定できる
  • dekigoto / tanjyoubi / imibi / kinenbi を指定可能(キー名は 「今日は何の日」API のキー名のまま)
  • 結果のリストからランダムで一件取得する
  • WikipediaのURLを表示する

正規表現

/daytopic (\d{4}){1} (.*)$/i

ソースコード

daytopic.coffee
# Description
#   Output daytopic
#
# Dependencies:
#   None
#
# Configuration:
#   None
#
# Commands:
#   hubot daytopic <MMDD> dekigoto... - Output daytopic dekigoto
#   hubot daytopic <MMDD> tanjyoubi... - Output daytopic tanjyoubi
#   hubot daytopic <MMDD> imibi... - Output daytopic imibi
#   hubot daytopic <MMDD> kinenbi... - Output daytopic kinenbi
xml2js = require('xml2js')
parseXML = xml2js.parseString
module.exports = (robot) ->
  robot.respond /daytopic (\d{4}){1} (.*)$/i, (msg) ->
    month_day = msg.match[1]
    category = msg.match[2]
    if (['dekigoto', 'tanjyoubi', 'imibi', 'kinenbi'].indexOf(category) < 0)
        category = 'dekigoto'

    robot.http("http://www.mizunotomoaki.com/wikipedia_daytopic/api.cgi/#{month_day}")
      .get() (err, res, body) ->
        parseXML body, (err, xml) ->
          title = xml.feed.title
          wikipedia = xml.feed.wikipedia
          items = xml.feed[category][0].item
          item = msg.random(items)
          msg.send "title = #{title}"
          msg.send "#{category} = #{item}"
          msg.send wikipedia

出力

shell adapter で確認

Hubot> hubot daytopic 0101 dekigoto
title = 1月1日
Hubot> dekigoto = 1806年 - バイエルン大公国がバイエルン王国、ヴュルテンベルク公 国がヴュルテンベルク王国となる。
Hubot> http://ja.wikipedia.org/wiki/1%E6%9C%881%E6%97%A5
Hubot> hubot daytopic 0101 dekigoto
title = 1月1日
Hubot> dekigoto = 1700年 - ロシアがユリウス暦へと切り替わる。
Hubot> http://ja.wikipedia.org/wiki/1%E6%9C%881%E6%97%A5
Hubot> hubot daytopic 0101 tanjyoubi
title = 1月1日
Hubot> tanjyoubi = 1971年 - 草場道輝、漫画家
Hubot> http://ja.wikipedia.org/wiki/1%E6%9C%881%E6%97%A5
Hubot> hubot daytopic 0101 imibi
title = 1月1日
Hubot> imibi = 1881年 - ルイ・オーギュスト・ブランキ、社会主義革命家
Hubot> http://ja.wikipedia.org/wiki/1%E6%9C%881%E6%97%A5
Hubot> hubot daytopic 0101 kinenbi
title = 1月1日
Hubot> kinenbi = 四方拝
Hubot> http://ja.wikipedia.org/wiki/1%E6%9C%881%E6%97%A5

kandan adapter で確認

daytopic.png

9
8
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
9
8