Skip to content

feat: add group history backfill and offline auto-reply - #42

Merged
williamcachamwri merged 2 commits into
williamcachamwri:mainfrom
ntdatt812:feat/history-autoreply
Jun 14, 2026
Merged

williamcachamwri merged 2 commits into
williamcachamwri:mainfrom
ntdatt812:feat/history-autoreply

Conversation

@ntdatt812

Copy link
Copy Markdown
Contributor

Add two Telegram commands and harden startup against Zalo rate limits.

  • /history [N]: backfill recent GROUP chat history into the current topic via getGroupChatHistory, replayed through the existing message pipeline so already-synced messages dedupe themselves. DM topics are rejected because Zalo exposes no 1-1 history API.
  • /autoreply on|off|status: bridge-level offline auto-reply for DMs. Implemented locally (new src/zalo/autoReply.ts) instead of zca-js createAutoReply, which is zBusiness-only and fails on personal accounts. State persists to data/autoreply.json.

Anti-ban hardening:

  • Auto-reply answers DMs only, with a 30-min per-peer cooldown, a global cap of 12 replies/hour, and a randomized 3-8s human-like delay (never instant).
  • Fix startup member-cache warmup that fired every getGroupInfo/getUserInfo call at once (setTimeout 0 despite a "2s stagger" comment); it is now serialized with a real 2s gap to avoid tripping Zalo's rate limiter (code 221).

Register both commands in the Telegram command menu.

Add two Telegram commands and harden startup against Zalo rate limits.

- /history [N]: backfill recent GROUP chat history into the current topic
  via getGroupChatHistory, replayed through the existing message pipeline so
  already-synced messages dedupe themselves. DM topics are rejected because
  Zalo exposes no 1-1 history API.
- /autoreply on|off|status: bridge-level offline auto-reply for DMs. Implemented
  locally (new src/zalo/autoReply.ts) instead of zca-js createAutoReply, which
  is zBusiness-only and fails on personal accounts. State persists to
  data/autoreply.json.

Anti-ban hardening:
- Auto-reply answers DMs only, with a 30-min per-peer cooldown, a global cap of
  12 replies/hour, and a randomized 3-8s human-like delay (never instant).
- Fix startup member-cache warmup that fired every getGroupInfo/getUserInfo call
  at once (setTimeout 0 despite a "2s stagger" comment); it is now serialized
  with a real 2s gap to avoid tripping Zalo's rate limiter (code 221).

Register both commands in the Telegram command menu.
- /history: replay messages by awaiting the live handler one-by-one (new
  replayHistoryMessages) instead of fire-and-forget listener.emit, so ordering
  is preserved even for slow media; skip poll messages so a replayed poll-create
  event can't spawn a duplicate live Telegram poll.
- auto-reply: only answer text DMs (gate on msgType) so stickers/media don't get
  a canned reply; suppress the Zalo self-echo via sentMsgStore so the reply is
  not re-forwarded into the Telegram topic.
- auto-reply rate limiting: track global-cap reservations by unique id so a
  failed send can no longer drop another reply's slot when timestamps collide;
  on send failure keep the per-peer cooldown (don't hammer a failing peer) and
  release only this reservation.
- /autoreply confirmation: derive the cooldown/cap numbers from the exported
  constants instead of a hardcoded "10 phút" that disagreed with the 30-min value.
- startup: stop marking all group topics "loaded" up-front so the lazy per-message
  warmup can win during the 2s-staggered backfill; messages in not-yet-warmed
  groups now resolve member names instead of hitting an empty cache.
@ntdatt812

Copy link
Copy Markdown
Contributor Author

Correctness

  • /history now backfills via a helper that awaits the live message handler
    one-by-one
    instead of fire-and-forget listener.emit, so ordering is
    preserved even when some messages carry slow-to-download media.
  • /history skips poll messages during replay, so replaying an old
    poll-create event can no longer spawn a duplicate live Telegram poll.
  • Auto-reply now answers text DMs only (gated on msgType) —
    stickers/media no longer trigger a canned reply.
  • Auto-reply suppresses its own Zalo self-echo via sentMsgStore
    (consistent with the TG→Zalo send paths), so the bot's reply isn't
    re-forwarded back into the topic.

Anti-ban rate limiting

  • Global hourly-cap reservations are tracked by a unique id instead of a
    timestamp, so a failed send can no longer drop another reply's slot when two
    reservations share the same millisecond.
  • On send failure the per-peer cooldown is kept (no hammering a failing
    peer); only the failed reservation's global slot is released.

Misc

  • /autoreply confirmation text derives the cooldown/cap numbers from the
    exported constants (was a hardcoded "10 phút" that disagreed with the actual
    30-min cooldown).
  • Startup member-cache warmup no longer marks every group "loaded" up-front,
    so the lazy per-message warmup can win during the 2s-staggered backfill and
    messages in not-yet-warmed groups still resolve member names.

Two review notes were intentionally left as-is:
messages so re-flooding is effectively unreachable, and an unbounded cache
would be worse.

  • The sendMessage(groupId, …, { …replyOpts, parse_mode }) boilerplate is a
    repo-wide pattern (~90 call sites); introducing a one-off helper for just the
    two new commands would reduce consistency. A reply() helper is better done
    as a separate repo-wide refactor.

@williamcachamwri

Copy link
Copy Markdown
Owner

I will test it.

@williamcachamwri
williamcachamwri merged commit 530ecb0 into williamcachamwri:main Jun 14, 2026
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.

2 participants