LoginSignup
10
10

More than 5 years have passed since last update.

家庭用にHubotスクリプトを書いてみた話

Last updated at Posted at 2016-03-05

HerokuにHubot立ててみたの続きになります。

家族とのSlackチャット用にHubotスクリプトをいくつか書きました。

スクレイピング

Webサイトの情報を取得するには、cheerio-httpcliがラクチンです。jQuery風に取得できます。

client = require 'cheerio-httpcli'

client.fetch 'http://xxxxxx.com/', {q:'node.js'}, (err, $, res) ->
    console.log $('title').text()

スクリーンショット

スクリーンショットを取得できるパッケージはいくつかありますが、今回はphantomjs-prebuiltを使います。

childProcess = require 'child_process'
path = require 'path'
phantomjs = require 'phantomjs-prebuilt'

childArgs = [
    path.join(__dirname, './screenshot.js')
    [target-url]
    [picture-save-path]
 ]

#スクリーンショット取得
childProcess.execFile phantomjs.path, childArgs, (err, stdout, stderr) ->
screenshot.js
var system = require('system');
var args = system.args;
var url = args[1];
var savePath = args[2]

var page = require('webpage').create();
page.open(url, function() {
    page.render(savePath);
    phantom.exit();
});

gmail

こちらを参考に、gmailに届いたamazon発送メールをSlackに流すスクリプトを作りました。
Node.jsで社内日報メールを取得してFacebookページに自動投稿する仕組みを実装したので解説します。

iconvを使うためには、node-gypのRebuildが必要になります。WindowsだとC++コンパイルのためにVisual Studioが必要になるのですが、インストールが面倒だったので今回は使いませんでした。

cron

定期実行はnode-cronを使うとわりとかんたんです。

botRoom = "#general"
cronJob = require('cron').CronJob

module.exports = (robot) ->
  new cronJob(
    cronTime: "0 0 16 * * 3"  # 水曜の16:00
    start: true
    onTick: ->
      robot.send {room: "#{botRoom}"}, "your-message"
    )
10
10
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
10
10