Fix onboarding background reply outside conversations context
This commit is contained in:
parent
8033377cdf
commit
910d6cf2b2
@ -19,6 +19,30 @@ const PACE_MAP: Record<string, string> = {
|
|||||||
active: 'active',
|
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) {
|
export async function onboardingConversation(conversation: BotConversation, ctx: BotContext) {
|
||||||
const lang = getLang(ctx.from?.language_code)
|
const lang = getLang(ctx.from?.language_code)
|
||||||
|
|
||||||
@ -178,7 +202,7 @@ export async function onboardingConversation(conversation: BotConversation, ctx:
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (!result) {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,18 +213,18 @@ export async function onboardingConversation(conversation: BotConversation, ctx:
|
|||||||
description: result.description,
|
description: result.description,
|
||||||
}) + (isHttps ? '' : `\n\n🔗 ${appUrl}`)
|
}) + (isHttps ? '' : `\n\n🔗 ${appUrl}`)
|
||||||
|
|
||||||
await ctx.api.sendMessage(
|
await sendTelegramMessage(chatId, questText, isHttps ? {
|
||||||
chatId,
|
reply_markup: {
|
||||||
questText,
|
inline_keyboard: [[{ text: t(lang, 'open_quest'), web_app: { url: appUrl } }]],
|
||||||
isHttps ? {
|
},
|
||||||
reply_markup: {
|
} : {})
|
||||||
inline_keyboard: [[{ text: t(lang, 'open_quest'), web_app: { url: appUrl } }]],
|
|
||||||
},
|
|
||||||
} : {}
|
|
||||||
)
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Background quest creation failed:', 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user