Until recently, WhatsApp was undoubtedly the most used instant messaging application in the world. After a series of technical problems, however, more and more users have decided to migrate to Telegram. And if you are reading this article, most likely you are also part of this group. To unite the two applications are only the basic functions, which allow you to chat with other people by sending multimedia content, text or audio messages.
From the point of view of advanced features, however, Telegram has grown a lot in recent years, adding a series of unique features, which have given it "an edge" compared to WhatsApp. One of these is represented by bots, or by applications integrated in the chat that allow you to perform various automatic functions via API. In the guide that follows, in fact, I'll tell you about how to create a Telegram bot, so as to help you develop an app of your own, which can also be used by other users. Not being a simple tutorial, however, I tell you right away that you will have to dedicate a little more time than usual.
Index
- What is a Telegram bot
- What can be done with a Telegram bot
- How do you interact with a Telegram bot
- How to control your Telegram bot
- Creating a Telegram bot with BotFather
- Specific insights and tutorials
What is a Telegram bot
You are trying to understand cos'è a Telegram bot? No fear! Even the first few times I used this application I had no idea how bots worked. In reality, the mechanism that regulates them is simpler than you think and easily understood.
A Telegram bot is nothing more than a user of the public type messaging system, which allows you to carry out some activities automatically. Behind this user, however, there is an application, which automatically performs certain functions for which it was developed. Obviously, these are not too complicated operations, but small services that can greatly simplify daily life.
Typology on bot
I tipi on Telegram bot currently available are two:
- Bot Telegram stand-alone
- Bot Telegram inline
Type bots stand-alone they are users through whom it is possible to interact on single and independent chats. By entering certain commands, you can perform various automated operations.
Type bots inlineinstead, they are apps that are usually integrated into groups. They allow you to launch automated operations in multiple chat. These functions, of course, are decided by the group administrators.
Each Telegram bot, however, is identified by a Token. This is a unique identification code, which is used by APIs and applications to retrieve and access the service. Below, I'll explain in more detail what it is.
As regards the commands to insert, simply read the menu integrated into the chat, which acts as a legend. It contains most of the commands supported by the bot.
What can be done with a Telegram bot
Le outdoor activities that can be done with a Telegram bot are many and I'm sure you don't know many of them yet.
The functions offered are so many. They range from news services to sending files through external services, without forgetting the possibility of keeping Amazon prices under control. Then there are bots that allow you to play in the chat and also those that allow you to receive payments from users in exchange for goods and services.
In large cities, such as Rome, Milan or Turin, there is no shortage of Telegram bots for public transport timetables. Using them, in most cases, just enter the number of the stop to which you are closest to receive the arrival times of buses and trams in real time.
Do not forget that there are all types of Telegram bots when it comes to searching for multimedia content. Through these applications it will be possible to search for YouTube videos, GIFs or simple images online. Not only that: you can also stream movies and music.
Listing all the Telegram bots that I find useful in this paragraph would be somewhat difficult. However, if you find this topic interesting, I suggest you take a look at my guide on the best Telegram bots. Also, I suggest you read the introduction to bots written by the Telegram team.
How do you interact with a Telegram bot
If you have never used a Telegram bot, you should know that, unlike its creation, it is a very simple operation within everyone's reach.
In some cases, in fact, the belief has spread that in order to use these services it is necessary to know complex languages: nothing could be more false. Telegram bots were born precisely to simplify the approach of users, making their use suitable for everyone.
use a Telegram bot, first you need to find and launch it within the messaging application. After that, in the first message received, you will be shown gods commands (often in the form of buttons). Thanks to them, it is possible to perform the various operations supported.
What you can see in the example image is a bot that takes care of tracking shipments and is called TrackBot. Its function is to check the status of the shipment in real time, avoiding navigation on the couriers' websites. As you can see, its command legend is easily accessible and very clear.
How to control your Telegram bot
And if you are intent on creating a Telegram bot, how can you do for check it? In this case, the path is a little more complicated, although there are alternatives that allow you to simplify the operation.
The first three things you will need will be (in order):
- A name that identifies the Telegram bot
- The link to the Telegram API
- It Token of your bot Telegram
If choosing the name is really simple, we cannot say the same for the API and for the Tokens. If you have never approached this world, in fact, you will surely need a brief focus on the subject.
API bot Telegram
Le API they are not exclusive to Telegram, but have been used for some time in the world of programming. This acronym stands for Application Programming Interface. These are therefore standard features and procedures that can be used by third parties to interact with apps and services.
In this case, the two parties "called into question" will be Telegram, which makes its platform available, and the muzzle that you are going to create. To make these two elements communicate with each other, you will need to use the There is an API about Telegram.
Token
Un Token, in computer language, is nothing more than a set of characters and numbers that contains a certain amount of information.
As for Telegram, the use of the Token is linked to theaccess to the bot that you are going to create. In this way, in fact, an encrypted connection is created with a unique access key, the Token, in order not to have to enter a password every time. More information on bot authentication can be found on this Telegram support page.
Creating a Telegram bot with BotFather
Especially if you are a newbie on this topic, the first step to take create a Telegram bot is to become familiar with BotFather. This is an official bot that allows you to create other Telegram bots, thus managing to simplify, by far, the interface with the API.
To proceed, first go to this address and grant permission to start the bot on Telegram. To do this, just press the button Open Telegram, just as shown in the screen shot.
Now, you will be brought back inside the application, where BotFather it will open as if it were a normal chat. To start configuring your Telegram bot, press the button starts che trovi in basso.
You will be returned a series of commands, which will allow you to use all the functions of BotFather. To create your first bot, tap / newbot, which should be the first option available.
Now, choose the name of your Telegram bot. I recommend that you opt for an easily memorable solution that has an impact on the users you intend to engage. Once written in the space reserved for messages, tap on Send.
The next step involves choosing a username per il bot, which will also be the shortcut with which it can be called up by users. It does not need to be identical to the display name. In this case, in fact, I decided to use the nickname wordsm_bot.
If the username is available, a confirmation message. In it, the token, essential for accessing HTTP-based Telegram APIs. If the username has already been chosen by other users, BotFather will offer you a similar one.
To complete the Telegram bot pre-configuration, I also recommend that you use these three short ones commands, which you can type in chat with BotFather:
- / setdescription - allows you to change the description of the bot
- / setabouttext - allows you to select the info to show in the bot
- / setuserpic - gives the possibility to choose the profile picture of your bot
But that's not all: as I told you, these are only the initial procedures for creating a Telegram bot. To make it simpler, by doing so you will create a bot user with no functionality, i.e. an empty chat without automation.
Therefore, you will now need to program the options your bot will need to support. Next, you will need to associate them with the bot via the Telegram API. Read on to find out how.
Requirements
If using BotFather to generate the “façade of the service” you want to offer users is really a breeze, the same cannot be said for its concrete implementation. In fact, to create a Telegram bot that works in the right way, as I told you in the introduction, it is necessary to have programming basics.
Telegram in fact incorporates both the language Java than that Python, two programming languages luckily for you not too complicated (at least in the basics). Don't forget though that, in addition to these programming languages, you can also choose Basic, C, C+, COBOL, FORTRAN, To give, Pascal and others.
Furthermore, to insert the development of the commands you want to “hook” to your Telegram bot, you will need a virtual hosting service. For this topic, I recommend that you take a look at my guide dedicated to hosting a blog, where you can find the best alternatives currently available.
If you have no big claims or just want to test your skills, you can also choose to use Hook.io. It is a website that gives the possibility to create small applications with access limits set at 1000 requests per month. This service costs 2,00 $ a month for the version basis. Initially, however, you should be able to take advantage of the 60 day free trial. To register, just go to the official page and create a new account or log in with the GitHub profile.
If you have no knowledge in app and software programming, I suggest you go straight to this point of the tutorial. You will find more information about the courses to follow and any alternatives to bots.
Telegram bot API manuals
As you will have understood by now, the There is an API about Telegram they are necessary for any action you want to take with the service you have created or want to create.
Usually, these commands are divided into different categories, which allow you to execute different ones operations. The most important are the following:
- Authorize the bot By creating a Token
- Make requests via HTTPS
- Make requests while receiving an update
- Receive updates
- Data available and represented as JSON objects
- User
- Chat
- Messages
- Photo size
- Audio
- Documents
- Video
- Animations
- Voice notes
- Note video
- Contact us
- Location
- Polls
- Fillet
- Login
- Chat photo
- Chat permissions
- Message update
- Stickers
- Inline mode
List all the Telegram bot API manuals in this paragraph I assure you that it would be impossible, due to the hundreds of commands inside it. Luckily for you, all of this list is wrapped up in this Telegram Advanced FAQ page.
Telegram bot updates
Before we show you in detail how to create a Telegram bot, you need to know that, if in the future you have to update the bot, the instant messaging service offers you this possibility without any problems.
The update methods made available are:
- setWebhooks
- getUpdates
The main difference is that the first method is of type pull, while the second is of type push and uses so-called long polling. In both cases, however, the output will always be an update in JSON. This result will be kept on the server for 24 hours.
I suggest you proceed with reading the insights to understand more in detail what webhooks consist of.
webhook
Unlike the push method, the procedure using webhook it allows to obtain numerous advantages, among which the possibility of saving CPU cycles and increasing the response speed.
To use this method however, the bot's background server will need to meet the following requirements:
- Support IPv4
- Accept incoming calls from subnets 149.154.160.0/20 and 91.108.4.0/22 through ports 443, 80, 88 or 8443
- Ability to use TLS1.0 + HTTPS traffic
- Support verified certificates
- Use a CN or SAN that supports the domain you chose during setup
- Provide all certificates that make up a verification chain
Since Telegram bot updates are a rather complex topic, I decided to rely, at least for this time, on the Telegram FAQs related to webhooks. I remind you, however, that the subnet mentioned above can be used by July 2019, but they may vary over time. I will try to keep the information up to date. However, to be sure not to make a mistake, also check the in-depth page.
Lastly, you may find it useful to check the Telegram bot API changelog. You can find it at this address of the Telegram site.
Practical example of a Telegram bot with BotFather
Now that you understand how bots work in more detail, it's time to look into the specifics how to create a Telegram bot. I'll give you a small example using the platform Hook.io, already mentioned a few paragraphs above. In this case, the Telegram bot will not be anything complicated, it will only serve a repeat what you write in the chat via the keyboard of your smartphone, tablet or PC.
The first step I recommend you take is to memorize the token Telegram you received from BotFather (as I showed you in the previous point). You have to paste it in the appropriate space of Hook.io by pressing on the three levers present in the central part of the screen, in the field marked by the item Value. In the field Keyinstead, you will have to enter the recognition key, which in my case is token_epictrick.
Now, still on the main page, select the item Create new service, which you find in the top bar. Now enter the name of your service and press the blue button named Save and Continue.
We have arrived at the crucial stage of the guide, where you will have to enter the commands to use the Telegram bot you are creating. First, regarding the option Language, select the language from the list Python (for the example I chose this programming language, but depending on your skills, you can use the one you think is most suitable).
Now, delete everything you find in the box for command input and paste the following lines of code:
import requests messaggio-wsm=Hook['params']['message']['text'] ID-conversazione-wsm=Hook['params']['message']['chat']['id'] URL='https://api.telegram.org/bot' + Hook['env']['token_epictrick'] + '/sendMessage' req=requests.get(URL,verify=False,data={'chat_id':ID-conversazione-wsm,'text':messaggio-wsm})
Don't forget to replace the term token_epictrick (highlighted in green) with the alphanumeric string that you received from BotFatherotherwise your Telegram bot will fail to communicate properly with the platform.
If you are not familiar with the Python language, or if you are approaching this world for the first time, let me explain briefly what the commands mean I just entered:
- import requests - inserts the Python library you want to use into your service, so that you can exploit it properly
- message-wsm - copy the text you enter into the Telegram bot from the keyboard
- ID-conversation-wsm - uniquely identifies the Telegram chat to resend the message to the correct one
- URL - used to send the HTTP request from the bot to the service you created on Hook.io
- req - this command creates the reply to be sent on Telegram and sends it via chat
Specific insights and tutorials
In this section, I'm going to include a few insights particularly interesting ones that I came across. They answer the questions most users usually ask about the programming a Telegram bot.
These are external guides, in English language, but I'm sure you won't have any particular problems understanding them.
Telegram bot creation without programming
Create a Telegram bot without programming it is a far from simple undertaking. As I showed you earlier, in fact, when you want to insert a working service inside, you will necessarily have to insert lines of code with the correct syntax.
In this case, then, at least for the preliminary part, you could use BotFather to build the foundation of your Telegram bot. If you went to see among the available commands, in fact, you would see that there are also those to insert a list of pre-configured commands. The user experience, however, would be very limited.
Alternatively, which is very interesting, you might think about using Udemy. This web platform integrates thousands of online courses for the most diverse topics. Obviously, there are also those to create a Telegram bot. And don't be frightened by the prices: often, Udemy offers really tempting offers, with the most expensive courses discounted up to 90%.
As another choice, you could create a Telegram channel instead of a bot. This would give you your own broadcast chat, which users could subscribe to.
Telegram bot creation with Laravel
Do you know what is it Laravel? This is a novelty that has caught the interest of programmers and that brings with it a series of very promising features. More precisely, Laravel is a PHP framework born in 2011, oriented towards object-oriented programming. Laravel's strengths are undoubtedly its simplicity and the ease with which code maintenance can be performed.
Waiting for an in-depth study on this PHP framework, I found a really interesting guide online, which illustrates just how to create a Telegram bot with Laravel. The example describes the process of launching a Telegram bot that fetches cryptocurrency data in real time from global markets. You can find it at this link and I highly recommend reading it.
Telegram bot creation with Python
Python is a high-level, object-oriented and widely used programming language. Born in the nineties, it owes its name to Monty Python, a television series that made history, especially in the United Kingdom.
The points in favor of Python are undoubtedly dynamism, simplicity and flexibility. Just such features make this programming language perfect for your purposes.
So if you wanted to create a Telegram bot with Python, you should definitely read this in-depth study. In it, you find an example of a bot that writes the text of the messages backwards.
Telegram bot creation with Java
The programming language Java it is certainly one of the most used in the world. Again, this is an option oriented towards object-oriented programming. It rests its foundation on the software platform that gave it its name, owned by Sun Microsystem.
Java is widely used when building applications for mobile devices, especially for Android. Precisely for this reason, it could be very interesting to create a Telegram bot with Java.
After a careful search on the net, I found this guide that can be for you. It describes the steps for creating a bot that sends photos to users based on emojis. The tutorial includes all the relevant insights, from creating the database with MongoDB to integrating with Redis.
Create Telegram bot with Node.js
Node.js it is not a real programming language. In reality it is a runtime linked to JavaScript Open Source, a platform oriented for the execution of server-side code. The java scripts are in fact incorporated into the HTML code of a web page and interpreted by the browser.
It is ideal for browser games and applications that need real-time communication. Precisely for this reason, it can be particularly interesting to create a Telegram bot with Node.js.
To do this, however, you need a well-made tutorial, which luckily for you, you can find at this link. Over there, you will find all node.js packages for Windows, Linux and macOS. In addition, you can see an example that illustrates, step by step, the generation of a bot that replies to messages with "Hello World".
Telegram bot creation with PHP
PHP (acronym for Personal Home Page) is a language used for programming dynamic web pages. Less common in the past, today PHP is present on practically any website. Its use is also exploited to create server-side web applications or stand-alone applications.
Born in 1994 thanks to a Danish programmer, PHP was initially used for many open source projects related to the Internet. Subsequently, this language has undergone considerable expansion, with consequent updates.
Again, if you wanted to carry out a particular project, you could create a Telegram bot with PHP. To learn the basics and understand the dynamics to use, just read this technical manual on GitHub carefully.
More examples of Telegram bots and documentation
Still haven't figured out which path you want to take to create a Telegram bot? Don't worry, you often approach a topic like this with a bit of healthy curiosity, then going to select the most interesting niches.
Precisely for this reason, I recommend that you take a look at this page of the Telegram help center, where you will be able to find numerous examples of bots and a lot documentation related.