From a8cab41a008c5a624d982251d0f074cddd4bd891 Mon Sep 17 00:00:00 2001 From: Jaymiesh Date: Wed, 11 Feb 2026 16:23:34 +0700 Subject: [PATCH] Fix onboarding webhook timeout by running quest creation in background --- bot/conversations/onboarding.ts | 72 ++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/bot/conversations/onboarding.ts b/bot/conversations/onboarding.ts index b8d5f88..ae4bcdc 100644 --- a/bot/conversations/onboarding.ts +++ b/bot/conversations/onboarding.ts @@ -155,37 +155,53 @@ export async function onboardingConversation(conversation: BotConversation, ctx: return } - const result = await conversation.external(() => - createQuestFromOnboarding({ - userId, - cityId, - cityName, - days, - companions, - pace, - userComment, - lang, - }) - ) - - if (!result) { + const chatId = ctx.chat?.id + if (!chatId) { await ctx.reply('Something went wrong. Please try /start again.') return } - const appUrl = process.env.APP_URL || 'https://guidly.example.com' - const isHttps = appUrl.startsWith('https://') - const questText = t(lang, 'quest_created', { - title: result.title, - description: result.description, - }) + (isHttps ? '' : `\n\nšŸ”— ${appUrl}`) + // Run heavy generation in background so webhook handlers can return quickly. + void (async () => { + try { + const result = await createQuestFromOnboarding({ + userId, + cityId, + cityName, + days, + companions, + pace, + userComment, + lang, + }) - await ctx.reply( - questText, - isHttps ? { - reply_markup: { - inline_keyboard: [[ { text: t(lang, 'open_quest'), web_app: { url: appUrl } } ]], - }, - } : {} - ) + if (!result) { + await ctx.api.sendMessage(chatId, 'Something went wrong. Please try /start again.') + return + } + + const appUrl = process.env.APP_URL || 'https://guidly.example.com' + const isHttps = appUrl.startsWith('https://') + const questText = t(lang, 'quest_created', { + title: result.title, + 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 } }]], + }, + } : {} + ) + } catch (error) { + console.error('Background quest creation failed:', error) + await ctx.api.sendMessage(chatId, 'Something went wrong. Please try /start again.') + } + })() + + // Conversation finishes immediately; final quest message will be sent by background task. + return }