diff --git a/bot/conversations/onboarding.ts b/bot/conversations/onboarding.ts index 4ba8f4f..46e0db6 100644 --- a/bot/conversations/onboarding.ts +++ b/bot/conversations/onboarding.ts @@ -19,6 +19,30 @@ const PACE_MAP: Record = { active: 'active', } +async function sendTelegramMessage( + chatId: number, + text: string, + extra?: Record +) { + const token = process.env.TELEGRAM_BOT_TOKEN + if (!token) throw new Error('TELEGRAM_BOT_TOKEN is not set') + + const response = await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + chat_id: chatId, + text, + ...extra, + }), + }) + + if (!response.ok) { + const body = await response.text() + throw new Error(`Telegram sendMessage failed (${response.status}): ${body}`) + } +} + export async function onboardingConversation(conversation: BotConversation, ctx: BotContext) { const lang = getLang(ctx.from?.language_code) @@ -178,7 +202,7 @@ export async function onboardingConversation(conversation: BotConversation, ctx: }) if (!result) { - await ctx.api.sendMessage(chatId, 'Something went wrong. Please try /start again.') + await sendTelegramMessage(chatId, 'Something went wrong. Please try /start again.') return } @@ -189,18 +213,18 @@ export async function onboardingConversation(conversation: BotConversation, ctx: description: result.description, }) + (isHttps ? '' : `\n\nšŸ”— ${appUrl}`) - await ctx.api.sendMessage( - chatId, - questText, - isHttps ? { - reply_markup: { - inline_keyboard: [[{ text: t(lang, 'open_quest'), web_app: { url: appUrl } }]], - }, - } : {} - ) + await sendTelegramMessage(chatId, questText, isHttps ? { + reply_markup: { + inline_keyboard: [[{ text: t(lang, 'open_quest'), web_app: { url: appUrl } }]], + }, + } : {}) } catch (error) { console.error('Background quest creation failed:', error) - await ctx.api.sendMessage(chatId, 'Something went wrong. Please try /start again.') + try { + await sendTelegramMessage(chatId, 'Something went wrong. Please try /start again.') + } catch (sendError) { + console.error('Failed to send fallback error message:', sendError) + } } })()