Launch Python Telegram bot in background
I want to run a Telegram bot written in Python (with the pyTelegramBotAPI library) on repl.it in the background, that is, to keep it running even when I close the browser. Is this possible? If so, how to do it?
Yes, this is possible. Just make sure you have a web server running at the same time as your bot. If you're using asyncio, use
aiohttp for this, otherwise, use
flask. Just make a simple "Hello world" webserver, and then make an account on Uptime Robot and put a monitor on your repl URL. It should stay up.
I need Telegram bot to work in the background, not an http server. Or I misunderstood you.
(A small clarification - I run the bot in my private Repl on repl.it using the command "python3 main.py")
@GarvitJoshi make a second python file with a flask app in it (if you don't know how, open new python repl, click examples, and press flask, then copy that code). Make sure to not add
app.run() at the bottom. Then in your
main.py, import your app (if filename is keepalive.py do
from keepalive import app), and
import threading. Finally, above where you run your bot, do
threading.Thread(target=lambda: app.run(host='0.0.0.0')).start() which should open a server in the background. Let me know if you have any questions.
Hey artm, you don’t need the browser open to run repls. You can use something called UptimeRobot and set it to ping your repl at thirty minute intervals. Make sure you have a web server that you ping (.co tld, not .run). For more info on this, I believe H has a guide that you can look to.
@artm04 Hello. I have a telegram bot too. I made with pyTelegramBotAPI module and I used repl.it, uptimerobot like you. But my bot has a problem. Actually I don't know what's the problem but my repl.it code always fails. At first, it works but a few minutes later it fails* Do you have any idea?
I don't think that's possible. The only way to get this working is to copy and paste your code to GitHub and use Heroku to connect it to your Github which has your code, select your bot from GitHub and from Heroku you must deploy the repository. I believe you have 2-3 month free hours then they ask to fill in your card info and subscription, but it is still better than those websites which say try it free and then they immediately ask your card info, Heroku doesn't do that, also maybe you can make Heroku work forever and free by making new.... you know what I mean ;)
To sum up, GitHub is like a Core and Heroku is like a cable connectors that connect Core's energy to the city which keeps city alive, something like that