Skip to content

Fix webhook filters: evaluate on a built Telethon event and resolve filters.chats - #44

Merged
erfnzdeh merged 2 commits into
mainfrom
fix/webhook-filters
Oct 3, 2026
Merged

erfnzdeh merged 2 commits into
mainfrom
fix/webhook-filters

Conversation

@erfnzdeh

@erfnzdeh erfnzdeh commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #43 (fix/daemon-jobs). Merge that one first; GitHub then retargets this PR to main.

Both halves of [webhook.filters] were dead.

What was wrong

  1. filters.chats dropped everything. should_push compared each event's chat_id against _resolved_chat_ids, but set_resolved_chats had no caller, so the set was always empty. Configuring any chats list dropped every event that had a chat.
  2. The other filter keys read the wrong object. [webhook.filters] keys such as chat_type and contains were evaluated against the raw TL update the bus carries. Every filter reads a high-level Telethon event (event.chat_id, is_private, message.sender_id), so they raised (the bus logged "event handler failed") or never matched. This is the same shape bug Fix gateway jobs: deliver incoming updates, build Telethon events, resolve @chat refs, start jobs at boot #43 fixed for gateway jobs.

Changes

  • gateway/tlevents.py (new, no behaviour change): the builder tables and build_event() move out of the Gateway class so both consumers share them. The module adds a table keyed by bus event type: message_new, message_edited, message_deleted, message_service, member_*, read_inbox / read_outbox (with the matching inbox=) and user_status.
  • daemon/webhook.py:
    • Numeric chats are read at start. @name entries are resolved through the event's account and retried at most once a minute if that fails, logged once.
    • Filter keys run against the Telethon event built from the raw update, in both directions, so outgoing messages are still reported. is_incoming narrows that if needed.
    • A type Telethon does not model (typing, for one) is held back while filter keys are set, and logged once per type.
    • Self-origin events still skip the filter keys, as before, because they have no raw update.
  • daemon/app.py: the pusher gets Daemon.get_client to look up an account's client and resolver.
  • webhook.example.toml and the CHANGELOG note the filter keys.

Tests

  • tests/test_webhook_filters.py: chat_type, contains, both directions and is_incoming, a type with no builder (logged once), self-origin, an offline account, @name resolution (resolved once), numeric chats without an account, and retry after a failed resolve. All use real TL updates.
  • tests/test_webhook.py: an end-to-end test from a Telethon update fed through the account's client, via the bus and filters, to a POST to a local receiver. It fails on the old code.

make lint typecheck test docs parity: all pass, 13335 tests.

…ents.py

No behaviour change. The webhook pusher needs the same translation for its
filters, so the builder tables and build_event() move out of the Gateway
class into a module both can use. The table also gains the v2 bus types,
which is what the webhook is keyed by.
…rs.chats

Both halves of [webhook.filters] were dead:

- filters.chats was compared against _resolved_chat_ids, which nothing ever
  filled (set_resolved_chats had no caller), so configuring it dropped every
  event that had a chat. Numeric refs are now read at start, and '@name' refs
  are resolved through the event's account, retried once a minute on failure.
- The other keys were evaluated against the raw TL update the bus carries,
  while every filter reads a high-level Telethon event, so they raised (the
  bus logged 'event handler failed') or never matched. The pusher now builds
  the event with gateway/tlevents.py, keyed by bus type, in both directions.
  A type Telethon does not model is held back while filters are set, logged
  once per type.

The daemon hands the pusher its get_client lookup for both. A self-origin
event still skips the filter keys, as before: it has no raw update.
@erfnzdeh
erfnzdeh changed the base branch from fix/daemon-jobs to main October 3, 2026 16:29
@erfnzdeh
erfnzdeh merged commit 87de859 into main Oct 3, 2026
13 checks passed
@erfnzdeh
erfnzdeh deleted the fix/webhook-filters branch October 3, 2026 16:32
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