File size: 1,101 Bytes
77f3809
 
 
 
 
 
 
 
7e75e75
77f3809
42cca33
77f3809
7e75e75
77f3809
 
7e75e75
77f3809
7e75e75
77f3809
 
7e75e75
 
 
 
 
 
 
 
 
77f3809
 
7e75e75
 
77f3809
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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}")

@app.route("/", methods=["POST"])
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")

@app.route("/", methods=["GET"])
def health():
    return "Bot is alive!", 200

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))