0
0

More than 5 years have passed since last update.

hubotでAdventarから記事を取得しSlackに表示する

Posted at

アドベントカレンダーの季節ですね。
わたしたちの研究室でも、Adventarで絶賛アドベントカレンダー中です。

さて、チャットツールにSlackを活用しているのですが、アドベントカレンダーの情報がSlackに流れれば便利ですね。

スクリーンショット 2016-12-07 04.08.58.png

cronで更新があるか回せばいいんですが、手動でチェックするのも侘び寂びがあるので今回はその方法を紹介します。

スクリーンショット 2016-12-07 04.08.08.png

hubotスクリプト

Adventarからデータを取得するためのパッケージがあるので、これを使います。

akameco/adventar: Retrieve articles from adventar

インストール

$ npm install --save adventar

使い方

リンクかアドベントカレンダーのidを引数にとり、{date: ?string, user: ?string, title: ?string, url: ?string}のオブジェクト25個の配列を返します。

adventar('1536').then(res => {
  const {date, user, title, url} = res[2];
  console.log(`今日(${date})は${user}さんの「${title}」です\n${url}`);
});
// => 今日(12/03)はatsuo1203さんの「岩井研残留日誌 - あつおの日常~あつおと過ごした365日~」です
// => http://atsuocps.hatenablog.com/entry/zanryuDiary

hubot

もちろんCoffeeScriptを使ってる人はもういないので、JavaScriptでbotのコードを書きます。
scripts以下に任意の名前で配置すれば動きます。

'use strict';
const adventar = require('adventar');

const id = '1536';

module.exports = robot => {
    robot.hear(/今日のアドベントカレンダー/, msg => {
        const d = (new Date()).getDate();
        adventar(id).then(res => {
            const {user, url} = res[d - 1];
            const mes = url ? url : `まだ記事がないようです担当は${user}です`;
            msg.send(mes);
        });
    });
};

以上です。
みなさまのアドベントカレンダーが完走できることを願っています。

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