For Telegram there are so-called Bots – ultimately these are simply user accounts for programs/robots instead of people. These accounts can then be addressed via API/interface and used “normally” in Telegram.
For example, you can integrate surveys for appointments into a Telegram group, cobble together your own clients, send files, photos or text from the command line, display messages as Telegram messages and so on. The nice thing about it: You create the bot very simply via the Telegram standard account Botfather in a dialog. We show you step by step how to create a bot and send texts and files from the command line. Prior knowledge is not required.
[UPDATE] In the second part we show you how you Receive messages in the terminal can.
[UPDATE 2/XNUMX/XNUMX] Since the article is a bit older: As of September 2017, the procedure for Telegram bots still works. If something doesn't work check the quotes!
Of course there are also ready-made Telegram bots for dog pictures and stuff like that, but this is about creating your own bots:
1. Integrate Botfather
First add the "user" @botfather - via the search function or the Link on home page.
2. Create Telegram bot
To create your bot, simply send the new user Botfather the messages
and then assigns user and bot names on request - the latter must end with bot. At the end you get the so-called token, a longer character string that uniquely identifies your bot. It is best to copy the token code directly. Optionally, you can also set up an about page, a user picture for the bot, etc. – simply send commands like /setuserpic, /help and so on in the Telegram client. Don't forget: You have to start the bot now and send a first message, otherwise the next step won't work.
3. Find out ID of chat
In order to send a message to a Telegram chat or a Telegram group, you need the corresponding chat ID, which you can get with the following command:
curl -X POST https://api.telegram.org/bot123456:abcde1234ABCDE/getUpdates
First of all: You must of course replace 123456:abcde1234ABCDE with your token code – but the preceding “bot” must remain. The Linux standard tool curl is only used to send an HTTP request to Telegram. On Windows you have to first curl for Windows to install. By the way: cURL stands for "see url", but it doesn't matter. The result of this query is something like this:
The ID of the chat is important here, in this example 268963852. Note: Of course you can add your new bot to a group like any other account - then the ID of the group chat that you can use will also appear in the above result to post to the group. If no chats appear: Post a message in the desired chat - of course there are no updates without messages!
4. Send messages and files to Telegram
We also use curl again to send messages:
curl -X POST 'https://api.telegram.org/bot123456:abcde1234ABCDE/sendMessage?chat_id=-4194264&text="texty text"'
You see, it's again the same construct as above but with the method/command "sendMessage" appended to it, separated by ?, first the chat id and then separated with & the text to send. And the message "texty text" ends up in your Telegram account, sent by "user" TutoBot in our example. Sending files and photos is then self-explanatory:
curl -X POST "https://api.telegram.org/bot123456:abcde1234ABCDE/sendDocument" -F chat_id=-419426123 -F document="@/home/mirco/misto.txt"
The method is now called sendDocument - however, the HTTP request must be sent in the format "multipart/form-data", which is why the chat ID and document path are appended separately to the curl command via F parameters. In this example the file misto.txt will be sent to our Tutonaut own group chat (the dash belongs to the group chat id!). Remember the @ in front of the document path. The photo command is completely analogous:
curl -X POST "https://api.telegram.org/bot123456:abcde1234ABCDE/sendPhoto" -F chat_id=-4194264 -F photo="@/home/mirco/rathaus.jpg"
Use Telegram bots productively
So far it's all just a gimmick - what's next? On the one hand, you could of course also address Telegram via programming languages and knit entire program logic around it. But there is still a lot that can be done on the command line: Instead of a fixed text, you can of course also use a variable, for example “$text” instead of “texty text” – and this variable could be easily assigned via script or user requests. A super simple example would be a mini-script like:
echo "Gib die Nachricht ein:" && read text && curl -X POST "https://api.telegram.org/bot123456:abcde1234ABCDE/sendMessage?chat_id=-4194264&text=$text"
If you save it as an alias or script and then call it up, "Enter the message" is first output, then you just enter the message and the entered text is sent to Telegram via return. So far just as an introduction.
If something is unclear, just ask, there are no such thing as “stupid” questions anyway. And what do you do with bots? Or would you like to do it? What do you think of it anyway?