After you have created a new bot by chatting with BotFather, now you are ready to implement the code for bot. But, before you jump to coding, you need to decide where to host your code.
You have plenty of options available for hosting your code - including web hosting companies, free providers and you can host the code locally without paying for any hosting charges.
Now, before diving into the specifics of each type of hosting, we should look at the way Telegram will interact with your bot (code).
Telegram provides two ways to interact with the bot: Web Hooks and Long Polling.
Web Hooks: You have to have a public URL where your bot will receive commands when anyone interacts with your bot. You also need to have a valid SSL certificate on the public URL (whether self-signed or issued by a trusted CA) for web hooks to work. And also, when web hooks are set, you won't be able to receive messages via polling and vice versa.
Long Polling: This method does not require you to have a public address. And you can get updates using this method from the Telegram server using a simple GET request. You need to know that the updates that you receive using this method will be not older than 24 hours. So, using this, you will need to get updates for your bot at least once every 24 hours.
And both of these methods are exclusive. If you use one of the method, the other will be disabled automatically.
Clearly, web hooks have an advantage over long polling, but it may differ in your case. For example, web hooks allow to reply to the update directly using the output to the execution of script without making a fresh request to the Telegram servers, which is not possible in long polling.
Note that there is another option to host your own Local Bot API Server, but that will be outside of scope of this series, so it won't even be discussed here. You may refer to this more here (Github).
Now, that you know about the methods of getting updates, we can move on to looking for options to host the bot's code.
Web Hosting Companies
There are plenty of web hosting companies that you can use to host your bot code. They usually charge you for a few dollars per year and allow you to run your own website using their web hosting servers. You will need to own a domain name to have a public address. Theses are also fairly available with these hosting providers or other domain vendors like Google Domains, NameCheap etc.
Free Hosting Providers
There are plenty of free hosting providers out there as well where you can host your bot code without paying a dime for hosting (and some even offer a free sub-domain, saving you from that expense as well).
You can however use any company to host your code, but you need to be aware of the limitations for those. For instance, if your bot needs to access other resources from the Internet to function, then you need to see if your hosting provider allows you that access the internet or not. And if your host gives you very less up time, that might also be not favorable for your bot as well. However, there are other companies which are good if you are just starting like 000webhost and Heroku.
You will need to do a few additional settings when choosing these. Such as setting up SSL secured website, work under the limitations of free accounts and a few more. But for initial stage these aptly fit the purpose.
Running Telegram bot without any hosting
Now, if you want to run your Telegram bot without hosting your code anywhere. You can do so by two methods: using your own system as a web server or using the long polling method for getting updates.
The first approach requires you to have a static IP for hosting a website, or use a dynamic DNS provider, or have a tunnel running to forward the requests to your system.
The second approach does not need all the above extra steps but requires you to schedule polling after adequate intervals and act on the received updates simultaneously. It should be noted that along with polling on adequate intervals, you also need to take care of not abusing the Telegram servers by hitting rate limits established for all bots.
This approach also requires you to have a decent internet connection and an always on system for polling in new requests. You also need to keep information about how many requests you have made in past time frame, so that you will not go over your rate limits.
Since, this approach requires a lot to be done on your part besides writing bot's code, it will be advised to go for the second method at the start. And when everything seems right, go for the first approach of using a web hosting provider.
Now, that you have made decision regarding where to host your bot code. It is time to start coding the back end of your bot.
These articles are a part of Telegram bot series and will be using PHP as their coding language. But if you read through the code, you can easily adapt the code and algorithm to be used in any language.