📜 ⬆️ ⬇️

Push notifications via Telegram



June 25 Telegram opened the platform for bots . My first thought was - after all, you can send notifications through it! With the help of telegram notifications, you can solve several problems at once, at least partially:

Thus, PushAll can now send notifications to Android, Chrome and Telegram devices through a bot.


')
Under the cut is a small guide to setting up and development details. (a small instruction for those who are going to do the same bot)

We tie


You just need to open the link - telegram.me/PushAllBot and write to the bot anything.
In response, he will give a link that you need to go to establish a connection.



After the transition, you will be prompted to log in, if you have not logged in, or a message will be displayed that the device is tied.

Among the disadvantages of this method of admission are:


Technical details


Everything works quite simply.
There is API documentation.
I created a bot with the / newbot command and followed the instructions, then I set up a picchu, with the / setuserpic command
Using the received token, I tied the Webhook to receive messages.
Here you need to understand one small thing: if you are working with PHP you will not get the data in the $ _POST variable.
JSON data can be obtained using the command:

file_get_contents('php://input') 


And then you can already parse JSON response.
Sending is on the chat ID. It is equal to the user ID.
I did not find any restrictions on the use of the API or the number of sending messages. Therefore, this function will be tested for a long time. At least, I don’t like the need to make a separate request for each message. This means that I either have to do them in several threads of several hundred per second, or do them in turn. And the processing of each takes about 50-100 ms, which is quite long. If there are any other restrictions there, I may encounter a problem when I can send out 1000 notifications in just a few minutes.

This is how Webhook works for me:

 $gram=json_decode(file_get_contents('php://input'),true); $message=', ' .$gram['message']['chat']['last_name'].' ' .$gram['message']['chat']['first_name'].'.  ID  Telegram: ' .$gram['message']['chat']['id']; file_get_contents('https://api.telegram.org/botTOKEN/sendMessage?chat_id=' .$gram['message']['chat']['id'].'&text=' .urlencode($message)); 


It seems to me, you can make good bots for the "blind" chat. When interlocutors know nothing about each other. Through Redis to make interaction. When you receive a message through Webhook, send a message to another message pending. After creating a connection, forward messages using different chat_id users.

The Telegram API was very simple. No developer accounts, and the like - not needed. When you create a bot, you immediately get a token, immediately work with the API.

Source: https://habr.com/ru/post/261531/


All Articles