feat: add Telegram bot integration with AI-powered food/exercise logging#1099
feat: add Telegram bot integration with AI-powered food/exercise logging#1099serjsv87 wants to merge 4 commits intoCodeWithCJ:mainfrom
Conversation
- Add TelegramBotService with modular architecture (intentExecutor, telegramAiService, telegramTranslations) - Add telegramRoutes and chatRepository for bot-user linking - Add chatService for conversation management - Add Telegram.api.zod schema to shared package - Register /api/telegram routes and webhook handler in server - Add Telegram setup documentation - Add English translations for Telegram settings UI Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request introduces a Telegram bot integration for SparkyFitness, enabling users to log nutrition, exercise, and measurements via AI-driven chat. Key additions include a TelegramBotService for message handling, an intentExecutor for database updates, and a TelegramAiService for context-aware interactions. The PR also adds necessary API routes, database models for chat history, and setup documentation. Feedback highlights several issues: potential memory leaks due to uncleared intervals in the bot service, a security vulnerability in the webhook endpoint lacking secret verification, and the absence of timeouts for external AI API calls. Improvements regarding code duplication and hardcoded constants were also suggested.
SparkyFitnessServer/integrations/telegram/telegramBotService.ts
Outdated
Show resolved
Hide resolved
- telegramBotService: use try/finally to guarantee typingInterval is always cleared in processMessage and handleDataRequest - telegramBotService: remove duplicate getTranslations private method, use the already-imported function from telegramTranslations.ts - telegramRoutes: verify X-Telegram-Bot-Api-Secret-Token header when TELEGRAM_WEBHOOK_SECRET env var is set (security hardening) - chatService: add 60s AbortController timeout to OpenAI-compatible provider fetch calls to prevent indefinite hangs - intentExecutor: extract water unit conversion factors into named constants (WATER_ML_PER_UNIT, DEFAULT_WATER_ML_PER_UNIT, DEFAULT_DRINK_ML)
…ng exercise range helper
5353d64 to
f401d81
Compare
87c25af to
a4800c6
Compare
Tip
Help us review and merge your PR faster!
Please ensure you have completed the Checklist below.
For Frontend changes, please run
pnpm run validateto check for any errors.PRs that include tests and clear screenshots are highly preferred!
Note: AI-generated descriptions must be manually edited for conciseness. Do not paste raw AI summaries.
Description
What problem does this PR solve?
Adds a Telegram bot integration that allows users to log food and exercises directly via Telegram using AI-powered natural language processing.
How did you implement the solution?
Added a modular TelegramBotService split into
telegramBotService.ts(core),intentExecutor.ts(action dispatch),telegramAiService.ts(AI context), andtelegramTranslations.ts(i18n). AddedtelegramRoutes.tsfor webhook and account linking endpoints,chatRepository.tsfor conversation storage, andchatService.tsfor message handling. Registered/api/telegramroutes and webhook handler in the server. AddedTelegram.api.zodschema to the shared package.Linked Issue: #
How to Test
TELEGRAM_BOT_TOKENandTELEGRAM_BOT_NAMEin your.env[TELEGRAM BOT] Bot initializedappears in logs/startPR Type
Checklist
All PRs:
New features only:
Frontend changes (
SparkyFitnessFrontend/orsrc/):pnpm run validateand it passes.en) translation file.Backend changes (
SparkyFitnessServer/):rls_policies.sqlfor any new user-specific tables. (Note: No new user-specific tables were added; chat history uses existing user auth).UI changes (components, screens, pages):
Screenshots
Click to expand
Before
After
Notes for Reviewers