Telethon: 在Telethon中,群组和频道具有相同的实体类型。

huangapple go评论60阅读模式
英文:

Telethon: Same entity type for a group and channel in telethon

问题

我有一堆句柄(@xyz),我试图找出该句柄属于用户、组还是频道。为了检查它,我正在尝试以下代码:

from telethon.sync import TelegramClient
from telethon.tl.types import Channel, Chat, User

async def check_handle(client,handle):
    username = handle
    try:
        entity = await client.get_entity(username)
        if entity:
            if isinstance(entity, User):  # 检查是否为用户(私聊)
                print(f"{username} 是一个私聊。")
            elif isinstance(entity, Channel):  # 检查是否为频道
                print(f"{username} 是一个频道。")
            elif isinstance(entity, Chat):  # 检查是否为聊天组
                print(f"{username} 是一个群组。")
            else:
                print(f"无法确定 {username} 的类型。")
    except ValueError as e:
        print(f"错误:{e}")

await check_handle(client,'@aim4pg') # 调用该函数。

上述代码返回 - "@aim4pg 是一个频道",但我可以清楚地在Telegram中搜索到所提到的句柄是一个具有X个成员的群组。

我有点知道为什么会发生这种情况 - 频道和群组都返回Channel实体(我尝试过一个频道和一个群组进行实验)。如果是用户,它返回User实体。

需要帮助:如何在Telethon中确定它是群组、频道还是用户?

英文:

I have a bunch of handles(@xyz) and I am trying to find if that handle belongs to a user, a group or a channel. To check it, I am trying with the below code:

from telethon.sync import TelegramClient
from telethon.tl.types import Channel, Chat, User

async def check_handle(client,handle):
    username = handle
    try:
        entity = await client.get_entity(username)
        if entity:
            if isinstance(entity, User):  # Check if it's a User (private chat)
                print(f"{username} is a private chat.")
            elif isinstance(entity, Channel):  # Check if it's a Channel
                print(f"{username} is a channel.")
            elif isinstance(entity, Chat):  # Check if it's a Chat (group)
                print(f"{username} is a group.")
            else:
                print(f"Unable to determine the type of {username}.")
    except ValueError as e:
        print(f"Error: {e}")

    

........
........

await check_handle(client,'@aim4pg') # Call to the function.

The above code returns - "@aim4pg is a channel.", but I can clearly see on searching telegram that the mentioned handle is a group with X numbers of members in it.

I kind of know why it is happening - Both channel as well as group return Channel entity(I tried to experiment with one channel and one group). And if it is a user, it returns User entity.

Help needed: How to determine if it is a group or channel or user in Telethon?

答案1

得分: 0

"Megagroups" 是 Channels,其中 megagroup 设置为 True:https://tl.telethon.dev/constructors/channel.html

英文:

Megagroups are Channels with megagroup set to True: https://tl.telethon.dev/constructors/channel.html

答案2

得分: 0

在Telegram中,只有三种聊天类型:

  • 用户(可以设置.bot标志)
  • 频道(只能设置.megagroup或.broadcast标志中的一个)
  • 聊天(小群组,它们不能设置用户名)

通过get_entity函数返回的内容不会有其他类型。

通常情况下,非小群组在API中称为“megagroup”,它们的类型也是Channel,并具有类似的行为。频道被称为广播。

要区分它们:

if isinstance(entity, Channel)
    if entity.megagroup: # (也就是说,如果不是entity.broadcast)
        # 这是一个超级群组
    else:
        # 这是一个广播频道
英文:

There are only three chat types in Telegram:

  • User (can have .bot flag set)
  • Channel (can have only one of the .megagroup or .broadcast flags set)
  • Chat (small groups, they can have no username)

There can't be any other thing returned by get_entity.

The usual non-small groups are called "megagroup" in the api, they're also Channel in type and have similar behavior. channel is called broadcast.

to differentiate them:

if isinstance(entity, Channel)
    if entity.megagroup: # (aka, if not entity.broadcast
        # it's a supergroup
    else:
        # it's a broadcast channel

huangapple
  • 本文由 发表于 2023年7月14日 07:29:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定