hubot-jvn
Version:
Hubot-scripts returns JVN feed information from https://jvn.jp
73 lines (65 loc) • 2.48 kB
text/coffeescript
BRAIN_KEY = 'jvn::modified'
FEED_URL = 'https://jvn.jp/rss/jvn.rdf'
CRON_TIME = process.env.HUBOT_JVN_CRON_TIME or '00 15 * * * *'
CRON_MESSAGE = process.env.HUBOT_JVN_CRON_MESSAGE or ''
ERROR_MESSAGE = process.env.HUBOT_JVN_ERROR_MESSAGE or ''
REPLY_MESSAGE = process.env.HUBOT_JVN_REPLY_MESSAGE or ''
xml2js = require 'xml2js'
cron = require 'cron'
module.exports = (robot) ->
getItems = (callback) ->
robot.http(FEED_URL).get() (err, res, body) ->
if res.statusCode is not 200
callback {statusCode: res.statusCode}
else
(new xml2js.Parser()).parseString body, (err, json) ->
if err
callback err
else
callback null, json['rdf:RDF'].item
createMessage = (items, prefix) ->
resStr = ("#{i.title} #{i.link[0]}" for i in items).join('\n')
if resStr and prefix
resStr = "#{prefix}\n#{resStr}"
resStr
robot.respond /jvn( (\d+))?/i, (msg) ->
getItems (err, items) ->
if err and ERROR_MESSAGE
msg.send ERROR_MESSAGE
else
count = msg.match[2] or 5
msg.send createMessage items[...count], REPLY_MESSAGE
new cron.CronJob CRON_TIME, ->
getItems (err, items) ->
if items and items.length > 0
modified = robot.brain.get BRAIN_KEY
modified = modified and new Date modified
items = items.filter (i) ->
date = new Date(i['dcterms:modified'][0])
not modified or date > modified
resStr = createMessage items, CRON_MESSAGE
resStr and robot.messageRoom process.env.HUBOT_JVN_ROOM, resStr
robot.brain.set BRAIN_KEY, new Date().toString()
robot.brain.save()
, null, true