-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
134 lines (117 loc) · 3.43 KB
/
bot.js
File metadata and controls
134 lines (117 loc) · 3.43 KB
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
128
129
130
131
132
133
134
const ah = require("./ah-parse")
const fs = require("fs")
const Telegraf = require("telegraf")
const bot = new Telegraf(process.env.BOT_TOKEN)
const pollIntervalMin = 240 // in seconds
const pollIntervalMax = 360 // in seconds
const stateFile = "state.json"
let trackers = {}
function loadState ()
{
fs.readFile(stateFile, (err, data) => {
if (err) return
try
{
const json = JSON.parse(data)
if (json && json.trackers)
trackers = json.trackers
console.log(new Date, "Loaded state", json)
}
catch (e) {
console.error(new Date, "Failed loading state", e)
}
});
}
function saveState ()
{
const json = JSON.stringify({ trackers: trackers }, null, 2)
fs.writeFile(stateFile, json, (err) => err && console.log(new Date, "saveState failed", json, err))
}
bot.use((ctx, next) => {
console.log(new Date, "Message from user", ctx.chat, "text:", ctx.message.text)
return next(ctx)
})
bot.start((ctx) => ctx.reply("Hallo! Welke postcode wil je tracken?"))
bot.command("stop", (ctx) => removeTracking(ctx.chat.id))
bot.on("text", function (ctx)
{
let postcode = normalizePostcode(ctx.message.text)
if (postcode)
{
ctx.reply("Je krijgt vanaf nu notificaties als er slots beschikbaar zijn voor " + postcode)
ctx.reply("Stuur /stop om notificaties te stoppen")
trackPostcode(postcode, ctx.chat.id)
}
else
{
ctx.reply("Dit is geen postcode?")
}
})
function normalizePostcode(input)
{
let postcode = input.match(/(\d{4})\s?([A-Za-z]{2})/)
if (postcode && postcode.length > 2)
return postcode[1] + postcode[2].toUpperCase()
else
return undefined
}
function newTracker ()
{
return { chats: [], prevChecked: null, prevSlots: [] }
}
function trackPostcode(postcode, chatId)
{
if (!trackers[postcode])
trackers[postcode] = newTracker()
let tracker = trackers[postcode]
tracker.chats.push(chatId)
saveState()
}
function removeTracking(chatId)
{
Object.keys(trackers).forEach(key => removeItem(trackers[key].chats, chatId))
saveState()
notify(chatId, "Notificaties gestopt.")
}
function removeItem(array, item)
{
const index = array.indexOf(item)
index != -1 && array.splice(index, 1)
}
async function pollPostcode (postcode)
{
const tracker = trackers[postcode]
const chats = tracker.chats
if (chats.length)
{
try
{
const results = await ah.getAvailability(postcode)
console.log(new Date, "Results received for ", postcode, results)
tracker.prevChecked = new Date();
if (results.length && (results+"")!==(tracker.prevSlots+""))
chats.forEach(c => notify(c, ah.presentAvailability(results, tracker.prevSlots.length)))
tracker.prevSlots = results
saveState()
}
catch (error)
{
console.log(new Date, "pollPostcode error", error)
}
}
}
function notify (user, message)
{
bot.telegram.sendMessage(user, message)
console.log(new Date, "Message to", user, message)
}
function poll ()
{
Object.keys(trackers).forEach(key => pollPostcode(key))
let nextPoll = Math.round(Math.random() * (pollIntervalMax - pollIntervalMin)) + pollIntervalMin
console.log(new Date, "Polling again in", nextPoll)
setTimeout(poll, nextPoll * 1e3)
}
loadState()
bot.launch()
poll()