Steps
Create a bot with BotFather
In Telegram, open @BotFather (the one with the blue check), send
/newbot, then pick a display name and a username that ends inbot. BotFather replies with a token like123456789:AAH.... That token is the only key you need.Check the token works
Open
https://api.telegram.org/botYOUR_TOKEN/getMein a browser (keep the wordbotright before the token). A good token returns"ok":truewith your bot's username. A wrong token returns401 Unauthorized; a cut-off token or missingbotprefix returns404 Not Found.Find your chat id
Open your bot, press Start and send it any message, then open
https://api.telegram.org/botYOUR_TOKEN/getUpdatesand look for"chat":{"id":...}. A private chat id is a positive number. In a group, add the bot and send/start@your_bot(bots only see commands and mentions in groups by default); the id is negative, and supergroups start with-100. For a channel, make the bot an admin that can post; you can then use@channelnamefor a public channel, or the-100...id from a post forwarded to getUpdates. Empty result? Send a fresh message and reload.Send your first message with curl
From any terminal:
The reply starts withcurl -s -X POST "https://api.telegram.org/botYOUR_TOKEN/sendMessage" \ -d chat_id=YOUR_CHAT_ID \ -d text="Hello from my script"{"ok":true,"result":{"message_id":...and the message appears in your chat. On Windows PowerShell, usecurl.exeinstead ofcurl.Send from a script, then schedule it
Use the Python script below in any job: a backup, a scraper, a price check. It escapes the text, retries once when Telegram says to slow down, and exits with an error you can see in logs. Run it on a schedule with cron, Task Scheduler or GitHub Actions; see run a Python script on a schedule.
The script
Save this as send_telegram.py. It uses only Python's standard library, so there is nothing to install.
#!/usr/bin/env python3
"""Send a Telegram message from a script. Standard library only."""
import html, json, os, sys, time, urllib.error, urllib.request
TOKEN = os.environ["TELEGRAM_BOT_TOKEN"]
CHAT_ID = os.environ["TELEGRAM_CHAT_ID"]
API = os.environ.get("TELEGRAM_API", "https://api.telegram.org")
def send(text, tries=3):
body = json.dumps({"chat_id": CHAT_ID, "text": text[:4096], "parse_mode": "HTML",
"disable_web_page_preview": True}).encode()
for attempt in range(tries):
req = urllib.request.Request(f"{API}/bot{TOKEN}/sendMessage", data=body,
headers={"Content-Type": "application/json"})
try:
with urllib.request.urlopen(req, timeout=20) as r:
return json.load(r)["result"]["message_id"]
except urllib.error.HTTPError as e:
err = json.load(e)
wait = err.get("parameters", {}).get("retry_after")
if e.code == 429 and wait and attempt < tries - 1:
time.sleep(wait) # Telegram tells you how long to wait
continue
sys.exit(f"Telegram said {e.code}: {err.get('description')}")
title = sys.argv[1] if len(sys.argv) > 1 else "Hello from my script"
print("sent message", send(f"<b>{html.escape(title)}</b>"))
Run it with your token and chat id in the environment:
export TELEGRAM_BOT_TOKEN=123456789:AA...
export TELEGRAM_CHAT_ID=987654321
python3 send_telegram.py "Backup finished"What we saw when we tested it (19 Oct 2026)
- Special characters: the title
Price <$10 & "free"went out as<b>Price <$10 & "free"</b>, so it shows exactly as typed, in bold, instead of breaking the HTML formatting. - Rate limit: when our stand-in for Telegram answered
429with"retry_after": 2, the script waited 2.0 seconds and the second try was delivered. One message, no duplicate. - Wrong chat: answers of
400 Bad Request: chat not foundand403 Forbidden: bot was blocked by the userstopped the script with that message and exit code 1, so cron logs and schedulers see the failure. - The real Telegram API with a made-up token answered
401 Unauthorized, and a token without thenumber:lettersshape answered404 Not Found. - Honest limit: apart from those real 401/404 answers, we tested against a local stand-in that mimics Telegram's replies, not a live bot.
Common errors and what they mean
401 Unauthorized: the token is wrong or was revoked in BotFather. Copy it again.404 Not Found: the URL is malformed, usually a missingbotbefore the token (/bot123:AA.../sendMessage) or a cut-off token.400 Bad Request: chat not found: the chat id is wrong, the person never pressed Start in your bot, or the bot isn't in that group or channel.403 Forbidden: bot was blocked by the userorbot is not a member: the person blocked the bot, or it was removed from the group.400 Bad Request: can't parse entities: withparse_modeset, a raw<,&or Markdown symbol in your text broke the formatting. Escape it, as the script does, or dropparse_mode.429 Too Many Requests: you are sending too fast. Wait theretry_afterseconds from the reply, then try again.409 Conflicton getUpdates: the bot has a webhook set. Callhttps://api.telegram.org/botYOUR_TOKEN/deleteWebhookif you don't need it.
Good to know
- Using the n8n Telegram node instead? It sends Markdown and an n8n attribution line by default and hides Telegram's reason behind generic errors: see n8n Telegram node not working.
- Message length: one message holds up to 4096 characters. The script cuts anything longer; split long reports into several messages if you need all of it.
- How fast you can send: Telegram's bot FAQ suggests about one message per second in a single chat, no more than 20 a minute in a group, and about 30 a second overall. For alerts, group several items into one message.
- HTML or MarkdownV2? HTML only needs
<,>and&escaped. MarkdownV2 needs a backslash before many characters, including_ * [ ] ( ) ~ ` > # + - = | { } . !, which is easy to get wrong with titles from feeds. - Prefer n8n? Add a Telegram credential with your bot token, then a Telegram node with Send Message, your chat id and the text. To stop repeats on scheduled workflows, see Remove Duplicates across executions. Hitting 429s? See n8n rate limits.
- Keep the token secret: anyone with it controls your bot. If it leaks, send
/revoketo BotFather and update your scripts.
9 free Telegram alerts you can set up next
Each one uses the same bot token and chat id you just found, and each comes with a free tested script.
- Any RSS feed, filtered by keyword: blogs, news, job boards, patch notes. Guide
- Reddit keyword mentions: your product, game or name, no Reddit API key. Guide
- New YouTube uploads: with or without Shorts. Guide
- Twitch go-live alerts: one message per stream. Guide
- Free games: Epic's weekly freebies plus Steam, GOG and itch.io giveaways. Guide
- Steam wishlist price drops: your target price or a new low. Guide
- GitHub releases: any public repo, pre-releases skipped. Guide
- Important emails: Gmail, Outlook or any IMAP, filtered by sender or subject. Guide
- A web page changed or is back in stock: with the lines that changed. Guide
Using Discord instead? Start with Discord webhooks for beginners and the 13 Discord webhook ideas.
Start from a working agent
FAQ
Is the Telegram Bot API free?
Yes. Creating bots with BotFather and sending messages through the Bot API costs nothing.
Can my bot message someone first?
No. A person has to open the bot and press Start (or add it to a group) before the bot can message them. That's why 'chat not found' is the most common first error.
Do I need a server to run a Telegram bot?
Not for sending alerts. A script that calls sendMessage and exits is enough, run by cron, Task Scheduler or GitHub Actions. You only need something always online if the bot must answer messages instantly.
How do I send to several chats?
Call sendMessage once per chat id, about one per second, or post to a channel that everyone joins.
What's the difference between the chat id and the bot username?
The username (like @my_alerts_bot) is how people find the bot. The chat id is the number for the conversation the bot posts into: you, a group or a channel.
Related guides
- n8n Telegram node not working? 20 tests, every error decoded
- Twitch live notifications in Telegram: get a message when a streamer goes live
- n8n Slack node not working: invalid_auth, not_authed, channel_not_found fixes
- Steam wishlist price drop alerts in Telegram (free, no API key)
- n8n Python Code node not working? Task runner errors, 9 tests
New guides
- GitHub release notifications in Slack (any public repo, stable releases only)
- Send RSS feeds to Slack with a keyword filter (free, incoming webhook)
- Forward emails to a Slack channel (Gmail, Outlook, any IMAP), free with an incoming webhook
- n8n Google Sheets Trigger not working: not firing, activation errors and fixes