Why Telegram Bot API Is Special
Most APIs make you fight with OAuth, rate limits, and pagination. Telegram Bot API gives you a simple HTTP interface with instant webhooks, no OAuth, and generous limits.
Create a Bot in 60 Seconds
- Open Telegram, search for @botfather
- Send /newbot
- Choose a name and username
- Copy the API token
Done. No OAuth app registration, no redirect URIs, no client secrets.
Send a Message
import requests
TOKEN = "your_bot_token"
BASE = f"https://api.telegram.org/bot{TOKEN}"
def send_message(chat_id, text):
r = requests.post(f"{BASE}/sendMessage", json={
"chat_id": chat_id, "text": text, "parse_mode": "HTML"
})
return r.json()["ok"]
send_message("your_chat_id", "<b>Hello</b> from Python!")
Get Updates (Polling)
def get_updates(offset=0):
r = requests.get(f"{BASE}/getUpdates", params={"offset": offset, "timeout": 30})
return r.json()["result"]
# Simple bot loop
offset = 0
while True:
updates = get_updates(offset)
for u in updates:
msg = u.get("message", {})
text = msg.get("text", "")
chat_id = msg["chat"]["id"]
if text == "/start":
send_message(chat_id, "Welcome! I am your bot.")
elif text == "/help":
send_message(chat_id, "Commands: /start, /help, /ping")
elif text == "/ping":
send_message(chat_id, "Pong!")
offset = u["update_id"] + 1
Send Files, Photos, Locations
# Send a photo
def send_photo(chat_id, photo_url, caption=""):
requests.post(f"{BASE}/sendPhoto", json={
"chat_id": chat_id, "photo": photo_url, "caption": caption
})
# Send location
def send_location(chat_id, lat, lon):
requests.post(f"{BASE}/sendLocation", json={
"chat_id": chat_id, "latitude": lat, "longitude": lon
})
# Send a document
def send_document(chat_id, file_url):
requests.post(f"{BASE}/sendDocument", json={
"chat_id": chat_id, "document": file_url
})
Inline Keyboards (Buttons)
def send_with_buttons(chat_id, text, buttons):
keyboard = {"inline_keyboard": buttons}
requests.post(f"{BASE}/sendMessage", json={
"chat_id": chat_id, "text": text, "reply_markup": keyboard
})
send_with_buttons("chat_id", "Choose an option:", [
[{"text": "Option A", "callback_data": "a"},
{"text": "Option B", "callback_data": "b"}],
[{"text": "Visit Website", "url": "https://example.com"}]
])
Real Bot Ideas
- Server monitor — send alerts when CPU/memory spikes
- Daily digest — summarize news/data every morning
- Expense tracker — log expenses by sending messages
- Deployment notifier — alert on successful/failed deploys
- RSS reader — forward new articles from your favorite blogs
Why Not Discord/Slack?
| Feature | Telegram | Discord | Slack |
|---|---|---|---|
| Bot creation | 60 seconds | 5 minutes | 10 minutes |
| OAuth needed | No | Yes | Yes |
| Free API | Unlimited | Yes | Limited |
| Message formatting | HTML/Markdown | Markdown | Blocks |
| File upload limit | 50MB | 25MB (free) | Varies |
| Webhook setup | 1 API call | Dashboard | Dashboard |
More from me: 10 Dev Tools I Use Daily | 77 Scrapers on a Schedule | 150+ Free APIs
Top comments (0)