Telegram API: Send Message – Personal Notification Bot

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.

Telegram Messenger

Download and install the telegram messenger for your platform.

Start the telegram client and follow the registration process.

Cool Tip: Mass emails sending from a Bash script in Linux! Read more →

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

Send the /start command:

/start

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 are a Linux guy and prefer the 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: Send emails through SMTP server using telnet from the Linux command-line interface! 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"

21 Replies to “Telegram API: Send Message – Personal Notification Bot”

  1. Ошибочка:
    Отправить сообщение из командной строки а Linux, с помощью curl:

    $ curl -s -X POST https://api.telegram.org/botsendMessage -d chat_id= -d text="Hello World"

    Нет слеша перед sendMessage.
    А так спасибо за статью!

    1. Поправил. Спасибо.

  2. Спасибо, работает!

  3. Отличная статья! Все работает.

  4. Большое спасибо!

  5. Отличная статья! Большое спасибо)))

  6. Если в теле сообщения есть #, то сообщение не отображается, можно ли это обойти?

  7. Спасибо, отличный скрипт!

  8. Нужно заменить решётку на ее код %23 кажется.

  9. Debian 9, OpenSSL 1.1.0f
    >curl: (60) SSL certificate problem: self signed certificate

  10. а как прокси применить? только для этого баш-скрипта чтоб,

  11. Спасибо за статью.
    При установленном торе в линукс пользуемся 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”

  12. Ввиду судебных преследований можно использовать на своём заграничном сервере прослойки в виде php-скрипта, например вот этот:
    https://github.com/mcnemesis/proxy.php

  13. А как отправить сообщение на заданный аккаунт в телеграме? Пользователю

  14. Is there any option to send the file like photo.jpg ?

  15. I open https://api.telegram.org/bot/getUpdates this link in my browser and got this {“ok”:true,”result”:[]} what’s next?

  16. 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.

  17. If you don’t get a Chat ID send a message in the chat and refresh the page in the browser.

  18. How do I send a scheduled message?
    like daily notifications

  19. For a Linux system, use the contrab -e for schedule sends.

Leave a Reply