DmitrMakeev commited on
Commit
72be308
·
verified ·
1 Parent(s): 5e33196

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -7
app.py CHANGED
@@ -11,8 +11,8 @@ import globs
11
  from api_logic import api
12
  import requests
13
 
14
-
15
-
16
  load_dotenv()
17
 
18
  # Инициализация базы данных
@@ -101,11 +101,33 @@ app.config['DEBUG'] = True
101
  TELEGRAM_BOT_TOKEN = "7766407698:AAGZHEbUuiOri4_YzZ7hDPSD6U8MGMXXSnA"
102
 
103
  def send_message(chat_id, text):
104
- """Отправка текстового сообщения в Telegram."""
105
- url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
106
- data = {"chat_id": chat_id, "text": text}
107
- response = requests.post(url, data=data)
108
- print(f"Ответ Telegram: {response.text}") # Логируем текстовый ответ для отладки
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  @app.route("/webhook", methods=["POST"])
111
  def process():
 
11
  from api_logic import api
12
  import requests
13
 
14
+ import http.client
15
+ import json
16
  load_dotenv()
17
 
18
  # Инициализация базы данных
 
101
  TELEGRAM_BOT_TOKEN = "7766407698:AAGZHEbUuiOri4_YzZ7hDPSD6U8MGMXXSnA"
102
 
103
  def send_message(chat_id, text):
104
+ """Отправка текстового сообщения в Telegram с использованием http.client."""
105
+ conn = http.client.HTTPSConnection("api.telegram.org")
106
+
107
+ # Подготовка данных
108
+ payload = json.dumps({
109
+ "chat_id": chat_id,
110
+ "text": text
111
+ })
112
+
113
+ # Заголовки для POST-запроса
114
+ headers = {
115
+ 'Content-Type': 'application/json'
116
+ }
117
+
118
+ # Формируем URL для API
119
+ url = f"/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
120
+
121
+ # Отправка запроса
122
+ conn.request("POST", url, body=payload, headers=headers)
123
+
124
+ # Получение ответа
125
+ response = conn.getresponse()
126
+ data = response.read()
127
+
128
+ # Логируем текстовый ответ для отладки
129
+ print(f"Ответ Telegram: {data.decode('utf-8')}")
130
+ conn.close()
131
 
132
  @app.route("/webhook", methods=["POST"])
133
  def process():