Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@rohannagpal
Copy link
Contributor

Summary

Adds DM thread awareness to Telegram sessions and improves inbound logging to
make DM threads observable and testable.

Why

Adds support for threaded conversations in Telegram.

How

  • Derives a thread-aware session key for DM topics so each thread becomes its
    own session context.
  • Keeps DM history limits stable by stripping thread suffixes when parsing
    session keys.
  • Adds verbose raw-update logging so inbound DM thread ids are visible during
    debugging.
  • Adjusts DM command targeting to respect thread-aware session keys.
  • Adds coverage for DM thread keying, DM command targeting, and DM history-
    limit parsing.

Tests

  • pnpm test (Full suite, vm)
  • pnpm test src/telegram/bot-message-context.DM-threads.test.ts
  • pnpm test src/telegram/bot.test.ts
  • pnpm test src/agents/pi-embedded-runner.get-DM-history-limit-from-session- key.returns-undefined-sessionkey-is-undefined.test.ts

Manual testing

  • With DM threads enabled, I sent two DM messages in different threads and
    confirmed distinct sessions in /status and different message_thread_id
    values in logs.
  • With DM threads disabled, I sent DM messages and confirmed
    message_thread_id was absent in logs and sessions behaved as a single
    thread.

Breaking changes

None.

Changelog

Added entries under 2026.1.23 (Unreleased).

AI assistance

  • AI-assisted: Yes
  • Testing: Fully tested (Unit + integration + manual DM checks)
  • Prompts/logs: Available in session history and vm logs if needed
  • Understanding: I tested it thoroughly, used /review in codex, and skimmed
    through the code.

@rohannagpal rohannagpal force-pushed the feat/telegram-dm-threads branch 2 times, most recently from bc4b512 to 05c82d5 Compare January 24, 2026 15:12
@steipete steipete self-assigned this Jan 25, 2026
@steipete steipete force-pushed the feat/telegram-dm-threads branch from 55cf610 to a7b13c4 Compare January 25, 2026 04:48
@steipete steipete merged commit 06a7e1e into openclaw:main Jan 25, 2026
42 checks passed
@steipete
Copy link
Contributor

Landed via temp rebase onto main.\n\n- Gate: pnpm lint && pnpm build && pnpm test\n- Land commit: a7b13c4\n- Merge commit: 06a7e1e\n\nThanks @rohannagpal!

jaydenfyi pushed a commit to jaydenfyi/clawdbot that referenced this pull request Jan 25, 2026
* Telegram: isolate dm topic sessions

* Tests: cap vitest workers

* Tests: cap Vitest workers on CI macOS

* Tests: avoid timer-based pi-ai stream mock

* Tests: increase embedded runner timeout

* fix: harden telegram dm thread handling (openclaw#1597) (thanks @rohannagpal)

---------

Co-authored-by: Peter Steinberger <[email protected]>
@e-jung
Copy link

e-jung commented Jan 25, 2026

hi - i'm not technical but i was dealing with a routing issue and this is what my ai agent fixed/suggested:

Summary for GitHub Comment
PR #1597 introduced a regression bug that affects all direct message channels (Telegram, Slack, Discord, iMessage, Signal, LINE, WebChat).

What the PR was trying to do:
Add support for Telegram DM threads so each thread gets its own session context.

The Bug:
In the code that updates delivery routing when a DM message arrives, it always uses route.mainSessionKey (points to main webchat session) instead of the correctly calculated sessionKey variable.

Impact: When you send a DM from Telegram/Slack/etc., the main session's routing gets corrupted - messages show as coming from wrong channel, and responses get routed to web UI instead of back to Telegram.

Simple fix: Change one line in each channel file:
// Wrong (current code):
sessionKey: route.mainSessionKey
// Should be:
sessionKey: sessionKey

This bug exists in 6 different channel files:

  • telegram/bot-message-context.js (line 392) - Fixed locally
  • slack/monitor/message-handler/prepare.js (line 441)
  • discord/monitor/message-handler.process.js (line 230)
  • line/bot-message-context.js (lines 221, 350)
  • imessage/monitor/monitor-provider.js (line 457)
  • signal/monitor/event-handler.js (line 114)
  • web/auto-reply/monitor/process-message.js (line 221)

The fix is identical for all channels - just swap route.mainSessionKey to sessionKey in the updateLastRoute call.

mcinteerj pushed a commit to mcinteerj/moltbot that referenced this pull request Jan 25, 2026
* Telegram: isolate dm topic sessions

* Tests: cap vitest workers

* Tests: cap Vitest workers on CI macOS

* Tests: avoid timer-based pi-ai stream mock

* Tests: increase embedded runner timeout

* fix: harden telegram dm thread handling (openclaw#1597) (thanks @rohannagpal)

---------

Co-authored-by: Peter Steinberger <[email protected]>
@louzhixian
Copy link
Contributor

Hi @rohannagpal! Thanks for this feature 🙌

I'm on 2026.1.24-3 with DM topics fully enabled (BotFather setting on, thread UI visible in Telegram). However, messages from different DM threads still go to the same session (agent:main:telegram:dm: without thread suffix).

I checked the gateway logs but don't see any message_thread_id being logged. Is there a way to enable verbose/raw update logging to verify if Telegram is actually sending message_thread_id for DM threads?

Any pointers appreciated!

@rohannagpal
Copy link
Contributor Author

@louzhixian this works for me:

  • Set file log level to debug: clawdbot config set logging.level debug
  • Restart the gateway.
  • Send a DM message and look for telegram update: lines in the gateway logs; they include message_thread_id if Telegram sends it.

@louzhixian
Copy link
Contributor

Thanks a lot!

@rohannagpal
Copy link
Contributor Author

@e-jung my changes did not touch any of the lines highlighted in your comment. What exactly is the routing issue that you are/were running into?

@louzhixian
Copy link
Contributor

@rohannagpal Somehow the bot told me that it can see the separated sessions now, thanks!
Btw, any plan to support the drafting message sending api? Messages in threads are duplicated during generating process which is king of annoying 😂

@rohannagpal
Copy link
Contributor Author

rohannagpal commented Jan 26, 2026 via email

rodrigouroz pushed a commit to rodrigouroz/moltbot that referenced this pull request Jan 26, 2026
* Telegram: isolate dm topic sessions

* Tests: cap vitest workers

* Tests: cap Vitest workers on CI macOS

* Tests: avoid timer-based pi-ai stream mock

* Tests: increase embedded runner timeout

* fix: harden telegram dm thread handling (openclaw#1597) (thanks @rohannagpal)

---------

Co-authored-by: Peter Steinberger <[email protected]>
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.

4 participants