mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 22:34:03 +00:00
feat(WHE-22): ingest configured topic messages with idempotent persistence
This commit is contained in:
@@ -2,12 +2,37 @@ import { webhookCallback } from 'grammy'
|
||||
|
||||
import { createTelegramBot } from './bot'
|
||||
import { getBotRuntimeConfig } from './config'
|
||||
import {
|
||||
createPurchaseMessageRepository,
|
||||
registerPurchaseTopicIngestion
|
||||
} from './purchase-topic-ingestion'
|
||||
import { createBotWebhookServer } from './server'
|
||||
|
||||
const runtime = getBotRuntimeConfig()
|
||||
const bot = createTelegramBot(runtime.telegramBotToken)
|
||||
const webhookHandler = webhookCallback(bot, 'std/http')
|
||||
|
||||
let closePurchaseRepository: (() => Promise<void>) | undefined
|
||||
|
||||
if (runtime.purchaseTopicIngestionEnabled) {
|
||||
const purchaseRepositoryClient = createPurchaseMessageRepository(runtime.databaseUrl!)
|
||||
closePurchaseRepository = purchaseRepositoryClient.close
|
||||
|
||||
registerPurchaseTopicIngestion(
|
||||
bot,
|
||||
{
|
||||
householdId: runtime.householdId!,
|
||||
householdChatId: runtime.telegramHouseholdChatId!,
|
||||
purchaseTopicId: runtime.telegramPurchaseTopicId!
|
||||
},
|
||||
purchaseRepositoryClient.repository
|
||||
)
|
||||
} else {
|
||||
console.warn(
|
||||
'Purchase topic ingestion is disabled. Set DATABASE_URL, HOUSEHOLD_ID, TELEGRAM_HOUSEHOLD_CHAT_ID, and TELEGRAM_PURCHASE_TOPIC_ID to enable.'
|
||||
)
|
||||
}
|
||||
|
||||
const server = createBotWebhookServer({
|
||||
webhookPath: runtime.telegramWebhookPath,
|
||||
webhookSecret: runtime.telegramWebhookSecret,
|
||||
@@ -23,6 +48,10 @@ if (import.meta.main) {
|
||||
console.log(
|
||||
`@household/bot webhook server started on :${runtime.port} path=${runtime.telegramWebhookPath}`
|
||||
)
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
void closePurchaseRepository?.()
|
||||
})
|
||||
}
|
||||
|
||||
export { server }
|
||||
|
||||
Reference in New Issue
Block a user