Skip to content

feat: add Telegram bot integration with AI-powered food/exercise logging#1099

Open
serjsv87 wants to merge 4 commits intoCodeWithCJ:mainfrom
serjsv87:feature/telegram-clean
Open

feat: add Telegram bot integration with AI-powered food/exercise logging#1099
serjsv87 wants to merge 4 commits intoCodeWithCJ:mainfrom
serjsv87:feature/telegram-clean

Conversation

@serjsv87
Copy link
Copy Markdown

@serjsv87 serjsv87 commented Apr 7, 2026

  • 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

Tip

Help us review and merge your PR faster!
Please ensure you have completed the Checklist below.
For Frontend changes, please run pnpm run validate to 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), and telegramTranslations.ts (i18n). Added telegramRoutes.ts for webhook and account linking endpoints, chatRepository.ts for conversation storage, and chatService.ts for message handling. Registered /api/telegram routes and webhook handler in the server. Added Telegram.api.zod schema to the shared package.

Linked Issue: #

How to Test

  1. Set TELEGRAM_BOT_TOKEN and TELEGRAM_BOT_NAME in your .env
  2. Start the server — verify [TELEGRAM BOT] Bot initialized appears in logs
  3. Open Telegram, find your bot, send /start
  4. In Settings → External Providers → Telegram, generate a link code and send it to the bot
  5. Log a meal: send "I just ate 200g of chicken" — verify it logs to your food diary

PR Type

  • Issue (bug fix)
  • New Feature
  • Refactor
  • Documentation

Checklist

All PRs:

  • [MANDATORY - ALL] Integrity & License: I certify this is my own work, free of malicious code, and I agree to the License terms.

New features only:

  • [MANDATORY for new feature] Alignment: I have raised a GitHub issue and it was reviewed/approved by maintainers or it was approved on Discord.

Frontend changes (SparkyFitnessFrontend/ or src/):

  • [MANDATORY for Frontend changes] Quality: I have run pnpm run validate and it passes.
  • [MANDATORY for Frontend changes] Translations: I have only updated the English (en) translation file.

Backend changes (SparkyFitnessServer/):

  • [MANDATORY for Backend changes] Code Quality: I have run typecheck, lint, and tests. New files use TypeScript, new endpoints have Zod schemas, and new endpoints include tests.
  • [MANDATORY for Backend changes] Database Security: I have updated rls_policies.sql for any new user-specific tables. (Note: No new user-specific tables were added; chat history uses existing user auth).

UI changes (components, screens, pages):

  • [MANDATORY for UI changes] Screenshots: I have attached Before/After screenshots below.

Screenshots

Click to expand

Before

before

After

after

Notes for Reviewers

Optional — use this for anything that doesn't fit above: known tradeoffs, areas you'd like specific feedback on, qustions you have or context that helps reviewers.

- 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>
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

serjsv87 added 2 commits April 7, 2026 20:06
- 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)
@serjsv87 serjsv87 force-pushed the feature/telegram-clean branch from 5353d64 to f401d81 Compare April 8, 2026 09:24
@serjsv87 serjsv87 force-pushed the feature/telegram-clean branch from 87c25af to a4800c6 Compare April 8, 2026 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant