forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.ts
More file actions
451 lines (413 loc) · 18.1 KB
/
Copy pathbot.ts
File metadata and controls
451 lines (413 loc) · 18.1 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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import { Bot, InlineKeyboard, format, bold } from "gramio";
import { autoAnswerCallbackQuery } from "@gramio/auto-answer-callback-query";
import type Database from "better-sqlite3";
import type { Env, ButtonInput, OwnerStep } from "./types.js";
import { clearState, getState, listRecentPosts, setState } from "./db/client.js";
import {
accessDecisionKeyboard,
destinationPickerKeyboard,
ownerMenuKeyboard,
} from "./keyboards.js";
import { publishPost } from "./features/publish.js";
import { getButtons } from "./db/client.js";
import { updateButtonEverywhere } from "./features/buttons.js";
import { getUpcomingSchedule, schedulePost } from "./features/schedule.js";
import {
approve,
decline,
hasPendingRequest,
isApproved,
pendingRequests,
requestAccess,
} from "./features/access.js";
import { getDestinations, registerDestination } from "./features/destinations.js";
import { answerBusinessQuestion, extractMention } from "./features/groupMention.js";
interface StateData {
text?: string;
buttons?: ButtonInput[];
pendingLabel?: string;
selectedDestinations?: string[];
editPostId?: number;
editButtonId?: number;
}
function loadState(db: Database.Database, userId: string): { step: OwnerStep; data: StateData } {
const row = getState(db, userId);
if (!row) return { step: "idle", data: {} };
return { step: row.step as OwnerStep, data: JSON.parse(row.data_json) as StateData };
}
function saveState(db: Database.Database, userId: string, step: OwnerStep, data: StateData) {
setState(db, userId, step, data);
}
/** Parses simple schedule time input: "YYYY-MM-DD HH:mm" or "+30m" / "+2h" / "+1d". */
function parseScheduleTime(input: string): Date | null {
const trimmed = input.trim();
const relative = trimmed.match(/^\+(\d+)([mhd])$/i);
if (relative) {
const amount = Number(relative[1]);
const unit = relative[2].toLowerCase();
const ms = unit === "m" ? amount * 60_000 : unit === "h" ? amount * 3_600_000 : amount * 86_400_000;
return new Date(Date.now() + ms);
}
const absolute = new Date(trimmed.replace(" ", "T"));
if (!isNaN(absolute.getTime())) return absolute;
return null;
}
export function createBot(env: Env, db: Database.Database) {
const bot = new Bot(env.BOT_TOKEN).extend(autoAnswerCallbackQuery());
const isOwner = (userId: number | string) => String(userId) === String(env.OWNER_ID);
// -------------------------------------------------------------------
// Owner: entry points
// -------------------------------------------------------------------
bot.command("start", async (context) => {
if (!context.from) return;
if (isOwner(context.from.id)) {
clearState(db, String(context.from.id));
return context.send(
format`${bold("Telegram-Bot Control Center")}\nWhat would you like to do?`,
{ reply_markup: ownerMenuKeyboard() }
);
}
// Non-owner private chat: business assistance / access flow (doc section 6 & 9)
return handleUserGreeting(context.from.id, context.from.username, (text, kb) =>
context.send(text, kb ? { reply_markup: kb } : undefined)
);
});
bot.command("menu", async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
clearState(db, String(context.from.id));
return context.send("Control Center:", { reply_markup: ownerMenuKeyboard() });
});
// Owner runs this inside a group they've made the bot an admin of, to
// connect it as a publishing destination (doc section 14).
bot.command("addhere", async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
if (context.chat.type !== "group" && context.chat.type !== "supergroup") {
return context.send("Run /addhere inside the group you want to connect.");
}
registerDestination(db, String(context.chat.id), context.chat.title ?? "Untitled group", "group");
return context.send("✅ This group is now a connected destination.");
});
// Auto-registers channels/groups the moment the bot is promoted to admin.
bot.on("my_chat_member", async (context) => {
const update = context.payload;
const newStatus = update.new_chat_member?.status;
if (newStatus !== "administrator") return;
const chat = update.chat;
const type = chat.type === "channel" ? "channel" : "group";
registerDestination(db, String(chat.id), chat.title ?? "Untitled", type);
try {
await bot.api.sendMessage({
chat_id: env.OWNER_ID,
text: `✅ Connected new destination: "${chat.title ?? chat.id}" (${type}).`,
});
} catch {
/* owner may not have started a DM with the bot yet */
}
});
// -------------------------------------------------------------------
// Owner: main menu callbacks
// -------------------------------------------------------------------
bot.callbackQuery("menu:publish", async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
saveState(db, String(context.from.id), "compose_post_text", {});
return context.editText("Send me the text for your post.");
});
bot.callbackQuery("menu:schedule", async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
saveState(db, String(context.from.id), "compose_post_text", { pendingLabel: "schedule" });
return context.editText("Send me the text for the post you want to schedule.");
});
bot.callbackQuery("menu:destinations", async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
const dests = getDestinations(db);
const list = dests.length
? dests.map((d) => `• ${d.title} (${d.type})`).join("\n")
: "No destinations connected yet.";
return context.editText(
`${list}\n\nTo connect a new group: add the bot as admin, then run /addhere inside it.\nTo connect a channel: make the bot an admin of the channel - it will be added automatically.`,
{ reply_markup: new InlineKeyboard().text("⬅ Back", "menu:back") }
);
});
bot.callbackQuery("menu:access", async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
const pending = pendingRequests(db);
if (pending.length === 0) {
return context.editText("No pending access requests.", {
reply_markup: new InlineKeyboard().text("⬅ Back", "menu:back"),
});
}
for (const user of pending) {
await context.send(`Access request from ${user.username ? "@" + user.username : user.user_id}`, {
reply_markup: accessDecisionKeyboard(user.user_id),
});
}
return context.answer();
});
bot.callbackQuery("menu:edit_buttons", async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
const posts = listRecentPosts(db, 10).filter((p) => getButtons(db, p.id).length > 0);
if (posts.length === 0) {
return context.editText("No published posts with buttons yet.", {
reply_markup: new InlineKeyboard().text("⬅ Back", "menu:back"),
});
}
const kb = new InlineKeyboard();
posts.forEach((p) => {
kb.text(p.label.slice(0, 30) || `Post #${p.id}`, `edit_post:${p.id}`);
kb.row();
});
kb.text("⬅ Back", "menu:back");
saveState(db, String(context.from.id), "edit_button_pick_post", {});
return context.editText("Pick a post to update its button(s):", { reply_markup: kb });
});
bot.callbackQuery("menu:settings", async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
const upcoming = getUpcomingSchedule(db);
return context.editText(
`Owner: ${env.OWNER_ID}\nUpcoming scheduled posts: ${upcoming.length}`,
{ reply_markup: new InlineKeyboard().text("⬅ Back", "menu:back") }
);
});
bot.callbackQuery("menu:back", async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
clearState(db, String(context.from.id));
return context.editText("Control Center:", { reply_markup: ownerMenuKeyboard() });
});
bot.callbackQuery("cancel", async (context) => {
if (!context.from) return;
clearState(db, String(context.from.id));
if (isOwner(context.from.id)) {
return context.editText("Cancelled. Control Center:", { reply_markup: ownerMenuKeyboard() });
}
return context.editText("Cancelled.");
});
// -------------------------------------------------------------------
// Compose flow: destination picking (shared by Publish + Schedule)
// -------------------------------------------------------------------
bot.callbackQuery(/^dest_toggle:(.+)$/, async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
const chatId = context.queryData[1];
const { step, data } = loadState(db, String(context.from.id));
if (step !== "compose_post_pick_destinations") return;
const selected = new Set(data.selectedDestinations ?? []);
selected.has(chatId) ? selected.delete(chatId) : selected.add(chatId);
data.selectedDestinations = [...selected];
saveState(db, String(context.from.id), step, data);
const dests = getDestinations(db);
return context.editText("Choose where to publish:", {
reply_markup: destinationPickerKeyboard(dests, selected),
});
});
bot.callbackQuery("dest_done", async (context) => {
if (!context.from) return;
const userId = String(context.from.id);
const { data } = loadState(db, userId);
const destinations = data.selectedDestinations ?? [];
if (destinations.length === 0) {
return context.answer({ text: "Pick at least one destination first.", show_alert: true });
}
if (data.pendingLabel === "schedule") {
saveState(db, userId, "schedule_pick_time", data);
return context.editText(
"When should this publish? Send a date/time like `2026-09-20 10:00`, or a relative time like `+30m`, `+2h`, `+1d`."
);
}
// Immediate publish
const result = await publishPost(bot, db, {
label: (data.text ?? "").slice(0, 40) || "Untitled post",
text: data.text ?? "",
buttons: data.buttons ?? [],
destinationChatIds: destinations,
});
clearState(db, userId);
const failedNote = result.failed.length ? `\n⚠ Failed: ${result.failed.join(", ")}` : "";
return context.editText(
`✅ Published to ${result.sent} destination(s).${failedNote}`,
{ reply_markup: ownerMenuKeyboard() }
);
});
// -------------------------------------------------------------------
// Edit-buttons flow
// -------------------------------------------------------------------
bot.callbackQuery(/^edit_post:(\d+)$/, async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
const postId = Number(context.queryData[1]);
const buttons = getButtons(db, postId);
const kb = new InlineKeyboard();
buttons.forEach((b) => kb.text(`${b.label} → ${b.url}`, `edit_button:${b.id}:${postId}`).row());
kb.text("⬅ Back", "menu:edit_buttons");
saveState(db, String(context.from.id), "edit_button_pick_button", { editPostId: postId });
return context.editText("Pick a button to update:", { reply_markup: kb });
});
bot.callbackQuery(/^edit_button:(\d+):(\d+)$/, async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
const buttonId = Number(context.queryData[1]);
const postId = Number(context.queryData[2]);
saveState(db, String(context.from.id), "edit_button_new_url", {
editButtonId: buttonId,
editPostId: postId,
});
return context.editText("Send the new URL for this button.");
});
// -------------------------------------------------------------------
// Access-request decisions
// -------------------------------------------------------------------
bot.callbackQuery(/^access_approve:(.+)$/, async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
const userId = context.queryData[1];
approve(db, userId);
await bot.api
.sendMessage({ chat_id: userId, text: "✅ You've been approved! Send me a message any time." })
.catch(() => {});
return context.editText("Approved.");
});
bot.callbackQuery(/^access_decline:(.+)$/, async (context) => {
if (!context.from || !isOwner(context.from.id)) return;
const userId = context.queryData[1];
decline(db, userId);
await bot.api
.sendMessage({ chat_id: userId, text: "Your access request was declined." })
.catch(() => {});
return context.editText("Declined.");
});
// -------------------------------------------------------------------
// Free-text handling: drives the owner's multi-step workflows, and
// provides business assistance / access requests for normal users
// (doc sections 6, 9, 10, 25-26).
// -------------------------------------------------------------------
bot.on("message", async (context) => {
if (!context.from || !context.text) return;
const userId = String(context.from.id);
const text = context.text.trim();
if (text.startsWith("/")) return; // commands are handled above
// Group mention assistance (doc section 7 & 17) - only reacts when
// the bot is specifically @mentioned in a group/supergroup.
if (context.chat.type === "group" || context.chat.type === "supergroup") {
const me = await bot.api.getMe();
if (me.username) {
const query = extractMention(text, me.username);
if (query !== null) {
return context.send(answerBusinessQuestion(query));
}
}
return; // ignore ordinary group chatter
}
// -------------------- Owner multi-step workflows --------------------
if (isOwner(context.from.id)) {
const { step, data } = loadState(db, userId);
if (step === "compose_post_text") {
data.text = text;
saveState(db, userId, "compose_post_button_label", data);
return context.send(
"Send a button label to attach (e.g. `Visit Website`), or /skip to continue without buttons."
);
}
if (step === "compose_post_button_label") {
if (text === "/skip" || text === "/done") {
saveState(db, userId, "compose_post_pick_destinations", data);
const dests = getDestinations(db);
if (dests.length === 0) {
clearState(db, userId);
return context.send(
"No destinations connected yet. Add the bot as admin to a channel/group first."
);
}
return context.send("Choose where to publish:", {
reply_markup: destinationPickerKeyboard(dests, new Set()),
});
}
data.pendingLabel = text;
saveState(db, userId, "compose_post_button_url", data);
return context.send(`Send the URL for the "${text}" button.`);
}
if (step === "compose_post_button_url") {
const buttons = data.buttons ?? [];
buttons.push({ label: data.pendingLabel ?? "Button", url: text });
data.buttons = buttons;
data.pendingLabel = undefined;
saveState(db, userId, "compose_post_button_label", data);
return context.send('Add another button label, or /done to continue.');
}
if (step === "schedule_pick_time") {
const when = parseScheduleTime(text);
if (!when || when.getTime() < Date.now()) {
return context.send(
"I couldn't understand that time, or it's in the past. Try `2026-09-20 10:00` or `+30m`."
);
}
schedulePost(db, data.text ?? "", data.buttons ?? [], data.selectedDestinations ?? [], when);
clearState(db, userId);
return context.send(`⏰ Scheduled for ${when.toLocaleString()}.`, {
reply_markup: ownerMenuKeyboard(),
});
}
if (step === "edit_button_new_url") {
if (data.editButtonId == null || data.editPostId == null) {
clearState(db, userId);
return context.send("Something went wrong - please start over from the menu.");
}
const result = await updateButtonEverywhere(bot, db, data.editButtonId, text, data.editPostId);
clearState(db, userId);
return context.send(
`✅ Button updated on ${result.updated} message(s).${result.failed ? ` ⚠ ${result.failed} failed.` : ""}`,
{ reply_markup: ownerMenuKeyboard() }
);
}
// No active workflow - show the menu.
return context.send("Control Center:", { reply_markup: ownerMenuKeyboard() });
}
// -------------------- Normal users (private chat) --------------------
return handleUserMessage(userId, context.from.username, text, (reply, kb) =>
context.send(reply, kb ? { reply_markup: kb } : undefined)
);
});
// Shared logic for a brand-new /start from a non-owner.
async function handleUserGreeting(
userId: number | string,
username: string | undefined,
send: (text: string, kb?: InlineKeyboard) => Promise<unknown>
) {
const id = String(userId);
if (isApproved(db, id)) {
return send("Hi! How can I help you today?");
}
if (hasPendingRequest(db, id)) {
return send("Your access request is still pending. We'll let you know once it's approved.");
}
requestAccess(db, id, username);
await notifyOwnerOfRequest(id, username);
return send("Thanks! Your access request has been sent to the owner for approval.");
}
// Shared logic for any subsequent free-text message from a non-owner.
async function handleUserMessage(
userId: string,
username: string | undefined,
text: string,
send: (text: string, kb?: InlineKeyboard) => Promise<unknown>
) {
if (isApproved(db, userId)) {
return send(answerBusinessQuestion(text));
}
if (hasPendingRequest(db, userId)) {
return send("Your access request is still pending approval.");
}
requestAccess(db, userId, username);
await notifyOwnerOfRequest(userId, username);
return send("Thanks! Your access request has been sent to the owner for approval.");
}
async function notifyOwnerOfRequest(userId: string, username?: string) {
try {
await bot.api.sendMessage({
chat_id: env.OWNER_ID,
text: `New access request from ${username ? "@" + username : userId}`,
reply_markup: accessDecisionKeyboard(userId),
});
} catch (err) {
console.error("Could not notify owner of access request:", err);
}
}
bot.onStart(({ info }) => {
console.log(`Telegram-Bot is running as @${info.username}`);
});
return bot;
}