0

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?

David Dale
  • 10,958
  • 44
  • 73
  • Generate the session in your local machine, and move it to Heroku (or any other service) later on. [`StringSession`](https://docs.telethon.dev/en/latest/concepts/sessions.html#string-sessions) was designed with this use-case in mind. – Lonami Jan 24 '21 at 09:40
  • @Lonami this way it indeed works. But I am building an application where multiple users connect to Telegram as they want, so I as a developer won't be able to authorize them all in advance. Maybe you know a good framework allowing to perform authorization on the client side (e.g. by running some JS on a webpage), and then send the generated session to the server? – David Dale Jan 24 '21 at 11:19
  • I don't know any frameworks, but you can write your own `Session` storage to store it as you see fit. Docs mention a few alternative implementations too. – Lonami Jan 27 '21 at 13:39

0 Answers0