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"