- Replace Redis/in-memory storage with SQLite for persistence - Add database.ts service with tables for users, groups, purchases, participants - Update state.ts and groupState.ts to use SQLite backend - Fix buyer_name to use display name instead of Telegram ID - Remove legacy reminder array handlers (now using 3-slot system) - Add better-sqlite3 dependency, remove ioredis - Update env.example with BOT_DATABASE_PATH option - Add data/ directory to .gitignore for database files
70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
import TelegramBot from 'node-telegram-bot-api';
|
|
import { stateManager } from '../services/state';
|
|
import { logger, logUserAction } from '../services/logger';
|
|
import { getMainMenuKeyboard, getLightningAddressKeyboard } from '../utils/keyboards';
|
|
import { messages } from '../messages';
|
|
|
|
/**
|
|
* Handle /start command
|
|
*/
|
|
export async function handleStart(bot: TelegramBot, msg: TelegramBot.Message): Promise<void> {
|
|
const chatId = msg.chat.id;
|
|
const userId = msg.from?.id;
|
|
const username = msg.from?.username;
|
|
|
|
if (!userId) {
|
|
await bot.sendMessage(chatId, messages.errors.userNotIdentified);
|
|
return;
|
|
}
|
|
|
|
logUserAction(userId, 'Started bot', {
|
|
username: username,
|
|
firstName: msg.from?.first_name,
|
|
});
|
|
|
|
try {
|
|
// Check if user exists
|
|
let user = await stateManager.getUser(userId);
|
|
|
|
if (!user) {
|
|
// Create new user
|
|
user = await stateManager.createUser(
|
|
userId,
|
|
username,
|
|
msg.from?.first_name,
|
|
msg.from?.last_name
|
|
);
|
|
}
|
|
|
|
// Welcome message
|
|
await bot.sendMessage(chatId, messages.start.welcome, { parse_mode: 'Markdown' });
|
|
|
|
// Check if lightning address is set
|
|
if (!user.lightningAddress) {
|
|
await bot.sendMessage(chatId, messages.start.needAddressWithOptions(username), {
|
|
parse_mode: 'Markdown',
|
|
reply_markup: getLightningAddressKeyboard(username),
|
|
});
|
|
|
|
await stateManager.updateUserState(userId, 'awaiting_lightning_address');
|
|
} else {
|
|
// Show main menu
|
|
await bot.sendMessage(
|
|
chatId,
|
|
messages.start.addressSet(user.lightningAddress),
|
|
{
|
|
parse_mode: 'Markdown',
|
|
reply_markup: getMainMenuKeyboard(),
|
|
}
|
|
);
|
|
|
|
await stateManager.updateUserState(userId, 'idle');
|
|
}
|
|
} catch (error) {
|
|
logger.error('Error in handleStart', { error, userId });
|
|
await bot.sendMessage(chatId, messages.errors.startAgain);
|
|
}
|
|
}
|
|
|
|
export default handleStart;
|