cevheri commited on
Commit
d00c9a8
·
verified ·
1 Parent(s): 356e28a

Update bot_telegram.py

Browse files
Files changed (1) hide show
  1. bot_telegram.py +15 -7
bot_telegram.py CHANGED
@@ -13,6 +13,7 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(
13
  # Load environment variables from Hugging Face Secrets
14
  BOT_TOKEN = os.getenv("BOT_TOKEN")
15
  BASE_URL = os.getenv("BASE_URL")
 
16
  API_USERNAME = os.getenv("API_USERNAME")
17
  API_PASSWORD = os.getenv("API_PASSWORD")
18
 
@@ -23,6 +24,7 @@ SECRET_PASSWORD = "secure123" # Change this to your desired password
23
  AUTHENTICATED_USERS = set()
24
  AWAITING_PASSWORD = set()
25
 
 
26
  class TelegramBot:
27
  """A Telegram bot with password-based authentication."""
28
 
@@ -132,11 +134,13 @@ class TelegramBot:
132
 
133
  user_message = update.message.text
134
 
 
135
  headers = {
136
- "Authorization": f"Bearer {self.auth_token}",
137
  "accept": "application/json"
138
  }
139
 
 
140
  json_payload = {"question": user_message}
141
  form_payload = {"question": user_message}
142
 
@@ -146,7 +150,8 @@ class TelegramBot:
146
 
147
  if response.status_code == 422:
148
  logging.warning("JSON format rejected. Retrying with form-data...")
149
- response = requests.post(self.ai_url, headers={**headers, "Content-Type": "application/x-www-form-urlencoded"}, data=form_payload)
 
150
 
151
  if response.status_code == 200:
152
  bot_reply = response.json().get("answer", "I didn't understand that.")
@@ -156,9 +161,11 @@ class TelegramBot:
156
  await self.chat_with_ai(update, context)
157
  return
158
  else:
 
159
  bot_reply = f"Error: {response.status_code}"
160
 
161
  except Exception as e:
 
162
  bot_reply = f"Connection error: {e}"
163
 
164
  await update.message.reply_text(bot_reply)
@@ -192,7 +199,7 @@ class TelegramBot:
192
  file_bytes = await file.download_as_bytearray()
193
 
194
  headers = {
195
- "Authorization": f"Bearer {self.auth_token}",
196
  "accept": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
197
  }
198
 
@@ -200,16 +207,16 @@ class TelegramBot:
200
  # Create form data with the file
201
  form_data = aiohttp.FormData()
202
  form_data.add_field('file',
203
- file_bytes,
204
- filename=document.file_name,
205
- content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
206
 
207
  # Send file to API
208
  async with session.post(self.excel_url, headers=headers, data=form_data) as response:
209
  if response.status == 200:
210
  # Get the Excel file content directly
211
  file_content = await response.read()
212
-
213
  # Send the Excel file back to Telegram
214
  await context.bot.send_document(
215
  chat_id=update.effective_chat.id,
@@ -242,6 +249,7 @@ class TelegramBot:
242
  logging.info("Starting Telegram bot...")
243
  self.app.run_polling()
244
 
 
245
  if __name__ == "__main__":
246
  bot = TelegramBot(
247
  bot_token=BOT_TOKEN,
 
13
  # Load environment variables from Hugging Face Secrets
14
  BOT_TOKEN = os.getenv("BOT_TOKEN")
15
  BASE_URL = os.getenv("BASE_URL")
16
+ HF_TOKEN = os.getenv("HF_TOKEN")
17
  API_USERNAME = os.getenv("API_USERNAME")
18
  API_PASSWORD = os.getenv("API_PASSWORD")
19
 
 
24
  AUTHENTICATED_USERS = set()
25
  AWAITING_PASSWORD = set()
26
 
27
+
28
  class TelegramBot:
29
  """A Telegram bot with password-based authentication."""
30
 
 
134
 
135
  user_message = update.message.text
136
 
137
+ hf_authorization = "Bearer " + HF_TOKEN
138
  headers = {
139
+ "Authorization": hf_authorization,
140
  "accept": "application/json"
141
  }
142
 
143
+
144
  json_payload = {"question": user_message}
145
  form_payload = {"question": user_message}
146
 
 
150
 
151
  if response.status_code == 422:
152
  logging.warning("JSON format rejected. Retrying with form-data...")
153
+ response = requests.post(self.ai_url, headers={**headers, "Content-Type": "application/x-www-form-urlencoded"},
154
+ data=form_payload)
155
 
156
  if response.status_code == 200:
157
  bot_reply = response.json().get("answer", "I didn't understand that.")
 
161
  await self.chat_with_ai(update, context)
162
  return
163
  else:
164
+ logging.error(f"Error: {response.status_code}")
165
  bot_reply = f"Error: {response.status_code}"
166
 
167
  except Exception as e:
168
+ logging.error(f"Connection error: {e}")
169
  bot_reply = f"Connection error: {e}"
170
 
171
  await update.message.reply_text(bot_reply)
 
199
  file_bytes = await file.download_as_bytearray()
200
 
201
  headers = {
202
+ "Authorization": f"Bearer {HF_TOKEN}",
203
  "accept": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
204
  }
205
 
 
207
  # Create form data with the file
208
  form_data = aiohttp.FormData()
209
  form_data.add_field('file',
210
+ file_bytes,
211
+ filename=document.file_name,
212
+ content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
213
 
214
  # Send file to API
215
  async with session.post(self.excel_url, headers=headers, data=form_data) as response:
216
  if response.status == 200:
217
  # Get the Excel file content directly
218
  file_content = await response.read()
219
+
220
  # Send the Excel file back to Telegram
221
  await context.bot.send_document(
222
  chat_id=update.effective_chat.id,
 
249
  logging.info("Starting Telegram bot...")
250
  self.app.run_polling()
251
 
252
+
253
  if __name__ == "__main__":
254
  bot = TelegramBot(
255
  bot_token=BOT_TOKEN,