Hi. I am testing a small script that works with telegram api through python module 'Telethon'. Here is the code:
from telethon import TelegramClient, events, sync
import socks
import os
def main():
TELEGRAM_API_ID = os.environ['API_ID']
TELEGRAM_API_HASH = os.environ['API_HASH']
if os.environ.get('PYTHONANYWHERE_DOMAIN'):
pythonanywhere_proxy = (socks.HTTP, 'proxy.server', 3128)
print('Connecting through proxy')
telegram_client = TelegramClient('testing_session', TELEGRAM_API_ID, TELEGRAM_API_HASH,
proxy=pythonanywhere_proxy)
else:
telegram_client = TelegramClient('testing_session', TELEGRAM_API_ID, TELEGRAM_API_HASH)
telegram_client.start(force_sms=True)
if __name__ == '__main__':
main()
When i launch it on my PC it works correct, but when i try to run it on the Pythonanywhere server it fails with an error:
$ python3.8 test.py
Connecting through proxy
Traceback (most recent call last):
File "test.py", line 19, in <module>
main()
File "test.py", line 15, in main
telegram_client.start(force_sms=True)
File "/home/Maxu360/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 132, in start
else self.loop.run_until_complete(coro)
File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete
return future.result()
File "/home/Maxu360/.local/lib/python3.8/site-packages/telethon/client/auth.py", line 139, in _start
await self.connect()
File "/home/Maxu360/.local/lib/python3.8/site-packages/telethon/client/telegrambaseclient.py", line 439, in connect
if not await self._sender.connect(self._connection(
File "/home/Maxu360/.local/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 125, in connect
await self._connect()
File "/home/Maxu360/.local/lib/python3.8/site-packages/telethon/network/mtprotosender.py", line 250, in _connect
raise ConnectionError('Connection to Telegram failed {} time(s)'.format(self._retries))
ConnectionError: Connection to Telegram failed 5 time(s)
I got this error before, and then i found this article: http://help.pythonanywhere.com/pages/403ForbiddenError/ so I connected through proxy, but nothing changed. It is strange because I found this in the whitelist:
...
api.telegram.org
...
core.telegram.org
...
Hope you will help me to solve this problem. Thank you.