Telegram does not send authorization code if the request was sent from telethon code hosted on Heroku

1.3k Views Asked by At

I have a problem when using the Telethon Python client for Telegram for logging in.

The script below works perfectly fine when I run it on my own laptop: it prints a SentCode object, Telegram sends me a confirmation code, and I use it to log in.

When I run exactly the same script on Heroku, it still prints the same SentCode object, however, I don't receive any confirmation code from Telegram and cannot continue logging in.

from telethon import sync  # noqa
from telethon import TelegramClient
from telethon.sessions import StringSession
client = TelegramClient(session=StringSession(), api_hash=API_HASH, api_id=API_ID)
client.connect()
result = client.send_code_request(phone=PHONE)
print(result)
# prints SentCode(type=SentCodeTypeApp(length=5), phone_code_hash=..., next_type=CodeTypeSms(), timeout=None)
... the code for logging in and saving the session...

It seems that Telegram doesn't acknowledge authorization from Heroku for security reasons - maybe, Heroku is often used to host malicious bots, and Telegram fights them this way. However, neither Telethon nor MTProto documentation reflects this restriction. So I wonder, how is it even possible to use MTProto clients like Telethon from a cloud platform? And if it is possible, what should I do to get around this restriction?

0

There are 0 best solutions below