How about to get a push notification when some long-running job in Linux is completed?
Or how about to have a script in crontab
that collects some data and periodically sends the reports to your mobile device?
This tutorial describes how to create a personal notification bot, that can send messages from the Linux command-line through the Telegram API.
Cool Tip: Install a Telegram’s app on Ubuntu and Linux Mint! Read more →
Telegram Messenger
Download and install the telegram messenger for your platform.
Start the telegram client and follow the registration process.
Create Telegram Bot
Start a conversation with the BotFather
:
🔍 GLOBAL SEARCH -> BotFather
BotFather: The BotFather
is the one bot to rule them all. Use it to create new bot accounts and manage your existing bots.
Create a new bot:
/newbot
Choose a user-friendly name for your bot, for example:
Notifier
Choose a unique username for your bot (must ends with “bot”), for example:
notifier_bot
Once the bot is created, you will get a token to access the Telegram API.
TOKEN: The token is a string that is required to authorize the bot and send requests to the Telegram API, e.g. 4334584910:AAEPmjlh84N62Lv3jGWEgOftlxxAfMhB1gs
Get The Chat ID
CHAT_ID: To send a message through the Telegram API, the bot needs to provide the ID of the chat it wishes to speak in. The chat ID will be generated once you start the first conversation with your bot.
Start a conversation with your bot:
🔍 GLOBAL SEARCH -> MY_BOT_NAME -> START
/start
command followed by any message (to not get stuck at {"ok":true,"result":[]}
while trying to get a chat ID):
/start Hello my bot!
To get the chat ID, open the following URL in your web-browser: https://api.telegram.org/bot<TOKEN>/getUpdates
(replace <TOKEN>
with your bot token).
If you prefer a Linux command-line interface, you can simply request the above URL from shell with curl
, for example:
$ curl https://api.telegram.org/bot4334584910:AAEPmjlh84N62Lv/getUpdates ..."chat":{"id":123456789,"first_name":"my_first_name","type":"private"}...
Cool Tip: How to send a message to a Telegram channel using PHP! Read more →
Telegram Bot API: Send Message
Below i will show the examples of how to send a message through the Telegram API using a web-browser, curl
and a Bash script.
Web-Browser
To send the “Hello World” message using a web-browser, just open the URL:
https://api.telegram.org/bot<TOKEN>/sendMessage?chat_id=<CHAT_ID>&text=Hello%20World
Linux Command Line
Send a message from the Linux command-line interface, using curl
:
$ curl -s -X POST https://api.telegram.org/bot<TOKEN>/sendMessage -d chat_id=<CHAT_ID> -d text="Hello World"
Bash Script
A simple Bash script that sends the “Hello World” message through the Telegram API:
#!/bin/bash TOKEN=<TOKEN> CHAT_ID=<CHAT_ID> MESSAGE="Hello World" URL="https://api.telegram.org/bot$TOKEN/sendMessage" curl -s -X POST $URL -d chat_id=$CHAT_ID -d text="$MESSAGE"
Cool Tip: Send emails through SMTP server using telnet
from the Linux command-line interface! Read more →
Ошибочка:
Отправить сообщение из командной строки а Linux, с помощью curl:
Нет слеша перед sendMessage.
А так спасибо за статью!
Поправил. Спасибо.
Спасибо, работает!
Отличная статья! Все работает.
Большое спасибо!
Отличная статья! Большое спасибо)))
Если в теле сообщения есть #, то сообщение не отображается, можно ли это обойти?
very usefull
Спасибо, отличный скрипт!
Нужно заменить решётку на ее код %23 кажется.
Debian 9, OpenSSL 1.1.0f
>curl: (60) SSL certificate problem: self signed certificate
а как прокси применить? только для этого баш-скрипта чтоб,
Спасибо за статью.
При установленном торе в линукс пользуемся curl таким образом:
curl –socks5-hostname 127.0.0.1:9050 -s -X POST https://api.telegram.org/bot123456:fgflgfg-dfgfg-fgfgfgf/sendMessage -d chat_id=12345678 -d text=”proizvolniy text”
Ввиду судебных преследований можно использовать на своём заграничном сервере прослойки в виде php-скрипта, например вот этот:
https://github.com/mcnemesis/proxy.php
А как отправить сообщение на заданный аккаунт в телеграме? Пользователю
Is there any option to send the file like photo.jpg ?
I open https://api.telegram.org/bot/getUpdates this link in my browser and got this {“ok”:true,”result”:[]} what’s next?
Some changes are required to make it work.
1. Don’t `/start` your bot before opening your token url.
2. Open your token url. (it should give you something like `{“ok”:true,”result”:[]}`
3. Go to your bot and `/start` it
4. Refresh your token url. (it should now give you some data)
If you did `/start` your bot before opening your token url and got stuck at `{“ok”:true,”result”:[]}`, go to your bot and delete conversation. After that follow steps 3 and 4.
If you don’t get a Chat ID send a message in the chat and refresh the page in the browser.
How do I send a scheduled message?
like daily notifications
For a Linux system, use the contrab -e for schedule sends.
thanks, I used curl command and worked nice.
Hi, i’m having some trouble trying to send text from the clipboard — im running a automator workflow to do run Speedtest CLI, then filter the results (load as parameter to copy to clipboard only the Download and Upload Speeds) and the send the clipboard to telegram using the CURL Telegram Api route… nice, everything work, except i cannot paste the clipboard to telegram.
The CURL is:
curl -X POST -H ‘Content-Type: application/json’ -d ‘{“chat_id”: “-xxxxxxxxx”, “text”: (here goes the text from PBPASTE – don’t know how to do it), “disable_notification”: true}’ https://api.telegram.org/botBOTAPITOKEN/sendMessage
Any helo / guide will be deeply appreciated (im not a programmer)
send me my Telegram code in my phone ?