Telegram bot notifications: the two values you need
A Telegram bot is the simplest way to get notifications from a script, a server or a home project onto your phone. Monitoring alerts, finished backups, price thresholds and failed cron jobs can all become messages in a chat you already read. To send them you need two values: the bot token, which identifies your bot, and the chat ID, which says where the message goes. This guide shows how to get both, how to test them, and how to send messages from Python and the command line.
In this guide
- Telegram bot notifications: the two values you need
- Step 1: create the bot with BotFather
- Step 2: start a chat with your bot
- Step 3: find the chat ID
- Step 4: send a test message
- Sending messages from Python and curl
- Formatting, silence and limits
- Keeping the token safe
- Common errors and what they mean
- Questions readers often ask
Step 1: create the bot with BotFather
Every Telegram bot is created through @BotFather, an official Telegram bot. Before you write to it, check for the blue verification tick, because look-alike accounts exist.
- Open a chat with @BotFather and send
/newbot. - Enter a display name, for example Server Alerts. You can change it later.
- Enter a username that ends in
bot, for examplemyserver_alerts_bot. It must be unique across Telegram. - BotFather replies with an HTTP API token that looks like
123456789:AAH4xYz…. The part before the colon is the bot’s numeric ID and the rest is its secret.
Treat the token as a password. Anyone who has it can send messages as your bot and read the messages people send to it.
Step 2: start a chat with your bot
A bot cannot write to a person who has never talked to it. Open your bot through the link BotFather sent, press Start and send any message. This also gives the next step something to read.
Step 3: find the chat ID
Every private chat, group and channel has its own ID. The most reliable way to find it is to ask Telegram which messages your bot has received. Open this address in a browser, with your token in place of <TOKEN>:
https://api.telegram.org/bot<TOKEN>/getUpdatesThe JSON reply contains "chat":{"id":412345678,…}. That number is your chat ID.
| Destination | How to get the ID | Looks like |
|---|---|---|
| Private chat with you | Send the bot a message, then call getUpdates | 412345678 |
| Group | Add the bot to the group, post a message in the group, then call getUpdates | -1001234567890 (negative) |
| Public channel | Add the bot as an administrator with permission to post; the channel’s username works as the ID | @my_channel |
| Private channel | Add the bot as an administrator, post in the channel, then read channel_post.chat.id from getUpdates | -100… |
If the reply is "result":[], the bot has not received anything yet, or another program already read the updates. Send a new message and reload. In groups, the bot’s privacy mode means it sees only commands and replies to it. Send /start@your_bot in the group so the message reaches it. A bot that uses a webhook does not answer getUpdates. Remove the webhook with deleteWebhook first, or take the ID from your webhook logs.
Step 4: send a test message
Check that the token and chat ID work together before you build anything on them:
https://api.telegram.org/bot<TOKEN>/sendMessage?chat_id=<CHAT_ID>&text=HelloA reply with "ok":true and the message in your chat means everything works. The address contains your token, so do not share a screenshot of the browser bar.
Sending messages from Python and curl
Keep the token and chat ID out of the code. Put them in environment variables or a .env file that is not committed:
TG_TOKEN=123456789:AAH4xYz...
TG_CHAT=412345678Python, standard library only:
import json
import os
import urllib.parse
import urllib.request
def send_telegram(text: str) -> bool:
token, chat = os.environ["TG_TOKEN"], os.environ["TG_CHAT"]
data = urllib.parse.urlencode({
"chat_id": chat,
"text": text,
"parse_mode": "HTML",
"disable_web_page_preview": "true",
}).encode()
url = f"https://api.telegram.org/bot{token}/sendMessage"
try:
with urllib.request.urlopen(url, data=data, timeout=10) as r:
return json.load(r).get("ok", False)
except Exception:
return False # never let a failed notification crash the main job
send_telegram("<b>Backup finished</b> in 4 min 12 s")Shell, for example at the end of a cron job:
curl -s "https://api.telegram.org/bot${TG_TOKEN}/sendMessage" \
--data-urlencode "chat_id=${TG_CHAT}" \
--data-urlencode "text=Disk usage on $(hostname) is above 90%"Use POST rather than putting the text in the URL. Long or special-character messages then arrive intact, and the text does not end up in web server logs.
Formatting, silence and limits
- Formatting: with
parse_mode=HTMLyou can use<b>,<i>,<code>,<pre>and<a href>. Escape<,>and&in any text that comes from outside, or Telegram rejects the message. - Quiet messages:
disable_notification=truedelivers a message without a sound, which is useful for routine heartbeats. - Length: one message holds up to 4096 characters. Split longer reports or send them as a file.
- Rate limits: Telegram allows roughly one message per second to a chat and about 20 per minute to a group. Deduplicate repeated alerts and group bursts into one summary message instead of sending dozens.
Keeping the token safe
- Store the token only in an environment variable, a
.envfile or a secret manager. Add.envto.gitignore. - If the token leaks, send
/revoketo @BotFather, choose the bot and replace the token everywhere it is used. The old token stops working immediately. - A chat ID alone is not secret. Combined with the token, it lets anyone write into your chat.
- Do not paste the
getUpdatesoutput into public forums: it contains your messages and user details.
Common errors and what they mean
| Error | Usual cause |
|---|---|
401 Unauthorized | Wrong or revoked token, or a missing bot prefix in the URL |
400 chat not found | Wrong chat ID, or you never pressed Start in the bot chat |
403 bot was blocked by the user | The recipient blocked the bot; unblock it in the chat |
403 bot is not a member of the channel chat | The bot is not an administrator of the channel |
400 can't parse entities | Unescaped < or & with parse_mode=HTML |
429 Too Many Requests | Rate limit; wait for the number of seconds in retry_after |
Questions readers often ask
Is a Telegram bot free?
Yes. Creating a bot and sending messages through the Bot API costs nothing.
Can one bot send to several people?
Yes. Each person must start the bot once, and you send a separate message to each chat ID. For a larger audience, a group or channel with one ID is simpler.
Does the chat ID change?
A private chat ID stays the same. A group gets a new ID with a -100 prefix when it is upgraded to a supergroup, so update your configuration if messages to a group suddenly fail.
Do I need a server running all the time?
Not for sending. Any script that can make an HTTPS request can send a message. A permanently running process is needed only when the bot has to answer incoming messages.
Primary references: Telegram’s bot tutorial, the Telegram Bot API documentation and Telegram’s bot FAQ.