-
Notifications
You must be signed in to change notification settings - Fork 1
/
receive.js
29 lines (27 loc) · 957 Bytes
/
receive.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
module.exports = function(RED) {
function Receive(config) {
RED.nodes.createNode(this,config);
const axios = require("axios");
const node = this;
const storage = node.context();
const poll = async function() {
try {
let url = new URL( config.server );
const responds = await axios.get(url.origin+"/message?token="+config.token+"&limit=1");
let lastMsgId = storage.get("lastId");
const msgs = responds.data.messages;
for(let i=0;i<msgs.length;i++) {
if(msgs[i].id !== lastMsgId) {
storage.set("lastId",msgs[i].id);
node.send({payload:msgs[i]});
}
}
} catch(e) {console.log(e);}
}
node.on('input', async function(msg) {
poll();
});
setInterval(poll,60000);
}
RED.nodes.registerType("Gotify-Receive",Receive);
}