User Tools

Site Tools


bash_в_telegram

Отправка сообщения из bash в Telegram

Чтобы отправить себе сообщение через Telegram, нужно сделать 3 вещи.

  1. Создайте бота, получите свой идентификатор чата
  2. Начните чат с этим ботом
  3. Отправьте сообщение через командную строку.

Создание бота

Все боты Telegram имеют глобальную видимость. Это важно при установке имени для бота. Если в дальнейшем, будут создаваться еще другие боты, желательно называть их – myusername_botname. Благодаря этому у всех моих ботов одинаковые имена.

Чтобы настроить бота, нужно начать чат с @botfather. Отправляем сообщение, /newbot и BotFather проведет вас через некий “мастер настройки” нового бота. Типичный разговор с @botfather:

nevvad, [24.06.21 18:33]
/newbot
 
BotFather, [24.06.21 18:33]
Alright, a new bot. How are we going to call it? Please choose a name for your bot.
 
V, [24.06.21 18:33]
nevvad_master
 
BotFather, [24.06.21 18:33]
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.
 
V, [24.06.21 19:02]
nevvad_master_bot
 
BotFather, [24.06.21 19:02]
Done! Congratulations on your new bot. You will find it at t.me/nevvad_master_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.
 
Use this token to access the HTTP API:
102932392:AAGjexEEZBJ3D09ubEb57zAAhq_qF8MDEI
Keep your token secure and store it safely, it can be used by anyone to control your bot.
 
For a description of the Bot API, see this page: https://core.telegram.org/bots/api

Начинаем чат с новоиспеченным ботом – https://t.me/nevvad_master_bot

B и получаем id чата, для этого обратимся к боту @userinfobot либо к @ShowJsonBot если нужно больше информации. Идентификатор чата будет иметь вид 394882049

Отправляем сообщение

API_KEY="the access token"
CHAT_ID="the chat id"
curl -s "https://api.telegram.org/bot$API_KEY/sendMessage?chat_id=$CHAT_ID&text=my_message" > /dev/null
#!/bin/bash
API_KEY="some_key"
# пробелы заменяем на %20
RES=$(echo "my time is $(date +%F)" | sed  "s/ /%20/g")
echo $RES
CHAT_ID=11111
curl -s "https://api.telegram.org/bot$API_KEY/sendMessage?chat_id=$CHAT_ID&text="$RES"" >/dev/null>
bash_в_telegram.txt · Last modified: 2023/04/06 10:28 (external edit)