forked from joodaloop/telegram-bot-as-cf-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceive.ts
More file actions
41 lines (29 loc) · 1.14 KB
/
receive.ts
File metadata and controls
41 lines (29 loc) · 1.14 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
import { Env } from ".";
import { handleCallback, handleCommand, handleMessage } from "./handle";
import { Update } from "./types";
export async function telegramBotHandler(req: Request, env: Env) {
try {
const update: Update = await req.json();
const username = update.message?.chat.username
let auth: false | string[] = false
// uncomment the below line and add your username so only your messages are accepted
auth = ['urcades']
if (auth && username && !auth.includes(username)) {
console.error('Please go away, user #' + update.message?.chat.id + ' and username: ' + username);
return Response.json({ ok: true });
}
if (update.callback_query) {
await handleCallback(env.BOT_TOKEN, update.callback_query, env.D1_BINDING);
}
else if (update.message?.text?.startsWith('/')) {
await handleCommand(env.BOT_TOKEN, update.message, env.D1_BINDING);
}
else if (update.message?.text) {
await handleMessage(env.BOT_TOKEN, update.message, env.CLAUDE_TOKEN, env.D1_BINDING)
}
return Response.json({ ok: true });
} catch (error) {
console.error('Error handling update:', error);
return Response.json({ ok: true });
}
}