📜 ⬆️ ⬇️

Minimal "hello world" telegram bot

Here, in four steps, the magician will show how to assemble the bot annunciator for a telegram without pain and reading the documentation.

  1. Sign up for a telegram
  2. Get the ID of the new bot (hereinafter <Bot_token> ): contact the @BotFather bot with a request to create a new bot (/ newbot command)
  3. Get the ID of the conversation with the bot (hereinafter <chat_id> ):
    • Open a dialogue with your created bot and write an arbitrary message to it.
    • Open the link in the browser, replacing <Bot_token> with the received identifier from @BotFather https://api.telegram.org/bot<Bot_token>/getUpdates
    • In the received json-answer, find the value in the parameter result-> message-> chat-> id, this is <chat-id>
  4. Open a browser and follow the link, replacing <Bot_token> and <chat_id> with your data
    https://api.telegram.org/bot<Bot_token>/sendMessage?chat_id=<chat_id>&text=%20

Thus, you will receive a message from the bot to all your devices with a telegram client.

You can use the last call in a bat-file or console or distribute to friends who do not have a telegram and write from a smart iron (but only to very good friends as <Bot_token> confidential information actually).

Use cases
Replacing <Bot_token> and <chat_id> with our data, we get:
Console example:
ping yandex.ru -t || start https://api.telegram.org/bot<Bot_token>/sendMessage?chat_id=<chat_id>^&text=%20%20

An example for a bat file saved as utf-8 without a BOM and running from taskschd.msc once every 2 weeks:
chcp 65001
start https://api.telegram.org/bot<Bot_token>/sendMessage?chat_id=<chat_id>^^^&text=C%20%20


I recall the option of running a single task from the console:
at 19:47 c:\temp\go_home.bat

Python example:
import urllib2
get = urllib2.urlopen('https://api.telegram.org/bot<Bot_token>/sendMessage?chat_id=<chat_id>&text=%20python%20%20'.encode('UTF-8'))


')

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


All Articles