LoginSignup
1
2

More than 3 years have passed since last update.

【Node.js × LINE WORKS API】監査ログをダウンロードする

Posted at

すんごいドマイナーな内容なのですが、やりたいことのために必要だったので調べました。( ゚Д゚)

ただ、勉強不足なせいか思ったより時間がかかってしまいました。。。
反省と自身の備忘録的にしっかりとアウトプットしておこうと思います(=_=)

LINE WORKS API

LINE WORKS で提供されている API です。
トーク Bot を操作したり、カレンダーやメールの内容を取ってきたりできます。
今回使用するのは 監査データのダウンロード API です。

LINE WORKS Developers

実行環境

実行環境は以下の通りです。

  • Node.js
  • Visual Studio Code
  • LINE WORKS API

事前準備

  1. Node.js をインストール
  2. Visual Studio Code をインストール
  3. LINE WORKS Developer Console で認証情報を取得
  4. 各モジュールをインストール
npm install request --save
npm install csv --save

API を Request するコード

download.js
// LINEWORKS 認証情報
const CONSTS = require("./consts.js");

const request = require('request');
const csv = require('csv');

const apiId = CONSTS.API_ID;
const feature = 'log';
const service = 'message';
const serviceId = 'audit';
const endDate = getUnixTime();
const startDate = Number(endDate) - 86400; // 24時間前から現在時刻までのログを取得
const tenantId = CONSTS.DOMAIN_ID;
const domainId = CONSTS.DOMAIN_ID;
const rangeName = 'tenant';
const uri = `https://audit.worksmobile.com/r/${apiId}/audit/v2/${feature}/${service}/logs.csv?apiId=downCsvLog&serviceId=${serviceId}&startDate=${startDate}&endDate=${endDate}&tenantId=${tenantId}&domainId=${domainId}&rangeName=${rangeName}`;

const options = {
  'method': 'GET',
  'url': uri,
  'headers': {
    'consumerKey': CONSTS.CONSUMER_KEY,
    'Authorization': `Bearer ${CONSTS.TOKEN}`
  }
};
request(options, function (error, response) {
    if (error) throw new Error(error);
    csv.parse(response.body,
        { bom: true, columns: true },
        (err, data) => { console.log(data) }
    )
});

function getUnixTime() {
  const date = new Date();
  const now = date.getTime();
  return Math.floor(now/1000);
};

コードについて補足

  • 認証情報は CONSTS.js ファイルにまとめてあります。
CONSTS.js
const DOMAIN_ID = 'xxxxxxxxxxxx';
const API_ID = 'xxxxxxxxxxx';
const CONSUMER_KEY = 'xxxxxxxxxxx';
const TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

exports.DOMAIN_ID = DOMAIN_ID;
exports.API_ID = API_ID;
exports.CONSUMER_KEY = CONSUMER_KEY;
exports.TOKEN = TOKEN;
  • 今回はトークログを取得しているので const service = 'message'; を指定しています。ログイン状況やメールなどのログも取得できます。詳しくは公式ドキュメントを参照ください。

  • トークログのみ unix timestamp 形式で時間単位の指定が可能なので unix timestamp 形式で指定しています。

  • UNIX time stamp 形式で指定する場合 24時間以内のデータしか取得できません。

  • Request URL、ちょー長いですよね!( ゚Д゚)メンドイ

  • 取得した CSV データには BOM が入っているので csv.parse するときにオプションを指定しています。{ bom: true, columns: true }

おわりに

ここまでお付き合いいただきありがとうございました。

冒頭にも書きましたが、やりたいことがあって・・・
Bot の入っていないトークルームの発言をトリガーにしたいんですよね。
本当は GAS で実装したかったのですが GAS での csv ダウンロードがどうにもうまくできなくて。
GAS は Node.js での実装が終わったら再チャレンジしようと思います。

ではまた!(^^)/

参考にさせていただきましたm(_ _)m

LINE WORKS Developers
Visual Studio Codeをインストールする

1
2
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
1
2