φ(.. ) 備忘録
   
  

2014年6月7日土曜日

hubot-loggerを設定する

hubot-loggerを設定する。文字化け対策もやる。対策に使うnkf.plは本ブログの過去記事を参照すること。ログの保存はutf-8になる。noticeでもログはとる。設定は以下。
(1)~/tools/hubot/package.jsonを修正。青字追加。
   :
  "dependencies": {
    "hubot": "~2.5.1",
    "hubot-scripts": ">= 2.1.0",
    "optparse": "1.0.3",
    "hubot-irc": "0.1.x",
    "hubot-logger": "~0.0.11"  },
   :
(2)~/tools/hubot/external-scripts.jsonを以下の内容で作成。
["hubot-logger"]
(3)% npm install
(4)~/tools/hubot/node_modules/hubot-logger/scripts/hubot-logger.coffeeを修正。
        :
log_message = (root, date, type, channel, meta) ->
  mkdirp(path.resolve root, channel)
  log_file = path.resolve root, channel, date.toString("%Y-%m-%d") + '.txt'
  meta.date = date
  meta.channel = channel
  meta.type = type
  if meta.message
    cmd = "~/tools/hubot/perl/nkf.pl -in " + escape(meta.message)
    exec = require('child_process').exec
    exec cmd, (err,stdout,stderr) ->
      meta.message=stdout
      fs.appendFile log_file, JSON.stringify(meta) + '\n', (err) ->
        if err
          throw err
  else

    fs.appendFile log_file, JSON.stringify(meta) + '\n', (err) ->
      if err
        throw err
        :
    robot.adapter.bot.on 'message', (nick, to, text, message) ->
      result = (text + '').match(/^\x01ACTION (.*)\x01$/)
      if !result
        log_message(logs_root, new Tempus(), "message", to, {nick: nick, message: text, raw: message })
      else
        log_message(logs_root, new Tempus(), "action", to, {nick: nick, action: result[1], raw: message })
    robot.adapter.bot.on 'notice', (nick, to, text, message) ->
      result = (text + '').match(/^\x01ACTION (.*)\x01$/)
      if !result
        log_message(logs_root, new Tempus(), "message", to, {nick: nick, message: text, raw: message })
      else
        log_message(logs_root, new Tempus(), "action", to, {nick: nick, action: result[1], raw: message }) 

        :
(5) ~/tools/hubot/runbot.shに以下の設定追加。
export IRCLOGS_PORT=3001 #expressがlistenするポート番号
export IRCLOGS_FOLDER="chatlogs" #ログの保存先
以上を設定すると、http://localhost:3001/irclogsでログを参照することができる。

0 件のコメント :

コメントを投稿