-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
127 lines (116 loc) · 2.8 KB
/
app.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var express = require('express');
var https = require('https');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var app = express();
app.use(bodyParser.json());
var lastPing = 0;
var status = null;
function store(){
var url = 'mongodb://mongodb:27017/temgentenopet';
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
var col = db.collection('statuschanges');
col.insertOne({
status: status,
ts: new Date()
}, function(err, r){
db.close();
});
});
}
app.get('/qwertyuiop/sim', function (req, res) {
if(status != "sim"){
status = "sim"
store();
}
res.send("ok");
lastPing = new Date();
console.log(status);
});
app.get('/qwertyuiop/nao', function (req, res) {
if(status != "nao"){
status = "nao";
store();
}
res.send("ok");
lastPing = new Date();
console.log(status);
});
app.get('/qwertyuiop/webhook', function (req, res) {
res.send("Tem gente no PET: "+ status || "Nao sei");
});
var subscribers = [];
function sendMsg(msg){
var options = {
hostname: "api.telegram.org",
path: '/'+process.env.TELEGRAM_TOKEN+'/sendMessage',
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": msg.length
}
}
var post_req = https.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
});
post_req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
post_req.write(msg);
post_req.end();
}
app.post('/qwertyuiop/webhook', function (req, res) {
console.log(req.body);
try {
var responseText = '';
if(req.body.message.text=="/subscribe"){
var idx = subscribers.indexOf(req.body.message.chat.id)
if(idx == -1){
subscribers.push(req.body.message.chat.id);
}
responseText = "Inscrito!";
} else if(req.body.message.text=="/unsubscribe"){
var idx = subscribers.indexOf(req.body.message.chat.id)
if(idx > -1){
subscribers.splice(idx, 1);
}
responseText = "Tchau!";
} else {
responseText = "Tem gente no PET: "+ (status || "Nao sei");
console.log(subscribers);
}
var msg = JSON.stringify({
"chat_id": req.body.message.chat.id,
"text": responseText,
});
res.json("ok");
sendMsg(msg);
} catch(e) {
console.log(e);
res.json("ok");
}
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
var delay = 3000;
setInterval(function(){
if(lastPing == 0 || (new Date() - lastPing) < 30000){
delay = 5000;
return;
}
console.log("Cade??");
console.log(lastPing);
console.log(new Date());
subscribers.forEach(function(chatid){
var msg = JSON.stringify({
"chat_id": chatid,
"text": "Cade meu arduino????",
});
sendMsg(msg);
});
delay *= 5;
}, 10000);