-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtelegram.ts
435 lines (401 loc) · 12.1 KB
/
telegram.ts
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import * as https from "https";
import * as fs from "fs";
import * as path from "path";
import * as Telegram from "telegraf/telegram";
import * as Telegraf from "telegraf";
import * as Extra from "telegraf/extra";
import * as Markup from "telegraf/markup";
import * as jwt from "jsonwebtoken";
import * as ShortUniqueId from "short-unique-id";
import * as R from "ramda";
import * as puppeteer from "puppeteer";
import * as mqtt from "mqtt";
import * as webPush from "web-push";
import { uploadFile } from "s3-bucket";
import * as getFileFromUrl from "@appgeist/get-file-from-url";
import { rand } from "./rand";
import * as db from "./db";
import logger from "./logger";
import { GAME_START_COUNTDOWN } from "./constants";
import { now } from "./timestamp";
import { addGameEvent } from "./table/games";
import { Command } from "./types";
if (!process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
console.log(
"You must set the VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY " +
"environment variables. You can use the following ones:"
);
console.log(webPush.generateVAPIDKeys());
process.exit(1);
}
webPush.setVapidDetails(
process.env.VAPID_URL!,
process.env.VAPID_PUBLIC_KEY!,
process.env.VAPID_PRIVATE_KEY!
);
const telegram = new Telegram(process.env.BOT_TOKEN);
const uid = new ShortUniqueId();
const officialGroups = process.env.BOT_OFFICIAL_GROUPS
? process.env.BOT_OFFICIAL_GROUPS.split(",")
: [];
console.log("connecting to mqtt: " + process.env.MQTT_URL);
var client = mqtt.connect(process.env.MQTT_URL, {
username: process.env.MQTT_USERNAME,
password: process.env.MQTT_PASSWORD,
});
client.subscribe("events");
client.subscribe("game_events");
var lastJoinPlayer: string | null = null;
client.on("message", async (topic, message) => {
if (topic === "events") {
const event = JSON.parse(message.toString());
if (event.type === "join") {
if (!event.user || event.bot) {
return;
}
subscribed.forEach(id =>
telegram
.sendMessage(
id,
`${event.user.name} joined https://qdice.wtf/${event.table}`
)
.catch(e => console.error(e))
);
if (lastJoinPlayer === event.user.id) {
return;
}
lastJoinPlayer = event.user.id;
// push notifications
const subscriptions = await db.getPushSubscriptions("player-join");
const text = `${event.user.name} joined table "${event.table}"`;
subscriptions.forEach(async row => {
if (row.subscription && event.user.id !== row.id) {
console.log("PN", row.id, event.user.id);
try {
const request = await webPush.sendNotification(
row.subscription,
JSON.stringify({
type: event.type,
timestamp: now(),
table: event.table,
text,
link: `https://qdice.wtf/${event.table}`,
}),
{
TTL: GAME_START_COUNTDOWN,
}
);
} catch (e) {
console.error("push subscription expired, removing", row.id);
try {
const result = await db.removePushSubscription(
row.id,
row.subscription
);
} catch (e) {
console.error(
"could not remove push subscription",
JSON.stringify({ id: row.id, subscription: row.subscription }),
e
);
}
}
}
});
} else if (event.type === "countdown") {
console.log("offical", officialGroups);
const { table, players } = event;
// push notifications
const subscriptions = await db.getPushSubscriptions("game-start");
const text = `A game countdown has started in "${table}"`;
subscriptions.forEach(async row => {
if (
row.subscription &&
!players.some(player => player.id === row.id) // don't notify players in game
) {
try {
const request = await webPush.sendNotification(
row.subscription,
JSON.stringify({
type: event.type,
timestamp: now(),
table: event.table,
text,
link: `https://qdice.wtf/${event.table}`,
}),
{
TTL: GAME_START_COUNTDOWN,
}
);
console.log("PN", row.name, request);
} catch (e) {
// console.error(e);
// TODO remove subscription if unsubscribed or expired
}
}
});
if (false && officialGroups.length) {
// const realPlayerCount = players.filter(p => !p.bot).length;
officialGroups.forEach(id => {
console.log("aviso", id);
telegram.sendMessage(
id,
`A game countdown has started in table ${table}, with ${players
.map(p => p.name)
.join(", ")}: https://qdice.wtf/${table}`
);
});
}
}
} else if (topic === "game_events") {
const {
tableName,
gameId,
command,
}: {
tableName: string;
gameId: number;
command: Command;
} = JSON.parse(message.toString());
const eventId = await addGameEvent(tableName, gameId, command);
switch (command.type) {
case "Roll":
const success = R.sum(command.fromRoll) > R.sum(command.toRoll);
// console.log(
// "attack",
// command.attacker.name,
// success ? "SUCCESS" : "FAILED",
// command.defender?.name ?? "Neutral"
// );
break;
}
}
});
db.connect().then(db => {
console.log("connected to postgres");
});
console.log("starting tg bot: ", process.env.BOT_TOKEN);
const bot = new Telegraf(process.env.BOT_TOKEN, { username: "quedice_bot" });
bot.catch(err => {
console.log("Ooops", err);
});
bot.telegram.getMe().then(botInfo => {
bot.options.username = botInfo.username;
});
bot.start(ctx => {
console.log("started:", ctx.from);
ctx.replyWithGame(gameShortName, markup);
});
bot.hears("que pasa", ctx => {
ctx.reply("¡Qué Dice!");
});
const dice = ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"];
bot.hears(/tira.*dado/i, ctx =>
ctx.reply(`Tirada de dado: ${dice[rand(1, 6) - 1]}`)
);
bot.command("dado", ctx => {
const {
text,
from: { first_name: name },
} = ctx.message;
const roll = rand(1, 6);
ctx.reply(`${name} ha tirado un: ${roll} ${dice[roll - 1]}`);
});
bot.command("dados", ctx => {
const {
text,
from: { first_name: name },
} = ctx.message;
let amount = Math.min(30, parseInt(text.split("/dados ").pop(), 10));
if (isNaN(amount)) {
amount = 2;
}
const rolls = R.range(0, amount)
.map(_ => rand(1, 6))
.map(r => dice[r - 1])
.join(" ");
ctx.reply(`${name} ha tirado: ${rolls}`);
});
bot.command("rank", ctx => {
console.log(ctx.message.from);
const {
text,
from: { first_name: name },
} = ctx.message;
const roll = rand(1, 6);
ctx.reply(`${name} ha tirado un: ${roll} ${dice[roll - 1]}`);
});
bot.on("sticker", ctx => ctx.reply("👍"));
bot.on("inline_query", ctx => {
const result = [
{
//type: 'photo',
//id: uid.randomUUID(16),
//photo_url: 'https://quedice.host/assets/ThreeDice.jpg',
//thumb_url: 'https://quedice.host/assets/ThreeDice_thumb.jpg',
//title: `Tirada de dado: ${dice[rand(1, 6) - 1]}`,
//caption: `Tirada de dado: ${dice[rand(1, 6) - 1]}`,
type: "game",
id: uid.randomUUID(16),
game_short_name: "quedice",
reply_markup: markup,
},
];
console.log(result);
// Using shortcut
ctx.answerInlineQuery(result);
});
const gameShortName = process.env.BOT_GAME;
//const gameUrl = 'http://lvh.me:5000';
const gameUrl =
gameShortName === "QueDiceTest" ? "http://lvh.me:5000" : "https://qdice.wtf";
const markup = Extra.markup(
Markup.inlineKeyboard([
Markup.gameButton("🎲 Play in telegram!"),
Markup.urlButton("Play in browser", gameUrl),
])
);
bot.command("game", ctx => {
console.log("/game", gameShortName);
ctx.replyWithGame(gameShortName, markup);
});
bot.gameQuery(ctx => {
console.log("----------gameQuery", ctx.update.callback_query.message);
db.getUserFromAuthorization(db.NETWORK_TELEGRAM, ctx.from.id)
.then(user => {
console.log("got user", user);
if (user) {
return user;
}
return telegram
.getUserProfilePhotos(ctx.from.id, 0, 1)
.then(({ photos: [[photo]] }) => {
const { file_id } = photo;
return telegram.getFile(file_id);
})
.then(({ file_path }) => {
return `https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${file_path}`;
})
.catch(e => {
console.error("could not get photo", e);
return "https://telegram.org/img/t_logo.png";
})
.then(downloadAvatar(uid.randomUUID(16)))
.then(filename => {
console.log("create tg user", ctx.from);
return db.createUser(
db.NETWORK_TELEGRAM,
ctx.from.id,
ctx.from.first_name || ctx.from.username,
null,
`${process.env.PICTURE_URL_PREFIX}/${filename}`,
{
user_id: ctx.from.id,
chat_id: ctx.chat.id,
chat_type: ctx.chat.type,
message_id: ctx.update.callback_query.message.message_id,
}
);
});
})
.then(profile => {
console.log("got profile", profile);
const token = jwt.sign(JSON.stringify(profile), process.env.JWT_SECRET!);
console.log("answer", gameUrl + "/token/" + token);
return ctx.answerGameQuery(gameUrl + "/token/" + token);
})
.catch(e => {
console.error("gameQuery error: " + e, e);
telegram.sendMessage(
ctx.chat.id,
`Could not register user ${ctx.from.first_name || ctx.from.username}`
);
});
});
bot.startPolling();
const subscribed = [] as number[];
bot.command("notifyme", ctx => {
const index = subscribed.indexOf(ctx.chat.id);
if (index === -1) {
subscribed.push(ctx.chat.id);
ctx.reply("I will notify you when anyone joins a table.");
} else {
subscribed.slice(index, 1);
ctx.reply("I will not notify you anymore.");
}
});
module.exports.notify = string => {
try {
} catch (e) {
console.error(e);
}
};
const setScore = ({ user_id, chat_id, chat_type, message_id }, score) => {
telegram
.getGameHighScores(user_id, undefined, chat_id, message_id)
.then(scores => {
const playerScore = scores
.filter(score => score.user.id === user_id)
.shift();
return playerScore ? playerScore.score : 0;
})
.catch(e => 0)
.then(currentScore => {
console.log(
"setScore",
JSON.stringify([
user_id,
chat_id,
chat_type,
message_id,
currentScore + score,
])
);
return telegram.setGameScore(
user_id,
currentScore + score,
undefined,
chat_id,
message_id,
true,
true
);
})
.catch(e => console.error("setGameScore failed:", e));
};
bot.command("score", ctx => {
console.log(ctx.message);
telegram
.getGameHighScores(
ctx.from.id,
undefined,
ctx.chat.id,
ctx.message.message_id
)
.then(scores => {
ctx.reply(JSON.stringify(scores, null, "\n"));
})
.catch(e => ctx.reply("Error: " + e));
});
const downloadAvatar = id => url => {
const filename = `user_${id}.jpg`;
const file = fs.createWriteStream(
path.join(process.env.AVATAR_PATH!, filename)
);
https.get(url, response => {
response.pipe(file);
});
return filename;
};
let browserSingleton;
const newPage = async () => {
if (!browserSingleton) {
browserSingleton = await puppeteer.launch();
}
return browserSingleton.newPage();
};
process.on("unhandledRejection", (reason, p) => {
console.error("Unhandled Rejection at: Promise", p, "reason:", reason);
// application specific logging, throwing an error, or other logic here
throw reason;
});