Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9314

slackチャンネルからメッセージ全部取得したい

$
0
0

前書き

slackのワークフローを記述することでログ用のチャンネルにメッセージが送信され、
SlackAPP経由でGASが叩かれスプレットシートに追記されるシステムを作った。(現在システムに関する記事執筆中)
ログ用のチャンネルを見返してるのがつらくなったのでメッセージを全部取得するクソコードを書いた。

成果物

module/common.js
constfs=require('fs');exports.makeOutputDir=path=>{if(!isExistFile(path)){fs.mkdirSync(path);}}
main.js
constcommon=require('./module/common');constrequest=require('request');constfs=require("fs");main=(path,oldest='1587362942.000200')=>{consttoken='トークン'constchannel='チャンネル';leturl='https://slack.com/api/conversations.history?token='+token+'&channel='+channel+'&oldest='+oldest+'&pretty=1'varoptions={url:url,method:'POST'};functioncallback(error,response,body){letoldest_t;if(!error&&response.statusCode==200){constobj=JSON.parse(body);if(obj.messages.length==0){return}obj.messages.reverse().forEach(element=>{consttext="\""+element.text.replace(/,/g,'').replace(/\n/g,'').trim()+"\",\n";console.log(text)fs.appendFileSync(path+'out.csv',text);console.log('write end');oldest_t=element.ts})}main(path,oldest_t);}request(options,callback);}(()=>{constdate=newDate();constpath='output/'+date.getFullYear()+'-'+('00'+(date.getMonth()+1)).slice(-2)+'-'+('00'+date.getDate()).slice(-2)+'/';common.makeOutputDir(path);main(path);})();

説明

https://api.slack.com/methods/conversations.history
こいつを使えば良いだけです!
後は取得した内容を吐き出すだけ!
でも100件しか取得できないから現状2000件を超えるメッセージの全取得は無理だ。
なのでタイムスタンプを保存して続きのメッセージを取得するように再帰してる。

トークンの作り方は別の記事に書いてる。(執筆中)
でもググればめっちゃ出てくるからぶっちゃけ僕が書く必要全くないと思う。

以上!!!


Viewing all articles
Browse latest Browse all 9314

Trending Articles