Ask coding questions

← Back to all posts
Launch Python Telegram bot in background

I want to run a Telegram bot written in Python (with the pyTelegramBotAPI library) on in the background, that is, to keep it running even when I close the browser. Is this possible? If so, how to do it?

Answered by Scoder12 [earned 5 cycles]
View Answer

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 using the command "python3")


@artm04 It will run in the background, even with the tab closed, but you NEED an http server for this to work.


@Scoder12 Thanks a lot, it helped!


@Scoder12 Can you please explain in detail. I have a Private repl.


@GarvitJoshi async or sync?


@Scoder12 It uses python-telegram-bot. I have not used asyncio.


@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 at the bottom. Then in your, import your app (if filename is do from keepalive import app), and import threading. Finally, above where you run your bot, do threading.Thread(target=lambda:'')).start() which should open a server in the background. Let me know if you have any questions.


@Scoder12 hi
what about asyncio bots ?
how should i use aiohttp ?


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.


@SushiPython Thanks a lot, it helped!


@artm04 Hello. I have a telegram bot too. I made with pyTelegramBotAPI module and I used, uptimerobot like you. But my bot has a problem. Actually I don't know what's the problem but my 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