-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (58 loc) · 1.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const ws = require('ws');
const request = require('request');
const config = require('./config');
const debug = process.argv[2];
const HOST = config.HOST;
const TOKEN = config.TOKEN;
const STREAM = config.STREAM;
const POST_TO = config.POST_TO;
// WebSocket接続の確立
const mas = new ws(HOST + '/api/v1/streaming?access_token=' + TOKEN + '&stream=' + STREAM);
// toot受信時
mas.on('message',(data,flags) => {
let raw = JSON.parse(data);
are(raw).catch(e => {
console.log("An Exception has occurred.");
console.dir(e);
});
});
// なぞのかんすう
const are = async (raw) => {
if(typeof raw.event !== 'undefined' && raw.event === 'update'){
let toot = JSON.parse(raw.payload);
if(debug === 'debug')console.dir(toot);
console.log(toot.created_at + " from:" + toot.account.username);
console.log("URI : " + toot.uri);
console.log(toot.content);
if(debug !== 'debug')send_to_miss(toot.uri);
}else{
console.log("Status skipped because that is not 'update'.");
console.dir(raw);
}
console.log("=============================================================");
};
// 飛ばし関数
const send_to_miss = (uri) => {
request.post(
{
uri:POST_TO,
headers:{
'Content-type':'application/json'
},
json:{
'uri':uri
}
},
(err,response,body) => {
if(err || debug){ // debugかエラーありで出力
console.log("= err =========================================");
console.dir(err);
console.log("= response ====================================");
console.dir(response);
console.log("= body ========================================");
console.dir(body);
console.log("===============================================")
}
}
);
};