-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgrammy.ts
More file actions
52 lines (42 loc) · 1.35 KB
/
grammy.ts
File metadata and controls
52 lines (42 loc) · 1.35 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
/**
* Shared Grammy bot.
* Used by app/bot/webhook (Vercel) and scripts/run-bot-local.ts (polling).
*/
import { Bot, type Context } from 'grammy';
import {
normalizeUsername,
upsertUserFromBot,
} from '../database/users.js';
import { handleBotAiResponse } from './responder.js';
export function createBot(token: string): Bot {
const bot = new Bot(token);
async function handleUserUpsert(ctx: Context): Promise<void> {
try {
const from = ctx.from;
if (!from) return;
const telegramUsername = normalizeUsername(from.username);
if (!telegramUsername) return;
const locale =
typeof from.language_code === 'string' ? from.language_code : null;
await upsertUserFromBot({ telegramUsername, locale });
} catch (err) {
console.error('[bot] upsert user failed', err);
}
}
bot.command('start', async (ctx: Context) => {
await handleUserUpsert(ctx);
await ctx.reply("That's @HyperlinksSpaceBot, you can use AI in bot and explore the app for more features");
});
bot.on('message:text', async (ctx: Context) => {
await handleUserUpsert(ctx);
await handleBotAiResponse(ctx);
});
bot.on('message:caption', async (ctx: Context) => {
await handleUserUpsert(ctx);
await handleBotAiResponse(ctx);
});
bot.catch((err) => {
console.error('[bot]', err);
});
return bot;
}