-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
81 lines (68 loc) · 2.52 KB
/
server.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
const express = require('express')
const app = express()
const axios = require('axios')
const fs = require('fs')
const chat_id = process.env.CHAT_ID
const botToken = process.env.BOT_TOKEN
const port = process.env.PORT ? process.env.PORT : 3000
const telegram = `https://api.telegram.org/bot${botToken}`
const sendMapEndpoint = `${telegram}/sendLocation?live_period=43200&chat_id=${chat_id}`
const updateMapEndpoint = `${telegram}/editMessageLiveLocation?chat_id=${chat_id}`
const sendMessageEndpoint = `${telegram}/sendMessage?chat_id=${chat_id}`
const trackingTime = 3600000; // 1 hour
let isTracking = false;
let trackingTimeout = null; // To stop the tracking
let message_id = -1
let location = {
lat: null,
lng: null
}
// Send location to this endpoint
app.get('/l/:lng/:lat', async (req, res) => {
// If location received is the same, we'll just ignore,
if(location.lat == req.params.lat && location.lng == req.params.lng) {
res.send("Same")
return
}
// If there is a change we update the location
location = {
lat: req.params.lat,
lng: req.params.lng
}
// Check if isTracking is set, we will send an updated map
if(isTracking) {
try {
await axios(`${updateMapEndpoint}&longitude=${location.lng}&latitude=${location.lat}&message_id=${message_id}`)
} catch(err) {
console.log(err)
}
}
fs.appendFileSync(`${__dirname}/log.txt`, `${new Date().getTime()},${req.params.lng},${req.params.lat}`)
console.log(`${new Date().getTime()}: ${req.params.lng},${req.params.lat}`)
res.send('Received')
})
// When the tracker comes online send initial position
// Update message_id
app.get('/start', async (req, res) => {
await axios(`${sendMessageEndpoint}&text=Tracker%20Online`)
res.send("Online")
})
app.get('/stop', (req, res) => {
clearTimeout(trackingTimeout)
res.send("Stopped")
})
// Tracking will auto stop after an hour or explicitly through the /stop endpoint
// Track will just keep updating the map if the location has been updated
app.get('/track', async (req, res) => {
isTracking = true;
let response = (await axios(`${sendMapEndpoint}&longitude=${location.lng}&latitude=${location.lat}`)).data.result
message_id = response.message_id
trackingTimeout = setTimeout(() => {
isTracking = false;
console.log("Tracking stopped")
}, trackingTime)
res.send("Tracking")
})
// Create log file upon startup
fs.closeSync(fs.openSync(`${__dirname}/log.txt`, 'a'))
app.listen(port, () => console.log(`Tracker Server listening on port ${port}!`))