Reading — step 1 of 5
Read
~1 min readState & Storage
Webhooks vs Polling
Two ways to receive events:
Polling:
python
Pros:
- Simple. No public IP needed.
- Works behind NAT.
- Good for development.
Cons:
- Latency: 1+ seconds.
- Wasteful: many empty polls.
- Rate limit on polls themselves.
Long polling (Telegram default):
python
Server holds connection up to 30s, returns when updates exist or timeout. Lower latency, fewer requests.
Webhooks:
python
Pros:
- Instant: server pushes immediately.
- Efficient: no polling.
- Scales to thousands of bots without polling each.
Cons:
- Need public HTTPS endpoint.
- Need TLS cert.
- Failure handling: returning 500 = redelivery (or loss, depending).
Setup:
python
Telegram POSTs updates. Bot processes + returns 200.
Verification:
- secret_token in headers.
- IP whitelist (Telegram has known IPs).
- Verify signature where supported.
WebSocket:
- Discord uses WebSocket.
- Persistent connection.
- Heartbeat to keep alive.
- Reconnect on disconnect.
- Auto-handled by SDKs.
python
Hybrid: receive via webhook, send via REST API.
Local development:
- Webhook needs public URL. Use ngrok / cloudflared tunnel.
- Or use polling locally + webhook in production.
Modern bot frameworks abstract this. Same code works for both.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…