-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
98 lines (86 loc) · 2.43 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
const { Telegraf } = require("telegraf");
const jwt = require("jsonwebtoken");
const crypto = require("crypto");
const TOKEN = process.env.TELEGRAM_BOT_TOKEN;
const LOGIN_URL = process.env.LOGIN_URL;
const bot = new Telegraf(TOKEN);
// Start command handling
bot.start((ctx) => {
const userData = {
authDate: Math.floor(new Date().getTime()),
firstName: ctx.update.message.from.first_name,
lastName: "",
username: ctx.update.message.from.username,
id: ctx.update.message.from.id,
photoURL: "",
};
const hash = generateTelegramHash(userData);
// Generate an auth token (JWT)
const telegramAuthToken = jwt.sign(
{
...userData,
hash,
},
TOKEN,
{ algorithm: "HS256" }
);
console.log("[Debug] Called by ", ctx.update.message.from.username);
const encodedTelegramAuthToken = encodeURIComponent(telegramAuthToken);
// Define the inline keyboard with a web app button
const keyboard = {
reply_markup: {
inline_keyboard: [
[
{
text: "Open Mini Web App 🚀",
web_app: {
url: `${LOGIN_URL}/?telegramAuthToken=${encodedTelegramAuthToken}`,
},
},
],
],
},
};
// Send a message with the inline keyboard
ctx.reply("Welcome to XYZ Mini Web App", keyboard);
});
// Start the bot
bot.launch();
/**
* generateTelegramHash function
* @param data the user data
* @returns hmac string
*/
const generateTelegramHash = (data) => {
// Prepare data object with the required fields
const useData = {
auth_date: String(data.authDate),
first_name: data.firstName,
id: String(data.id),
last_name: data.lastName,
photo_url: data.photoURL,
username: data.username,
};
// Filter out any undefined or empty values
const filteredUseData = Object.entries(useData).reduce(
(acc, [key, value]) => {
if (value) {
acc[key] = value;
}
return acc;
},
{}
);
// Create the data check string by sorting the keys
const dataCheckArr = Object.entries(filteredUseData)
.map(([key, value]) => `${key}=${String(value)}`)
.sort((a, b) => a.localeCompare(b))
.join("\n");
const TELEGRAM_SECRET = crypto.createHash("sha256").update(TOKEN).digest();
// Generate HMAC-SHA256 hash from the data check string
const hmac = crypto
.createHmac("sha256", TELEGRAM_SECRET)
.update(dataCheckArr)
.digest("hex");
return hmac; // hash value
};