Spaces:
Build error
Build error
Update app. py
Browse files
app. py
CHANGED
@@ -4,58 +4,34 @@ from flask import Flask, request, jsonify
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
-
# Get your Telegram Bot Token from environment variables
|
8 |
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
|
9 |
-
|
10 |
if not TELEGRAM_BOT_TOKEN:
|
11 |
-
raise ValueError("TELEGRAM_BOT_TOKEN
|
12 |
|
13 |
-
TELEGRAM_API_URL = f"https://api.telegram.org/bot{
|
14 |
|
15 |
def send_message(chat_id, text):
|
16 |
-
""
|
17 |
-
payload = {
|
18 |
-
"chat_id": chat_id,
|
19 |
-
"text": text
|
20 |
-
}
|
21 |
try:
|
22 |
response = requests.post(TELEGRAM_API_URL + "sendMessage", json=payload)
|
23 |
-
response.raise_for_status()
|
24 |
-
print(f"Message sent: {response.json()}")
|
25 |
except requests.exceptions.RequestException as e:
|
26 |
-
print(f"
|
27 |
|
28 |
@app.route("/", methods=["POST"])
|
29 |
-
def
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
if
|
36 |
-
|
37 |
-
|
38 |
-
message = update.get("message")
|
39 |
-
if message:
|
40 |
-
chat_id = message["chat"]["id"]
|
41 |
-
text = message.get("text")
|
42 |
-
|
43 |
-
if text:
|
44 |
-
# --- Your Bot's Logic Here ---
|
45 |
-
response_text = f"Hello from Hugging Face Space! You said: '{text}'"
|
46 |
-
send_message(chat_id, response_text)
|
47 |
-
else:
|
48 |
-
send_message(chat_id, "I only understand text messages for now.")
|
49 |
-
return jsonify({"status": "ok"}), 200
|
50 |
-
|
51 |
-
except Exception as e:
|
52 |
-
print(f"Error processing webhook: {e}")
|
53 |
-
return jsonify({"status": "error", "message": str(e)}), 500
|
54 |
|
55 |
@app.route("/", methods=["GET"])
|
56 |
-
def
|
57 |
-
"
|
58 |
-
return "Telegram Bot Space is running!", 200
|
59 |
|
60 |
if __name__ == "__main__":
|
61 |
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
|
|
7 |
TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
|
|
|
8 |
if not TELEGRAM_BOT_TOKEN:
|
9 |
+
raise ValueError("TELEGRAM_BOT_TOKEN not set.")
|
10 |
|
11 |
+
TELEGRAM_API_URL = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/"
|
12 |
|
13 |
def send_message(chat_id, text):
|
14 |
+
payload = {"chat_id": chat_id, "text": text}
|
|
|
|
|
|
|
|
|
15 |
try:
|
16 |
response = requests.post(TELEGRAM_API_URL + "sendMessage", json=payload)
|
17 |
+
response.raise_for_status()
|
|
|
18 |
except requests.exceptions.RequestException as e:
|
19 |
+
print(f"Send error: {e}")
|
20 |
|
21 |
@app.route("/", methods=["POST"])
|
22 |
+
def webhook():
|
23 |
+
data = request.get_json()
|
24 |
+
message = data.get("message")
|
25 |
+
if message:
|
26 |
+
chat_id = message["chat"]["id"]
|
27 |
+
text = message.get("text", "")
|
28 |
+
if text:
|
29 |
+
send_message(chat_id, f"You said: {text}")
|
30 |
+
return jsonify(status="ok")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
@app.route("/", methods=["GET"])
|
33 |
+
def health():
|
34 |
+
return "Bot is alive!", 200
|
|
|
35 |
|
36 |
if __name__ == "__main__":
|
37 |
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|