Python Flask Webhook Bot
Use Python on a VPS, container or Python-capable hosting. The built-in dashboard runner does not launch persistent Python processes, so point Telegram to your Python service's public HTTPS webhook.
Deployment requirement
Your Python app must be hosted where a WSGI/ASGI process can run continuously. Do not upload app.py and expect the built-in PHP Start button to execute it.
01
Flask webhook app
Minimal production-minded structure with environment-based token loading.
app.py
import os
import requests
from flask import Flask, request, jsonify, abort
app = Flask(__name__)
TOKEN = os.environ["TELEGRAM_BOT_TOKEN"]
WEBHOOK_SECRET = os.environ["TELEGRAM_WEBHOOK_SECRET"]
API = f"https://api.telegram.org/bot{TOKEN}"
@app.post("/telegram/webhook/")
def telegram_webhook(secret: str):
if secret != WEBHOOK_SECRET:
abort(403)
update = request.get_json(silent=True) or {}
message = update.get("message") or {}
chat_id = (message.get("chat") or {}).get("id")
text = (message.get("text") or "").strip()
if chat_id and text == "/start":
requests.post(
f"{API}/sendMessage",
json={"chat_id": chat_id, "text": "Python bot is online ✅"},
timeout=10,
).raise_for_status()
return jsonify(ok=True)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
02
Dependencies
Pin versions in real projects after testing.
requirements.txt
Flask
requests
gunicorn03
Set Telegram webhook
Run once after your HTTPS service is live.
set_webhook.py
import os
import requests
TOKEN = os.environ["TELEGRAM_BOT_TOKEN"]
SECRET = os.environ["TELEGRAM_WEBHOOK_SECRET"]
PUBLIC_BASE_URL = "https://your-domain.example"
url = f"{PUBLIC_BASE_URL}/telegram/webhook/{SECRET}"
response = requests.post(
f"https://api.telegram.org/bot{TOKEN}/setWebhook",
data={"url": url},
timeout=15,
)
response.raise_for_status()
print(response.json())
04
Python deployment checklist
Required before you point Telegram at the service.