Fix onboarding background reply outside conversations context
All checks were successful
CI/CD / lint (push) Successful in 8s
CI/CD / build (push) Successful in 21s
CI/CD / deploy (push) Successful in 35s

This commit is contained in:
Jaymiesh 2026-02-11 16:40:50 +07:00
parent 8033377cdf
commit 910d6cf2b2

View File

@ -19,6 +19,30 @@ const PACE_MAP: Record<string, string> = {
active: 'active',
}
async function sendTelegramMessage(
chatId: number,
text: string,
extra?: Record<string, unknown>
) {
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)
}
}
})()