-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
424 lines (354 loc) · 11.3 KB
/
bot.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
const {Telegraf} = require('telegraf')
const {Flatastic} = require('./flatastic.js')
const LocalSession = require('telegraf-session-local')
var add = require('date-fns/add')
var format = require('date-fns/format')
let eoLocale = require('date-fns/locale/de')
const JSONdb = require('simple-json-db');
const db = new JSONdb('database.json');
const fs = require('fs')
let Imgflip = require('imgflip').default
require('dotenv').config()
var pjson = require('./package.json');
const imgflip = new Imgflip({
username: process.env.IMGFLIP_USER,
password: process.env.IMGFLIP_PASSWORD
})
let flatastic = new Flatastic(process.env.FLATASTIC_TOKEN)
const bot = new Telegraf(process.env.BOT_TOKEN)
//bot.use((new LocalSession({database: 'example_db.json'})).middleware())
bot.start((ctx) => ctx.reply('Hey, ich bin der WG Bot!'))
bot.help((ctx) => ctx.reply('Frag mich welche Tasks offen sind'))
let users
if (db.has("users")) {
users = db.get('users')
} else {
users = {};
db.set('users', users);
}
flatastic.getInformation(function (data) {
for (let i = 0; i < data.flatmates.length; i++) {
let user = data.flatmates[i];
users[user.id] = user;
}
db.set('users', users);
})
var cron = require('node-cron');
cron.schedule('0 17 * * *', () => {
setTimeout(function () {
listTasks()
}, 60 * 1000 * Math.random())
})
let taskTemplate = {
1650985: {
templatesDE: ["chasch du no d chuchi ufrume?"],
templatesEN: ["can you please clean the kitchen? It's overdue."]
},
1446700: {
templatesDE: ["chasch du no go ichoufe?"],
templatesEN: ["can you please go buy the groceries? It's overdue."]
},
1446702: {
templatesDE: ["chöntisch du bitte no d läbensmittu ufrume?"],
templatesEN: ["can you please remove the overdue food?"]
},
1446733: {
templatesDE: ["du sötisch no dr balkon ufrume."],
templatesEN: ["you should clean the balcony."]
},
1926990: {
templatesDE: ["hesch e idee für e kreative task? Schüsch chasch mau ufem board go luegä."],
templatesEN: ["do you have an idea for a creative task? Else you can check the board."]
},
2297251: {
templatesDE: ["cha öpper pflanze ufem balkon giessä?"],
templatesEN: ["can you please water the plant on the balcony?"]
},
2512334: {
templatesDE: ["chasch du no dr ofe putze?"],
templatesEN: ["can you please clean the oven?"]
},
2216033: {
templatesDE: ["chöntisch du no d tüecher wäsche?"],
templatesEN: ["can you please wash the cloths?"]
},
1446630: {
templatesDE: ["chöntisch du no ds bad putze?"],
templatesEN: ["can you please clean the bathroom?"]
},
1242269: {
templatesDE: ["chasch du d bluemä giessä?"],
templatesEN: ["can you please water the flowers?"]
},
1446646: {
templatesDE: ["chasch du no stoubsuge?"],
templatesEN: ["can you please vacuum the floor?"]
},
1446703: {
templatesDE: ["chasch du no ds wohnzimmer ufrume?"],
templatesEN: ["can you please clean the living room?"]
},
1242308: {
templatesDE: ["chasch du no d zahlige mache?"],
templatesEN: ["can you please do the payments?"]
},
1241468: {
templatesDE: ["chasch du no d petflasche entsorge?"],
templatesEN: ["can you please dispose the pet bottles?"]
},
1446712: {
templatesDE: ["chasch du no mitem roboter füecht ufnäh?"],
templatesEN: ["can you please vacuum the floor with the robot?"]
},
2012692: {
templatesDE: ["chasch du no e wg abe organisiere?"],
templatesEN: ["can you please organize a WG event?"]
}
}
function getText(user, days, templates) {
let template = getRandom(templates.templatesDE);
if (user.language === '1') {
template = getRandom(templates.templatesEN)
}
return template.replace("{name}", user.firstName).replace("{days}", days);
}
function getRandom(items) {
return items[Math.floor(Math.random() * items.length)];
}
function listTasks() {
flatastic.getTaskList(function (data) {
var tasks = "";
data = data.sort(function (x, y) {
let n = x.currentUser - y.currentUser;
if (n !== 0) {
return n;
}
return x.timeLeftNext - y.timeLeftNext;
});
let lastUser = 0;
for (const dataKey in data) {
let task = data[dataKey];
var daysUntilTask = (task.timeLeftNext / 60 / 60 / 24);
var passed = 'heute';
if (daysUntilTask < 0) {
passed = "vor " + Math.ceil(Math.abs(daysUntilTask)) + " Tag/en"
}
if (daysUntilTask > 1) {
passed = "in " + Math.ceil(Math.abs(daysUntilTask)) + " Tag/en"
continue;
}
if (task.rotationTime === -1) {
continue;
}
if (task.currentUser !== lastUser) {
if (task.currentUser === 1181336) {
tasks += "\nHallo Zäme, ";
} else {
tasks += "\nHey " + users[task.currentUser].firstName + ", ";
}
}
if (taskTemplate[task.id]) {
if (lastUser === task.currentUser) {
tasks += " U "
}
tasks += getText(users[task.currentUser], Math.abs(Math.round(daysUntilTask)), taskTemplate[task.id])
lastUser = task.currentUser;
continue;
}
tasks += task.title + " "
tasks += passed + " fällig\n";
lastUser = task.currentUser;
}
let taskList = tasks.trim().split("\n");
console.log(taskList)
taskList.forEach(function (task) {
setTimeout(function () {
bot.telegram.sendMessage(process.env.TELEGRAM_GROUP, task, {
parse_mode: 'HTML'
})
}, 3000 * Math.random())
});
})
}
cron.schedule('15 15 * * *', () => {
showShoppingList()
});
bot.hears(/version/i, (ctx) => {
console.dir(ctx.update)
ctx.replyWithHTML("Ich bin auf Version " + pjson.version);
})
let memes = [
[`61539`, {
captions: [
`We ir wg`,
`D chuchi nid ufgrumt isch`
],
path: `./memes/chuchi.png`
}],
[`285870`, {
captions: [
`We ds badezimmer putzt isch`,
`We ds badezimmer nid putzt isch`
],
path: `./memes/badezimmer.png`
}],
[`131087935`, {
captions: [
`Ig`,
`E ufgruhmti wg`,
`Mini ämtli`,
`Ig`,
`E ufgruhmti wg`,
],
path: `./memes/ufgruhmti.png`
}],
[`222403160`, {
captions: [
``,
`you to do your tasks`,
],
path: `./memes/bernie.png`
}],
[`93895088`, {
captions: [
`I mache mini ämtli nid`,
`I mache mini ämtli`,
`I mache mini ämtli wül i e suberi wg wott`,
`I mache mini ämtli wül i mini mitbewohner gärn ha`,
],
path: `./memes/mitbewohner.png`
}],
[`195515965`, {
captions: [
`I zieh ine ufgrumti wg`,
`I mache mini ämtli nid`,
'D wg wird unordentlich',
`D wg isch jitz unordentlich`,
],
path: `./memes/unordentlich.png`
}],
[`27813981`, {
captions: [
`Wed iz flatastic luegsch`,
`Us ämtli git wo überfällig si`,
],
path: `./memes/harold.png`
}],
[`129242436`, {
captions: [
`Du sötisch dini ämtli vor em ablaufdatum mache!`,
],
path: `./memes/change.png`
}],
[`89370399`, {
captions: [
`Wed dis ämtli machsch, de mache di anderä ihres o.`,
],
path: `./memes/anderä.png`
}],
[`80707627`, {
captions: [
`We di anderä ihres ämtli nid mache.`,
],
path: `./memes/anderä3.png`
}],
[`1202623`, {
captions: [
`Keep calm and do your tasks`,
],
path: `./memes/anderä4.png`
}],
]
bot.hears(/meme/i, async (ctx) => {
let meme = memes[Math.floor(Math.random() * memes.length)];
await imgflip.meme.call(imgflip, ...meme)
ctx.replyWithPhoto({source: fs.readFileSync(meme[1].path)}, )
})
bot.hears(/showshopping/i, (ctx) => {
showShoppingList()
});
function showShoppingList() {
flatastic.getShoppingList(function (data) {
var output = "";
data = data.filter(function (a) {
return a.bought === 0;
})
if (data.length === 0) {
} else {
output = "Momentan ist folgendes auf der Einkaufsliste:\n";
let items = [];
for (const dataKey in data) {
let item = data[dataKey];
if (item.bought) {
continue;
}
items.push(item.itemName)
}
output += items.join(", ")
let oldList = db.get("shoppingList");
console.log(oldList)
if (oldList === items.join(", ")) {
return;
}
db.set("shoppingList", items.join(", "))
bot.telegram.sendMessage(process.env.TELEGRAM_GROUP, output).then(function (message) {
bot.telegram.pinChatMessage(process.env.TELEGRAM_GROUP, message.message_id)
})
}
})
}
bot.hears(/dailytask/i, (ctx) => {
listTasks()
})
bot.hears(/wgabe/i, (ctx) => {
var nextDates = [];
let test = new Date();
for (let i = 0; i < 10; i++) {
test = add(test, {
days: 1,
})
nextDates.push(format(test, "EEEE, dd.MM.yyyy", {
locale: eoLocale
}))
}
bot.telegram.sendPoll(process.env.TELEGRAM_GROUP, 'Nächster WG Abend?',
nextDates,
{is_anonymous: false, allows_multiple_answers: true})
})
bot.hears(/showtasks/i, (ctx) => {
flatastic.getTaskList(function (data) {
var tasks = "";
data = data.sort(function (x, y) {
let n = x.currentUser - y.currentUser;
if (n !== 0) {
return n;
}
return x.timeLeftNext - y.timeLeftNext;
});
let lastUser = 0;
for (const dataKey in data) {
let task = data[dataKey];
if (task.currentUser !== lastUser) {
tasks += "\n<b>" + users[task.currentUser].firstName + "</b>\n";
}
tasks += task.title + " "
var daysUntilTask = (task.timeLeftNext / 60 / 60 / 24);
var passed = 'heute';
if (daysUntilTask < 0) {
passed = "vor " + Math.ceil(Math.abs(daysUntilTask)) + " Tag/en"
}
if (daysUntilTask > 1) {
passed = "in " + Math.ceil(Math.abs(daysUntilTask)) + " Tag/en"
}
passed = passed + " fällig\n";
if (task.rotationTime === -1) {
passed = "\n"
}
tasks += passed
lastUser = task.currentUser;
}
console.log(tasks);
ctx.replyWithHTML(tasks);
})
});
bot.launch()