Skip to content

feat(telegram): support business chat automation - #35342

Open
MilekhinAV wants to merge 2 commits into
NousResearch:mainfrom
MilekhinAV:feat/telegram-business-chat-automation
Open

feat(telegram): support business chat automation#35342
MilekhinAV wants to merge 2 commits into
NousResearch:mainfrom
MilekhinAV:feat/telegram-business-chat-automation

Conversation

@MilekhinAV

Copy link
Copy Markdown

Summary

  • Add Telegram Business/Chat Automation handlers for business connection lifecycle and deleted business message updates
  • Preserve business_connection_id in session/source metadata and outbound send/chat-action calls
  • Route authorization for business messages through the connected owner while keeping the external sender as alternate metadata
  • Add a scoped Telegram Business autoresponder prompt and regression tests for business metadata routing

Test Plan

  • python -m py_compile gateway/platforms/base.py gateway/platforms/telegram.py gateway/run.py gateway/session.py tests/gateway/test_telegram_thread_fallback.py
  • pytest -q tests/gateway/test_telegram_thread_fallback.py
  • pytest -q tests/gateway/test_telegram_send_path_health.py tests/gateway/test_telegram_reply_mode.py tests/gateway/test_telegram_noise_filter.py

@alt-glitch alt-glitch added type/feature New feature or request P3 Low — cosmetic, nice to have comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter labels May 30, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Competes with #26654 and #29727 — both open PRs implementing Telegram Business support. Closes #26653/#26858 (feature requests) and supersedes epic #21551.

This PR adds business connection lifecycle handlers and autoresponder prompt on top of the routing already proposed in the earlier PRs.

…s-chat-automation

# Conflicts:
#	gateway/run.py
@MilekhinAV
MilekhinAV force-pushed the feat/telegram-business-chat-automation branch from 7a4ef8b to ad81301 Compare May 31, 2026 20:51
@MilekhinAV

Copy link
Copy Markdown
Author

Resolved the merge conflicts by merging current origin/main into feat/telegram-business-chat-automation.

On overlap with #26654 / #29727: agreed this is in the same Telegram Business area, but this PR is scoped around the Chat Automation reply path:

  • preserving business_connection_id through SessionSource/send metadata so outbound replies and chat actions use the Business connection;
  • routing auth to the connected business owner while preserving the external sender as alternate metadata;
  • adding business connection lifecycle/deleted-message handling;
  • adding the scoped autoresponder prompt and regression coverage for business metadata routing.

#29727 looks focused on passive inbox persistence/diagnostics, while #26654 covers delegated inbox invocation/safe-mode behavior. If maintainers prefer consolidation, this PR can either be rebased on the chosen base PR or narrowed to the reply-routing/autoresponder pieces.

@teknium1 teknium1 added sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform labels Jul 13, 2026

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the detailed Business routing work. The capability is still absent on current main, but this patch needs a plugin-era, security-conscious rework before it can be salvaged.

Problems

  • The main implementation target, gateway/platforms/telegram.py, was moved by 5600105478ffde29d7566b45421b100eaa29c4ef to plugins/platforms/telegram/adapter.py; this branch is currently CONFLICTING/DIRTY.
  • gateway/platforms/telegram.py:6000 substitutes the Business owner into source.user_id. Gateway authorization reads that field (gateway/authz_mixin.py:317, :455-459), so an external contact would receive the owner's full agent path rather than restricted handling.
  • gateway/platforms/telegram.py:2026 adds the connection ID only to text sends; media paths such as send_voice (:3793-3801) and send_media_group (:3958-3965) omit it.
  • gateway/session.py:396 hardcodes Anton, Russian responses, and a fixed message into every Business session.

Suggested changes

  • Port the capability to plugins/platforms/telegram/adapter.py (or the standalone-plugin handler path proposed in #59159), preserving current intake behavior at plugins/platforms/telegram/adapter.py:7517.
  • Keep external contacts untrusted, add explicit restricted execution, centralize Business metadata propagation across all outbound paths, and make autoresponder policy user-configured.

Automated hermes-sweeper review.

str(user.id)
if user
else (str(chat.id) if chat_type in {"dm", "channel"} else None)
str(business_owner_id)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This maps an untrusted external contact to the owner's source.user_id, which is the identity consumed by gateway authorization. That grants the contact the owner's full agent/tool path rather than a restricted Business-contact path; keep caller identity separate from verified connection ownership.

parse_mode=ParseMode.MARKDOWN_V2,
reply_to_message_id=reply_to_id,
**thread_kwargs,
**business_kwargs,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This only propagates business_connection_id for text sends. The same metadata must be applied to the adapter's native voice, media-group, photo, document, video, image, animation, and streaming-edit paths, otherwise those replies take a different route.

Comment thread gateway/session.py
if getattr(context.source, "telegram_business_connection_id", None):
lines.append("")
lines.append(
"**Telegram Business / Chat Automation instructions:** You are Anton's autoresponder. "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is product-user-specific behavior embedded in a generic session prompt. Move the responder identity, language, and escalation text into opt-in user configuration or a skill so Telegram Business support is reusable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P3 Low — cosmetic, nice to have platform/telegram Telegram bot adapter sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants