Example bot from the documentation does not work
MarioVilas opened this issue · comments
Code that causes the issue
#!/usr/bin/python3
from telethon import TelegramClient, events, sync
# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = 0. # obviously I have real values here
api_hash = ''
client = TelegramClient('session_name', api_id, api_hash)
client.start()
print(client.get_me().stringify())
client.send_message('this_is_a_real_username', 'Hello! Talking to you from Telethon')
client.send_file('this_is_a_real_username', '/home/user/caca.jpg')
client.download_profile_photo('me')
messages = client.get_messages('this_is_a_real_username')
messages[0].download_media()
@client.on(events.NewMessage(pattern='(?i)hi|hello'))
async def handler(event):
await event.respond('Hey!')
Expected behavior
The bot should do something instead of crashing.
Actual behavior
The bot crashes.
Traceback
marito@Marios-MacBook-Air cacabot % python3 cacabot.py
Request caused struct.error: 'i' format requires -2147483648 <= number <= 2147483647: InvokeWithLayerRequest(layer=178, query=InitConnectionRequest(api_id=6857619493, device_model='arm64', system_version='23.4.0', app_version='1.35.0', system_lang_code='en', lang_pack='', lang_code='en', query=GetConfigRequest(), proxy=None, params=None))
Traceback (most recent call last):
File "/Users/marito/Work/cacabot/cacabot.py", line 10, in <module>
client.start()
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/client/auth.py", line 128, in start
else self.loop.run_until_complete(coro)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py", line 685, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/client/auth.py", line 135, in _start
await self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/client/telegrambaseclient.py", line 596, in connect
await self._sender.send(functions.InvokeWithLayerRequest(LAYER, req))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/network/mtprotosender.py", line 183, in send
state = RequestState(request)
^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/network/requeststate.py", line 17, in __init__
self.data = bytes(request)
^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/tl/tlobject.py", line 194, in __bytes__
return self._bytes()
^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/tl/functions/__init__.py", line 285, in _bytes
self.query._bytes(),
^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/tl/functions/__init__.py", line 127, in _bytes
struct.pack('<i', self.api_id),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
struct.error: 'i' format requires -2147483648 <= number <= 2147483647
Task was destroyed but it is pending!
task: <Task pending name='Task-4' coro=<MTProtoSender._send_loop() running at /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/network/mtprotosender.py:464> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-5' coro=<MTProtoSender._recv_loop() running at /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/network/mtprotosender.py:507> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-2' coro=<Connection._send_loop() running at /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/network/connection/connection.py:316> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-3' coro=<Connection._recv_loop() running at /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/telethon/network/connection/connection.py:335> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Exception ignored in: <coroutine object Connection._recv_loop at 0x1034efae0>
RuntimeError: coroutine ignored GeneratorExit
marito@Marios-MacBook-Air cacabot %
Telethon version
1.35.0
Python version
3.12.2
Operating system (including distribution name and version)
Mac OS X Sonoma 14.4.1
Other details
It's a desktop. The network is fine.
Checklist
- The error is in the library's code, and not in my own.
- I have searched for this issue before posting it and there isn't an open duplicate.
- I ran
pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip
and triggered the bug in the latest version.
I should also add I'm running this from an Apple M1. This may be relevant since the bug references struct.pack, and I'm running on ARM rather than Intel.
I'm trying to debug this but got stuck pretty quickly - the code seems to be using struct.pack correctly and the architecture should not matter, so all I can think of is some change in the Telegram protocol that this library can't quite parse... :(
Same problem and solution as #4366 (comment).