GooDeeJAY
GooDeeJAY

Reputation: 1820

Using "chats" argument in telethon.events

I'm running this:

from telethon import TelegramClient, events
from telethon.tl.types import PeerChat, PeerChannel

@client.on(events.ChatAction(chats=[PeerChannel(-1001244988104)]))
async def handle_chat_action(event):
    print(event)
    print(event.user_left)

@client.on(events.NewMessage(chats=[PeerChat(-100123456789)]))
async def my_event_handler(event):
    print(event.raw_text)

client.start()
client.run_until_disconnected()

But, not getting updates, why? What's wrong here?

At first, I tried chats=[-100123456] (without PeerChat), but did not get updates...

Upvotes: 2

Views: 6128

Answers (1)

Lonami
Lonami

Reputation: 7076

You should not mix the concept of "marked IDs" and Peer instances together. You can use:

@client.on(events.NewMessage(chats=[PeerChannel(123456789)]))
async def my_event_handler(event):
    print(event.raw_text)

Or:

@client.on(events.NewMessage(chats=[-100123456789]))
async def my_event_handler(event):
    print(event.raw_text)

But not both.

  • The -100 prefix tells Telethon it's a Channel (megagroups, also known as supergroups, are also Channel).
  • The - prefix tells Telethon it's a Chat.
  • The + prefix tells Telethon it's a User.

This follows the same convention as the Telegram HTTP Bot API.

If you don't use a prefix, Telethon will look in cache for all types. If it's not in cache, it will attempt to use a User.

If you use a prefix, Telethon will only get that type from cache. If it's not in cache, it will attempt to use the type your prefix says.

Internally,

-100123 == PeerChannel(123)
-123 == PeerChat(123)
123 == PeerUser(123)

event.chat_id, event.user_id, client.get_peer_id all return prefixed IDs because it's helpful for the library and convenient for the user.

Updates are the "raw" objects from Telegram and those are the original, real values.

Upvotes: 15

Related Questions