I am trying to deploy this Telegram bot* on Heroku: https://github.com/radinshayanfar/TGCopyBot. The bot first needs to sign in to my account, which requires a login code that’s sent to my devices on which I’m already logged in. When running the bot locally from a terminal, it requests input from the user, Enter code:
, upon which I enter the login code in the terminal and press Enter, and the bot proceeds to do its work. But after deploying the bot on Heroku and running the dyno, it fails and this shows in the logs:
2021-06-26T12:55:33.312544+00:00 app[worker.1]: Enter code:Traceback (most recent call last):
2021-06-26T12:55:33.312546+00:00 app[worker.1]: File "/app/app/main.py", line 77, in <module>
2021-06-26T12:55:33.312729+00:00 app[worker.1]: tg.login()
2021-06-26T12:55:33.312730+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/telegram/client.py", line 493, in login
2021-06-26T12:55:33.312946+00:00 app[worker.1]: result = actions[authorization_state]()
2021-06-26T12:55:33.312948+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.9/site-packages/telegram/client.py", line 585, in _send_telegram_code
2021-06-26T12:55:33.313163+00:00 app[worker.1]: code = input('Enter code:')
2021-06-26T12:55:33.313168+00:00 app[worker.1]: EOFError: EOF when reading a line
2021-06-26T12:55:33.373308+00:00 heroku[worker.1]: Process exited with status 1
2021-06-26T12:55:33.427790+00:00 heroku[worker.1]: State changed from up to crashed
It may then automatically start the bot again, and proceed to crash and output the same errors as above. What’s the problem here, and how can it be fixed?
If the issue is its request for the login code, then I do receive the login code on my other devices, but how do I give the login code to the bot? More generally, how do you repsond to a request for user input from any bot deployed on Heroku?
*not really a Telegram bot made with @BotFather; it just uses the Telegram API development tools
2
Answers
It seems the issue is with the request for user input, and that there is no direct way for the user to input anything on Heroku as they would on a terminal. The code can be modified to accept what would be inputted as an environment variable in this case, by using e.g. a session string: https://docs.pyrogram.org/topics/storage-engines#session-strings, https://docs.telethon.dev/en/latest/concepts/sessions.html#string-sessions
On your local environment, the built-in
input
works fine because you have access to the Terminal and can provide the input.On Heroku there is no such an option so the command returns the EOFError exception because the function hits the end-of-file condition (EOF) without reading anything.
A possible solution might be to request the user to enter the
login code
via the Telegram Bot, for example as first step after the user starts chatting.