Ask coding questions

← Back to all posts
Launch Python Telegram bot in background
h
artm04

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?

Answered by Scoder12 [earned 5 cycles]
View Answer
Voters
artm04
Comments
hotnewtop
Scoder12

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.

artm04

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")

Scoder12

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

artm04

@Scoder12 Thanks a lot, it helped!

GarvitJoshi

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

Scoder12

@GarvitJoshi async or sync?

GarvitJoshi

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

Scoder12

@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.

amirhosseinka

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

Scoder12
SushiPython

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

@SushiPython Thanks a lot, it helped!

MehmetKarahanl

@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?

Kazy6ek04

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