微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Telethon

如何解决使用 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 bot 收到的 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 的 Channel

他们需要使用 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_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,fwd_limit=42
    ))

代替:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。