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 { 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;