Telegram Bot API — The Most Developer-Friendly API I Have Ever Used
Source: Dev.to
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.
Getting Started
- Open Telegram and 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.
Basic Python Example
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", "**Hello** from Python!")Receiving Updates (Long 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"] + 1Sending Different Types of Content
# 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 Keyboard 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"}]
])Common Use Cases
- 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.
Feature Comparison
| 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 | 50 MB | 25 MB (free) | Varies |
| Webhook setup | 1 API call | Dashboard | Dashboard |