-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.ts
232 lines (216 loc) · 7.68 KB
/
bot.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
import {
Bot,
type Context,
InlineKeyboard,
webhookCallback,
} from "https://deno.land/x/[email protected]/mod.ts";
import { emoji } from "https://deno.land/x/[email protected]/mod.ts";
const isDebug = !!Deno.env.get("DEBUG");
const kv = await Deno.openKv();
const welcomeMessage =
"Hi! Glad to see that you want to join @grammyjs! Let me make sure that you are human.";
const empty = emoji("white_large_square");
function inputMessage(first = empty, second = empty, third = empty) {
return `
Which emojis do you see in the slot machine? Please enter them using the buttons below!
${emoji("red_question_mark")}: ${first}${second}${third}`;
}
function correctMessage(first: string, second: string, third: string) {
return `
Correct! Welcome to @grammyjs, human!
${emoji("check_mark_button")}: ${first}${second}${third}`;
}
function incorrectMessage(first: string, second: string, third: string) {
return `
Incorrect. Are you sure that you are human?
${emoji("cross_mark")}: ${first}${second}${third}`;
}
const helpTextPreRequest =
"This bot protects the chat @grammyjs. You did not request to join it, so this bot does nothing for you right now.";
const helpTextPostRequest = `
You have requested to join @grammyjs. We are happy to welcome you to the chat as soon as you have confirmed that you are human.
You can do this by sending me the three values that you see in the slot machine above. Simply tap three of the buttons above in the order that you see them.
(If you are completely stuck and you have no idea how to solve this captcha, feel free to open an issue on GitHub and tell us your Telegram username so we can add you to the chat. You can find the repository linked at the top of grammy.dev.)`;
const thirtyMinutesInMilliseconds = 30 * 60 * 1000;
const em = [
emoji("minus"),
emoji("grapes"),
emoji("lemon"),
emoji("keycap_digit_seven"),
];
function predictEmoji(value: number): [string, string, string] {
const n = value - 1;
return [
em[(n & 0b000011) >> 0],
em[(n & 0b001100) >> 2],
em[(n & 0b110000) >> 4],
];
}
const keyboard = InlineKeyboard.from([em.map((e) => InlineKeyboard.text(e))])
.toFlowed(2)
.row().text(emoji("back_arrow"), "back");
const bot = new Bot(Deno.env.get("BOT_TOKEN") ?? "");
const safe = bot.errorBoundary((err) => {
console.error(err);
});
const secretToken = bot.token.replaceAll(":", "_");
// disable the bot for all groups except @grammyjs
safe.on("my_chat_member")
.chatType(["group", "supergroup", "channel"])
.drop((ctx) => ctx.chat.username === "grammyjs")
.use((ctx) => ctx.leaveChat());
// DM a dice to users upon join request
safe.on("chat_join_request", async (ctx) => {
const dm = ctx.chatJoinRequest.user_chat_id;
await ctx.api.sendMessage(dm, welcomeMessage);
await sendCaptcha(ctx, dm, ctx.from.id);
});
async function sendCaptcha(ctx: Context, chatId: number, userId: number) {
const { dice } = await ctx.api.sendDice(chatId, emoji("slot_machine"));
const { message_id } = await ctx.api.sendMessage(chatId, inputMessage(), {
reply_markup: keyboard,
});
await kv.set([chatId, "solution"], dice.value, {
// set expiry for the rare case that the queue task is not delivered
expireIn: 2 * thirtyMinutesInMilliseconds,
});
await kv.enqueue({ chatId, userId, messageId: message_id }, {
delay: thirtyMinutesInMilliseconds,
});
}
kv.listenQueue(async ({ chatId, userId, messageId }) => {
const member = await bot.api.getChatMember("@grammyjs", userId);
// only run if the member has not joined yet
if (member.status !== "left") return;
// only run if the captcha has not been solved yet
const solution = await kv.get<number>([chatId, "solution"]);
if (solution.value === null) return;
const retry = new InlineKeyboard()
.url("Try again", "https://t.me/grammyjs");
await kv.delete([chatId, "solution"]);
await kv.delete([chatId, "input"]);
await bot.api.editMessageText(
chatId,
messageId,
"Your request has expired.",
{ reply_markup: retry },
).catch(console.error);
await bot.api.declineChatJoinRequest("@grammyjs", userId)
.catch(console.error);
});
// only respond in private chats
const dm = safe.chatType("private");
dm.on("callback_query:data").fork((ctx) => ctx.answerCallbackQuery());
dm.command("help", async (ctx) => {
const dm = ctx.chatId;
const solution = await kv.get<number>([dm, "solution"]);
await ctx.reply(
solution.value === null ? helpTextPreRequest : helpTextPostRequest,
);
});
dm.filter(() => isDebug).command(
"test",
(ctx) => sendCaptcha(ctx, ctx.chatId, ctx.from.id),
);
// disable bot for members and banned accounts
const captcha = dm.filter(async (ctx) => {
if (isDebug) return true;
const member = await ctx.api.getChatMember("@grammyjs", ctx.from.id);
switch (member.status) {
case "administrator":
case "creator":
await ctx.reply("You are admin in @grammyjs already!");
return false;
case "kicked":
await ctx.reply("You were banned from @grammyjs already!");
return false;
case "member":
case "restricted":
await ctx.reply("You are a member of @grammyjs already!");
return false;
}
return true;
});
// handle emoji input from inline keyboard
captcha.callbackQuery(em, async (ctx) => {
const dm = ctx.chatId;
const solution = await kv.get<number>([dm, "solution"]);
if (solution.value === null) {
await ctx.editMessageText(
"The captcha has expired. You need to request to join @grammyjs again.",
{ reply_markup: undefined },
);
return;
}
const res = await kv.get<string[]>([dm, "input"]);
const current = res.value ?? [];
current.push(ctx.callbackQuery.data);
if (current.length >= 3) {
const [i0, i1, i2] = current;
const [s0, s1, s2] = predictEmoji(solution.value);
if (i0 === s0 && i1 === s1 && i2 === s2) {
await ctx.editMessageText(correctMessage(i0, i1, i2), {
reply_markup: undefined,
});
if (!isDebug) {
await ctx.api.approveChatJoinRequest("@grammyjs", ctx.from.id);
}
await kv.delete([dm, "solution"]);
} else {
await ctx.editMessageText(incorrectMessage(i0, i1, i2), {
reply_markup: new InlineKeyboard().text("Try again", "again"),
});
}
await kv.delete([dm, "input"]);
} else {
await ctx.editMessageText(inputMessage(...current), {
reply_markup: keyboard,
});
await kv.set([dm, "input"], current, {
expireIn: thirtyMinutesInMilliseconds,
});
}
});
// handle back button
captcha.callbackQuery("back", async (ctx) => {
const dm = ctx.chatId;
const solution = await kv.get<number>([dm, "solution"]);
if (solution.value === null) {
await ctx.editMessageText(
"The captcha has expired. You need to request to join @grammyjs again.",
{ reply_markup: undefined },
);
return;
}
const res = await kv.get<string[]>([dm, "input"]);
const current = res.value ?? [];
if (current.length === 0) return;
current.pop();
await ctx.editMessageText(inputMessage(...current), {
reply_markup: keyboard,
});
await kv.set([dm, "input"], current, {
expireIn: thirtyMinutesInMilliseconds,
});
});
// handle try again button
captcha.callbackQuery("again", async (ctx) => {
await ctx.editMessageReplyMarkup();
await sendCaptcha(ctx, ctx.chatId, ctx.from.id);
});
// handle any other updates
captcha.use(async (ctx) => {
const dm = ctx.chatId;
const solution = await kv.get<number>([dm, "solution"]);
if (solution.value === null) {
await ctx.reply(
"Please request to join @grammyjs before messaging me.",
);
return;
}
await ctx.reply("Please use one of the provided buttons", {
reply_markup: keyboard,
});
});
if (isDebug) bot.start();
else Deno.serve(webhookCallback(bot, "std/http", { secretToken }));