Back to Home

Push notifications via Telegram

On June 25 · Telegram opened a platform for bots. My first thought was - you can send notifications through it! With the help of notifications via telegram · you can solve several problems at once · at least ...

Push notifications via Telegram



    On June 25, Telegram opened a platform for bots . My first thought was - you can send notifications through it! Using telegram notifications, you can solve several problems at once, at least partially:
    • IOS support
    • Windows Phone Support
    • Receive notifications without installing unnecessary applications

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



    Under the cutter, a little setup instructions and development details. (a small instruction for those who are going to make the same bot)

    Bind


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



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

    Among the disadvantages of this method of admission are:
    • Failure to further modify notifications
    • Inability to track delivery (maybe temporarily)
    • Inability to send an icon to a message (transferring an image file with a so-so option each time)


    Technical details


    Everything works quite simply.
    There is API documentation here.
    I created the bot with the / newbot command and followed the instructions, then set the piccu with the / setuserpic command.
    Using the received token, I bound Webhook to receive messages.
    Here you need to understand one small thing: if you work 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 parse the JSON response.
    Sending goes by 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 messages sent. Therefore, this function will be tested for a long time. At a minimum, I do not like that for each message you need to make a separate request. This means that I either have to do them in several streams of several hundred per second, or do them in turn. And the processing of each takes about 50-100 ms, which is long enough. If there are any restrictions there, I may run into 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 that you can make good bots for blind chat. When the interlocutors do not know anything about each other. Through Redis do the interaction. When you receive a message through Webhook, send a message to another person waiting for the message. After creating a connection, forward messages using different chat_id users.

    Telegram's API turned out to be very simple. No developer accounts and the like are needed. You create a bot immediately get a token, immediately work with the API.

    Read Next