Skip to content

feat(integrations): implement telegram bot command framework - #549

Merged
l1ttps merged 2 commits into
mainfrom
refactor/telegram-connect
Jul 17, 2026
Merged

feat(integrations): implement telegram bot command framework#549
l1ttps merged 2 commits into
mainfrom
refactor/telegram-connect

Conversation

@l1ttps

@l1ttps l1ttps commented Jul 17, 2026

Copy link
Copy Markdown
Member
  • Add TelegramBotService for centralized bot API interactions
  • Register /pair, /unpair, and /help commands
  • Map /start to /pair for deep link support
  • Improve user instructions for connection process

Summary by CodeRabbit

  • New Features
    • Added Telegram bot support for sending messages and validating bot connections.
    • Added Telegram commands for pairing, disconnecting, and viewing available help.
    • Added support for registering bot commands automatically.
    • Added /start <token> compatibility for existing pairing flows.
  • Improvements
    • Telegram connection and webhook flows now provide clearer responses for successful, unsuccessful, and invalid actions.
    • Pairing instructions are shown when the pairing command is used without a token.

- Add TelegramBotService for centralized bot API interactions
- Register /pair, /unpair, and /help commands
- Map /start to /pair for deep link support
- Improve user instructions for connection process
@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 950dc813-06af-4c95-954c-2782c35e4b09

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Telegram bot API access is centralized in a cached service. Connection flows use that service, while webhook and polling updates now provide bot context to dispatch /pair, /unpair, /help, and /start commands.

Changes

Telegram integration

Layer / File(s) Summary
Centralize Telegram API access
core-api/package.json, core-api/src/modules/integrations/telegram-bot.service.ts, core-api/src/modules/integrations/integrations.module.ts
Adds the Telegram bot dependency and injectable service for cached bot instances, message delivery, command registration, and token validation.
Refactor Telegram connection lifecycle
core-api/src/modules/integrations/telegram-connect.service.ts, core-api/src/modules/integrations/integrations.controller.ts
Routes username lookup and notifications through TelegramBotService and adds disconnectByChatId for active connections.
Dispatch Telegram commands
core-api/src/modules/integrations/telegram-webhook.service.ts, core-api/src/modules/integrations/integrations.controller.ts, core-api/src/modules/integrations/telegram-polling.service.ts
Passes bot context into update processing, synchronizes commands lazily, parses command mentions, and handles pairing, unpairing, and help responses.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Telegram
  participant integrationsController
  participant telegramWebhookService
  participant TelegramBotService
  participant TelegramConnectService
  Telegram->>integrationsController: Send webhook update
  integrationsController->>telegramWebhookService: processUpdate with bot context
  telegramWebhookService->>TelegramBotService: Synchronize commands
  telegramWebhookService->>TelegramConnectService: Confirm or remove connection
  TelegramConnectService->>TelegramBotService: Send connection response
  TelegramBotService->>Telegram: Deliver message
Loading

Possibly related PRs

  • oasm-platform/open-asm#542: Extends the related Telegram pairing and webhook implementation around connection confirmation and bot services.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding a Telegram bot command framework in integrations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/telegram-connect

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
core-api/src/modules/integrations/integrations.controller.ts (1)

214-232: 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

Authenticate Telegram webhook requests.
This public route accepts arbitrary updates, so a forged POST can spoof /unpair and disconnect a chat from the integration. Pass a webhook secret to setWebhook and reject requests whose X-Telegram-Bot-Api-Secret-Token header doesn’t match before calling processUpdate.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core-api/src/modules/integrations/integrations.controller.ts` around lines
214 - 232, Authenticate requests in telegramWebhook before invoking
TelegramWebhookService.processUpdate: load the configured webhook secret,
compare it against the X-Telegram-Bot-Api-Secret-Token header, and reject
mismatches or missing headers. Update the webhook registration path to pass the
same secret to setWebhook, while preserving the existing botToken and
integrationId handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@core-api/src/modules/integrations/integrations.module.ts`:
- Line 11: Replace the relative TelegramBotService imports with the configured
`@/` alias in integrations.module.ts (11-11), telegram-connect.service.ts (18-18),
and telegram-webhook.service.ts (3-3), preserving the existing imported symbol
and usage.

In `@core-api/src/modules/integrations/telegram-webhook.service.ts`:
- Around line 131-149: Update handlePair to HTML-escape ctx.firstName before
interpolating it into the instruction message, reusing the existing
HTML-escaping helper and preserving the “there” fallback so names containing
special characters cannot break Telegram’s HTML message.

---

Outside diff comments:
In `@core-api/src/modules/integrations/integrations.controller.ts`:
- Around line 214-232: Authenticate requests in telegramWebhook before invoking
TelegramWebhookService.processUpdate: load the configured webhook secret,
compare it against the X-Telegram-Bot-Api-Secret-Token header, and reject
mismatches or missing headers. Update the webhook registration path to pass the
same secret to setWebhook, while preserving the existing botToken and
integrationId handling.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: f5c4f724-c9fe-4570-bf8a-9d341afa919b

📥 Commits

Reviewing files that changed from the base of the PR and between 72ab459 and 6d34e7d.

📒 Files selected for processing (7)
  • core-api/package.json
  • core-api/src/modules/integrations/integrations.controller.ts
  • core-api/src/modules/integrations/integrations.module.ts
  • core-api/src/modules/integrations/telegram-bot.service.ts
  • core-api/src/modules/integrations/telegram-connect.service.ts
  • core-api/src/modules/integrations/telegram-polling.service.ts
  • core-api/src/modules/integrations/telegram-webhook.service.ts

import { TelegramConnectService } from './telegram-connect.service';
import { TelegramWebhookService } from './telegram-webhook.service';
import { TelegramPollingService } from './telegram-polling.service';
import { TelegramBotService } from './telegram-bot.service';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Use @/ aliases for the new internal imports.

  • core-api/src/modules/integrations/integrations.module.ts#L11-L11: replace the relative TelegramBotService import.
  • core-api/src/modules/integrations/telegram-connect.service.ts#L18-L18: replace the relative TelegramBotService import.
  • core-api/src/modules/integrations/telegram-webhook.service.ts#L3-L3: replace the relative TelegramBotService import.

As per coding guidelines, “Use @/ alias for imports in core-api.”

📍 Affects 3 files
  • core-api/src/modules/integrations/integrations.module.ts#L11-L11 (this comment)
  • core-api/src/modules/integrations/telegram-connect.service.ts#L18-L18
  • core-api/src/modules/integrations/telegram-webhook.service.ts#L3-L3
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core-api/src/modules/integrations/integrations.module.ts` at line 11, Replace
the relative TelegramBotService imports with the configured `@/` alias in
integrations.module.ts (11-11), telegram-connect.service.ts (18-18), and
telegram-webhook.service.ts (3-3), preserving the existing imported symbol and
usage.

Source: Coding guidelines

Comment on lines +131 to +149
private async handlePair(args: string, ctx: CommandContext): Promise<void> {
if (!args) {
const firstName = ctx.firstName ?? 'there';
const instruction =
`👋 Hi ${firstName}!\n\n` +
`To connect your Telegram chat to OpenASM, you need to send a pairing token.\n\n` +
`📋 <b>How to connect:</b>\n` +
`1. Open OpenASM Console → Integrations → Telegram\n` +
`2. Click <b>"Pair"</b> to generate a pairing token\n` +
`3. Send the token here:\n` +
` <code>/pair &lt;your-token&gt;</code>\n\n` +
`❓ If you need help, type /help`;

if (ctx.botToken) {
await this.telegramBotService.sendMessage(
ctx.botToken,
ctx.chatId,
instruction,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Locate the service and any HTML-escaping helpers/usages nearby.
git ls-files 'core-api/src/modules/integrations/telegram-webhook.service.ts' \
  'core-api/src/**/*' | sed -n '1,200p'

printf '\n--- outline ---\n'
ast-grep outline core-api/src/modules/integrations/telegram-webhook.service.ts --view expanded

printf '\n--- search for escaping helpers/usages ---\n'
rg -n "escape|html|firstName|sendMessage\\(" core-api/src/modules/integrations -g '!**/dist/**' -g '!**/build/**'

printf '\n--- relevant slice ---\n'
sed -n '120,170p' core-api/src/modules/integrations/telegram-webhook.service.ts

Repository: oasm-platform/open-asm

Length of output: 17617


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '1,120p' core-api/src/modules/integrations/telegram-bot.service.ts
printf '\n---\n'
sed -n '360,420p' core-api/src/modules/integrations/telegram-connect.service.ts

Repository: oasm-platform/open-asm

Length of output: 3601


Escape ctx.firstName before building the HTML message. In core-api/src/modules/integrations/telegram-webhook.service.ts:133, TelegramBotService.sendMessage() sends with parse_mode: 'HTML', so names containing <, > or & can break the greeting and stop the /pair instructions from being delivered. Reuse the existing HTML-escaping helper here.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core-api/src/modules/integrations/telegram-webhook.service.ts` around lines
131 - 149, Update handlePair to HTML-escape ctx.firstName before interpolating
it into the instruction message, reusing the existing HTML-escaping helper and
preserving the “there” fallback so names containing special characters cannot
break Telegram’s HTML message.

@l1ttps
l1ttps merged commit 8574eac into main Jul 17, 2026
17 checks passed
@l1ttps
l1ttps deleted the refactor/telegram-connect branch July 17, 2026 06:36
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