Reputation: 63
I am new to python. I am trying to use the telethon shell to add and remove users from a chat. I use the example code taken from here https://tl.telethon.dev/methods/messages/add_chat_user.html
My code:
(ID and hash I replaced in the text with arbitrary numbers)
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())
But unfortunately for me it doesn't work and gives an error.
(ID and hash I replaced in the text with arbitrary numbers.)
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
Telethon ver. 1.22.0
I would be grateful for any help
Thank you!
Upvotes: 1
Views: 3785
Reputation: 63
The "argument out of range" error I which indicated in the question, occurred because I was trying to specify a 40 byte Chat_id. Whereas for regular chats, the maximum id is 32 bytes.
This happened as you need to specify the so-called "real" id. As an example, the id that Telegram bots receive looks something like this -1001234567891 the real id would be 1234567891.
You can use the following code to get the real 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)
However, if you specify a real id, a ChatIdInvalidError may appear. The problem is that the AddChatUserRequest method can only be used with regular "chats" In my case, it was "supergroup" - is a Channel with the channel.megagroup attribute set to True
For them need use InviteToChannelRequest method, the code that worked was like this:
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())
Then I created a regular "chat" group
For her, the working code was like this:
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())
Upvotes: 3
Reputation: 545
Use:
result = await client(functions.messages.AddChatUserRequest(
chat_id=12345678912,
user_id='username',
fwd_limit=42
))
Instead of:
result = client(functions.messages.AddChatUserRequest(
chat_id=12345678912,
user_id='username',
fwd_limit=42
))
Upvotes: -1