使用 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和hash我在文中用任意数字替换)
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和hash。)
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
如有任何帮助,我将不胜感激
谢谢!
使用:
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
))
我在问题中指出的“参数超出范围”错误发生是因为我试图指定一个 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())
我是 python 的新手。我正在尝试使用 telethon shell 在聊天中添加和删除用户。 我使用从这里 https://tl.telethon.dev/methods/messages/add_chat_user.html
获取的示例代码我的代码:
(ID和hash我在文中用任意数字替换)
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和hash。)
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
如有任何帮助,我将不胜感激
谢谢!
使用:
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
))
我在问题中指出的“参数超出范围”错误发生是因为我试图指定一个 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())