- Implement Prisma storage adapter for Telegram session management - Add middleware to upsert Telegram user data - Extend TelegramUser model with additional fields - Add db:push command to turbo.json and package.json - Extend Prisma client with exists helper method
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import { serve } from '@hono/node-server';
|
|
import { config } from './config';
|
|
import { Bot, session, webhookCallback } from 'grammy';
|
|
import { Hono } from 'hono';
|
|
import { logger } from '@repo/logger';
|
|
import { PrismaStorageAdapter } from './session-storage';
|
|
import { BotContext } from './types';
|
|
import { upsertUser } from './middlewares/upsert-user';
|
|
|
|
const bot = new Bot<BotContext>(config.BOT_TOKEN);
|
|
const storage = new PrismaStorageAdapter();
|
|
|
|
bot.use(session({ storage, initial: () => ({}) }));
|
|
bot.use(upsertUser);
|
|
|
|
bot.command('start', async (ctx) => {
|
|
const session = await ctx.session;
|
|
await ctx.reply('Привет, ' + JSON.stringify(session));
|
|
});
|
|
|
|
if (config.MODE === 'webhook') {
|
|
logger.info('Starting in webhook mode...');
|
|
|
|
const app = new Hono();
|
|
app.post(`/${config.WEBHOOK_URI}`, webhookCallback(bot, 'hono'));
|
|
|
|
serve(
|
|
{
|
|
fetch: app.fetch,
|
|
port: Number(config.PORT),
|
|
},
|
|
(info) => {
|
|
logger.info(`Server is running on localhost:${info.port}`);
|
|
}
|
|
);
|
|
} else {
|
|
logger.info('Starting in polling mode...');
|
|
bot.start({
|
|
onStart: () => {
|
|
logger.info('Bot started polling...');
|
|
},
|
|
});
|
|
}
|