NetrunHome

How to run a Telegram bot on a webhook

· 6 min read

Most bots are written like this: the program asks Telegram “is there anything new?” in an endless loop. It is a simple approach, but it requires the process to run all the time. A webhook works the other way round: you tell Telegram an address once, and it knocks on your door itself whenever a user writes something. That is where the usual sticking point appears — the address has to be public and use a secure connection, which means a domain and a certificate.

That is exactly the part Netrun covers in full: you upload the code, we bring the app up and issue a public HTTPS address. All that is left is to tell Telegram that address. As a bonus, a webhook bot behaves like an ordinary website — and websites live on the free plan with no time limit.

  1. Rewrite the bot from polling to receiving requests

    Instead of an endless polling loop, the bot should come up as a small web app and handle incoming requests from Telegram. In Python that is usually Flask, FastAPI or the built-in web mode of aiogram; in Node.js, Express next to Telegraf or grammY. Your reply logic does not change, only the way messages arrive.

  2. Listen on the port from the environment variable

    The app has to listen on the port the platform sets through the PORT environment variable, not one hard-coded in the code. Otherwise the requests simply never arrive. Listen on all addresses rather than only the local one — otherwise the app stays unreachable from outside as well.

  3. Upload the code and get an address

    Upload the project as a ZIP archive or import a repository from GitHub, including a private one. Enter the BotFather token during project setup — it goes into environment variables encrypted. Netrun builds the app, starts it and issues a public HTTPS link: that link becomes your webhook address.

  4. Tell Telegram the address

    What is left is telling Telegram where to send messages. This is done once — with the setWebhook method, either by hand through a link in the browser or from your code on startup. The second option is more convenient: the webhook is then set after every publish and you cannot forget it. It is a good idea to add a secret part to the webhook path so that outsiders cannot pretend to be Telegram.

  5. Try it on the free plan

    A webhook bot is an ordinary web app, so on the free plan it lives with no time limit, unlike a polling bot. There is one nuance: when nobody writes to the bot for a long time, the project falls asleep and wakes up on the first incoming request. The first message after a long pause may be answered with a few seconds of delay; after that the bot replies as usual. If that delay is unacceptable, on the Pro plan the project does not sleep.

A webhook removes the need to keep a process in an endless polling loop, and with it the main argument for renting a server. The public HTTPS address is issued automatically, there is no certificate to configure, and on the free plan such a bot lives with no time limit. Try Netrun.

Common questions

Why is a webhook better than constant polling?

With polling the bot keeps asking Telegram about new messages itself, so the process must run continuously. With a webhook Telegram comes to you only when there is something to deliver. For the bot that is less wasted work, and for you it means you can run the bot like an ordinary website.

Do I need my own domain and certificate for a webhook?

Telegram requires an address with a secure connection, but you do not have to buy a domain or set up a certificate: Netrun issues a public HTTPS link right after publishing. You can attach your own domain later on the Pro plan if you need one.

Will a webhook bot work on the free plan?

Yes, and with no time limit — unlike a polling bot, which shuts down after three days on the free plan. The difference is that a webhook bot behaves like a website: it sleeps when there have been no messages for a while and wakes up on an incoming request. The first message after a long pause may arrive with a small delay.

How do I check that the webhook is set correctly?

Ask Telegram with the getWebhookInfo method: it shows the current address and the last delivery error, if there was one. On top of that, logs are visible in your dashboard in real time, so you can see whether requests reach the app.

Can I switch back to polling?

Yes, it is enough to remove the webhook with deleteWebhook and bring back the previous code. Keep in mind that a polling bot runs for three days on the free plan and then shuts down — such a bot needs the Pro plan to run permanently.

Can I host a Telegram bot?

Yes. Upload the bot code and provide the token from BotFather (Telegram itself gives it to you when you create the bot) — Netrun starts the bot, and no VPS or manual setup is needed. On the free plan the bot runs for 3 days so you can check everything; to keep it running around the clock, switch to the Pro plan.

Why does my website sometimes sleep?

On the free plan, websites sleep after being idle and wake up in a couple of seconds on the first request. On the Pro plan your project runs without sleeping.

Where do I set tokens and other secret values?

In the project settings there is a section for values from your code — for example, the token from BotFather. We store them encrypted.