Spaces:
Build error
Build error
import os | |
import requests | |
from flask import Flask, request, jsonify | |
app = Flask(__name__) | |
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN") | |
if not TELEGRAM_BOT_TOKEN: | |
raise ValueError("TELEGRAM_BOT_TOKEN not set.") | |
TELEGRAM_API_URL = f"https://api.telegram.org/bot2051251535:TEST:OTk5MDA4ODgxLTAwNQ/" | |
def send_message(chat_id, text): | |
payload = {"chat_id": chat_id, "text": text} | |
try: | |
response = requests.post(TELEGRAM_API_URL + "sendMessage", json=payload) | |
response.raise_for_status() | |
except requests.exceptions.RequestException as e: | |
print(f"Send error: {e}") | |
def webhook(): | |
data = request.get_json() | |
message = data.get("message") | |
if message: | |
chat_id = message["chat"]["id"] | |
text = message.get("text", "") | |
if text: | |
send_message(chat_id, f"You said: {text}") | |
return jsonify(status="ok") | |
def health(): | |
return "Bot is alive!", 200 | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860))) | |