繁体   English   中英

使用 Telethon 添加到电报聊天时的问题

[英]The problem when adding to a telegram chat using Telethon

我是python的新手。 我正在尝试使用 Telethon shell 在聊天中添加和删除用户。 我使用从这里获取的示例代码https://tl.telethon.dev/methods/messages/add_chat_user.html

我的代码:

(我在文本中用任意数字替换了 ID 和哈希)

from telethon.sync import TelegramClient
from telethon import functions, types
from telethon.errors import ChatIdInvalidError
from telethon.errors import PeerIdInvalidError
from telethon.errors import UserNotParticipantError
from telethon.tl.functions.messages import DeleteChatUserRequest
from telethon.tl.functions.messages import AddChatUserRequest


api_id = 1234567
api_hash = 'blablablabla123456'
phone = '+123456789'
name = 'testname'

with TelegramClient(name, api_id, api_hash) as client:
    result = client(functions.messages.AddChatUserRequest(
        chat_id=12345678912,
        user_id='username',
        fwd_limit=42
    ))
    print(result.stringify())

但不幸的是,对我来说它不起作用并出现错误。

(我在文本中用任意数字替换了 ID 和哈希。)

    Request caused struct.error: argument out of range: AddChatUserRequest(chat_id=123456789, user_id=InputUser(user_id=123456789, access_hash=-123456789789), fwd_limit=42)
Traceback (most recent call last):
  File "d:\python\AddUser\new\from telethon.tl.functions.messages impo.py", line 18, in <module>
    result = client(functions.messages.AddChatUserRequest(
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\users.py", line 30, in __call__      
    return await self._call(self._sender, request, ordered=ordered)
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\users.py", line 58, in _call
    future = sender.send(request, ordered=ordered)
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\network\mtprotosender.py", line 176, in send
    state = RequestState(request)
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\network\requeststate.py", line 17, in __init__
    self.data = bytes(request)
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\tl\tlobject.py", line 194, in __bytes__
    return self._bytes()
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\tl\functions\messages.py", line 139, in _bytes
    struct.pack('<i', self.chat_id),
struct.error: argument out of range

电视版。 1.22.0

我将不胜感激任何帮助

谢谢!

我在问题中指出的“参数超出范围”错误是因为我试图指定一个 40 字节的 Chat_id。 而对于常规聊天,最大 id 为 32 字节。

这是因为您需要指定所谓的“真实”ID。 例如,Telegram 机器人收到的 id 看起来像这样 -1001234567891,真实的 id 是 1234567891。

您可以使用以下代码获取真实ID:

from telethon import utils
real_id, peer_type = utils.resolve_id(-1001234567891)

print(real_id)  # 1234567891
print(peer_type)  # <class 'telethon.tl.types.PeerChannel'>

peer = peer_type(real_id)
print(peer)  # PeerChannel(channel_id=1234567891)

但是,如果您指定真实的 id,则可能会出现 ChatIdInvalidError。 问题是 AddChatUserRequest 方法只能与常规“聊天”一起使用在我的情况下,它是“超级组” - 是一个 Channel.megagroup 属性设置为 True 的频道

对于他们需要使用 InviteToChannelRequest 方法,工作的代码是这样的:

from telethon.sync import TelegramClient
from telethon import functions, types
    
api_id = 123456
api_hash = '******'
name = 'name'
    
with TelegramClient(name, api_id, api_hash) as client:
    result = client(functions.channels.InviteToChannelRequest(
            channel='channelname',
            users = ['username']
        ))
    print(result.stringify())

然后我创建了一个常规的“聊天”组

对她来说,工作代码是这样的:

from telethon.sync import TelegramClient
from telethon import functions, types
    
api_id = 123456
api_hash = '*******'
name = 'name'
      
with TelegramClient(name, api_id, api_hash) as client:
   result = client(functions.messages.AddChatUserRequest(
        chat_id=chatid,
        user_id='username',
        fwd_limit=42
    ))
    print(result.stringify())

采用:

result = await client(functions.messages.AddChatUserRequest(
        chat_id=12345678912,
        user_id='username',
        fwd_limit=42
    ))

代替:

result =  client(functions.messages.AddChatUserRequest(
        chat_id=12345678912,
        user_id='username',
        fwd_limit=42
    ))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM