-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
126 lines (121 loc) · 4.42 KB
/
start.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
const https = require('https'),
fs = require('fs'),
HttpBin = require('./httpbin'),
// limiting the responses can be useful to prevent the computer
// to be overwhelmed if reach the amount of maximum responses
limitResponse = true,
maxResponses = 10000
function getUpdates() {
let body = {
'timeout': 120
},
// fresh refer to whether we have here a new bot or not
fresh = info.update_id == undefined || info.offset == undefined
if (!fresh)
body.offset = info.update_id + info.offset
let stringified = JSON.stringify(body),
options = {
'hostname': 'api.telegram.org',
'port': 443,
'path': `/bot${info.token}/getUpdates`,
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(stringified)
}
},
req = https.request(options)
req.on('response', resp => {
resp.on('data', data => {
data = JSON.parse(data.toString())
if (data.ok) {
if (data.result.length > 0) {
if (fresh) {
info.update_id = data.result[0].update_id
info.offset = 0
}
// set received messages as "read"
// since the offset function is to tell telegram bot api
// where to start of next update
info.offset += data.result.length
for (update of data.result) {
// log interesting things about the messages
console.log('=====')
console.log('update:', update.update_id)
console.log('username:', update.message.chat.username)
console.log('chat:', update.message.chat.id)
console.log('text:', update.message.text)
// if message was request for ip and from you
// send ip address
if (update.message.text == info.command)
if (update.message.chat.username == info.me)
sendIp(update.message.chat.id)
else
sendMessage(update.message.chat.id,
`sorry, u're not @${info.me}`)
else
sendMessage(update.message.chat.id,
'sorry, not a ip request')
}
flushJSON('personal_info.json', info)
}
}
// a trick to escape of recursion
setImmediate(getUpdates)
})
})
req.on('error', err => console.log(err))
req.end(stringified)
}
function sendMessage(chatId, text) {
let body = {
'chat_id': chatId,
'text': text
},
stringified = JSON.stringify(body),
options = {
'hostname': 'api.telegram.org',
'port': 443,
'path': `/bot${info.token}/sendMessage`,
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(stringified)
}
},
req = https.request(options)
req.on('response', resp => {
resp.on('data', data => {
// log to console if message was successful
console.log('resp:', JSON.parse(data.toString()).ok)
if (limitResponse) {
responseCount++
if (responseCount >= maxResponses) {
console.log('=====')
console.log('# maximum responses reached')
process.exit(15)
}
}
})
})
req.on('error', err => console.log(err))
req.end(stringified)
}
function sendIp(chatId) {
let hb = new HttpBin()
hb.on('ip', ip => {
sendMessage(chatId, ip)
})
hb.getIp()
}
function flushJSON(file, obj) {
fs.writeFileSync(file, JSON.stringify(obj))
}
if (fs.existsSync('personal_info.json')) {
info = JSON.parse(fs.readFileSync('personal_info.json').toString())
if (limitResponse)
responseCount = 0
getUpdates()
} else {
console.log('You need to setup your bot, run "node setup"')
}