-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (53 loc) · 1.66 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
const telegram = require('./telegram.js');
const sendgrid = require('./sendgrid.js');
const keypress = require('keypress');
const fs = require('fs');
require('dotenv').config();
let chatId;
keypress(process.stdin);
process.stdin.on('keypress', (ch, key) => {
console.log('got keypress', key);
if(key && key.ctrl && key.name === 'c') {
process.stdin.pause();
console.log('paused');
} else if(key && key.name === 'v') {
console.log('geldi');
process.stdin.pause();
console.log('paused');
} else if(key && key.name === 'return' && chatId !== undefined) {
telegram.sendMessage('Emergency situation, help!', chatId, (errorMessage, results) => {
if (errorMessage) {
console.log(errorMessage);
} else {
console.log(JSON.stringify(results, undefined, 2));
}
});
console.log('SendMessage called.');
sendgrid.sendMail();
console.log('SendMail called.');
}
});
process.stdin.setRawMode(true);
fs.access('chatId.txt', (err) => {
if(err) {
let getUpdateLoop = setInterval(() => {
telegram.getUpdates((errorMessage, results) => {
if (errorMessage) {
console.log(errorMessage);
} else {
chatId = results;
clearInterval(getUpdateLoop);
fs.writeFile('chatId.txt', chatId, (err) => {
if (err) throw err;
console.log('ChatId saved!', chatId);
});
}
});
}, 5000);
} else {
let chatIdBuffer = fs.readFileSync('chatId.txt');
chatId = chatIdBuffer.toString();
console.log('ChatId already exists', chatId);
}
});
console.log('App started...');