Azizbekuzr commited on
Commit
83261c4
·
verified ·
1 Parent(s): d9db20c

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +21 -14
bot.py CHANGED
@@ -8,30 +8,37 @@ GEMINI_API_KEY = "AIzaSyD64lJq-_9dYl21yCsUEuFeD0NHC8WNEoI"
8
 
9
  # GEMINI
10
  genai.configure(api_key=GEMINI_API_KEY)
 
11
  model = genai.GenerativeModel("gemini-2.0-flash")
12
 
13
  # FLASK
14
  app = Flask(__name__)
15
 
16
- # SEND MESSAGE
17
  def send_message(chat_id, text):
18
  url = f"https://api.telegram.org/bot{TOKEN}/sendMessage"
19
 
20
- data = {
21
  "chat_id": chat_id,
22
  "text": text
23
  }
24
 
25
- requests.post(url, json=data)
26
 
27
  # WEBHOOK
28
  @app.route(f"/{TOKEN}", methods=["POST"])
29
  def webhook():
30
- data = request.get_json()
 
 
 
 
 
 
 
 
31
 
32
- if "message" in data:
33
- chat_id = data["message"]["chat"]["id"]
34
- text = data["message"].get("text", "")
35
 
36
  # START
37
  if text == "/start":
@@ -41,17 +48,17 @@ def webhook():
41
  )
42
 
43
  else:
44
- try:
45
- response = model.generate_content(text)
46
 
47
- reply = response.text
48
 
49
- send_message(chat_id, reply)
50
 
51
- except Exception as e:
52
- send_message(chat_id, f"Xatolik: {e}")
53
 
54
- return "ok"
 
 
55
 
56
  # HOME
57
  @app.route("/")
 
8
 
9
  # GEMINI
10
  genai.configure(api_key=GEMINI_API_KEY)
11
+
12
  model = genai.GenerativeModel("gemini-2.0-flash")
13
 
14
  # FLASK
15
  app = Flask(__name__)
16
 
17
+ # TELEGRAM SEND
18
  def send_message(chat_id, text):
19
  url = f"https://api.telegram.org/bot{TOKEN}/sendMessage"
20
 
21
+ payload = {
22
  "chat_id": chat_id,
23
  "text": text
24
  }
25
 
26
+ requests.post(url, json=payload)
27
 
28
  # WEBHOOK
29
  @app.route(f"/{TOKEN}", methods=["POST"])
30
  def webhook():
31
+ try:
32
+ data = request.get_json()
33
+
34
+ if "message" not in data:
35
+ return "ok"
36
+
37
+ message = data["message"]
38
+
39
+ chat_id = message["chat"]["id"]
40
 
41
+ text = message.get("text", "")
 
 
42
 
43
  # START
44
  if text == "/start":
 
48
  )
49
 
50
  else:
51
+ response = model.generate_content(text)
 
52
 
53
+ reply = response.text
54
 
55
+ send_message(chat_id, reply)
56
 
57
+ return "ok"
 
58
 
59
+ except Exception as e:
60
+ print(e)
61
+ return "error"
62
 
63
  # HOME
64
  @app.route("/")