baha-99 commited on
Commit
b3f60f7
·
verified ·
1 Parent(s): 6d9d48c

Update bot_telegram.py

Browse files
Files changed (1) hide show
  1. bot_telegram.py +35 -39
bot_telegram.py CHANGED
@@ -105,57 +105,53 @@ class TelegramBot:
105
  await update.message.reply_text("❌ Wrong password. Try again.")
106
 
107
  async def chat_with_ai(self, update: Update, context: CallbackContext):
108
- """Handles messages and sends them to the AI API."""
109
- user_id = update.message.from_user.id
110
 
111
- if user_id not in AUTHENTICATED_USERS:
112
- await update.message.reply_text("❌ You are not authenticated. Please enter the password first.")
113
- return
114
 
115
- if not self.auth_token:
116
- self.authenticate()
117
 
118
- if not self.auth_token:
119
- await update.message.reply_text("Authentication failed. Please try again later.")
120
- return
121
 
122
- user_message = update.message.text
123
 
124
- headers = {
125
- "Authorization": f"Bearer {self.auth_token}",
126
- "accept": "application/json"
127
- }
 
128
 
129
- json_payload = {"question": user_message}
130
- form_payload = {"question": user_message}
131
 
132
- try:
133
- logging.info(f"Sending payload as JSON: {json_payload}")
134
- response = requests.post(self.ai_url, headers={**headers, "Content-Type": "application/json"}, json=json_payload)
135
 
136
- if response.status_code == 422:
137
- logging.warning("JSON format rejected. Retrying with form-data...")
138
- response = requests.post(self.ai_url, headers={**headers, "Content-Type": "application/x-www-form-urlencoded"}, data=form_payload)
139
 
140
- logging.info(f"Response status: {response.status_code}")
141
- logging.info(f"Response content: {response.text}")
 
 
 
 
 
 
 
 
 
142
 
143
- if response.status_code == 200:
144
- bot_reply = response.json().get("answer", "I didn't understand that.")
145
- elif response.status_code == 401:
146
- logging.warning("Authorization expired. Re-authenticating...")
147
- self.authenticate()
148
- await self.chat_with_ai(update, context)
149
- return
150
- elif response.status_code == 422:
151
- bot_reply = "Error: The API rejected the request. Check payload format."
152
- else:
153
- bot_reply = f"Error: {response.status_code} - {response.text}"
154
 
155
- except Exception as e:
156
- bot_reply = f"Connection error: {e}"
157
 
158
- await update.message.reply_text(bot_reply)
159
 
160
  def setup_handlers(self):
161
  """Set up Telegram command and message handlers."""
 
105
  await update.message.reply_text("❌ Wrong password. Try again.")
106
 
107
  async def chat_with_ai(self, update: Update, context: CallbackContext):
108
+ """Handles messages and sends them to the AI API."""
109
+ user_id = update.message.from_user.id
110
 
111
+ if user_id not in AUTHENTICATED_USERS:
112
+ await update.message.reply_text("❌ You are not authenticated. Please enter the password first.")
113
+ return
114
 
115
+ if not self.auth_token:
116
+ self.authenticate()
117
 
118
+ if not self.auth_token:
119
+ await update.message.reply_text("Authentication failed. Please try again later.")
120
+ return
121
 
122
+ user_message = update.message.text
123
 
124
+ headers = {
125
+ "Authorization": f"Bearer {self.auth_token}",
126
+ "Content-Type": "application/x-www-form-urlencoded", # Always use form-data
127
+ "accept": "application/json"
128
+ }
129
 
130
+ form_payload = {"question": user_message} # Only use form-data
 
131
 
132
+ try:
133
+ response = requests.post(self.ai_url, headers=headers, data=form_payload)
 
134
 
135
+ logging.info(f"Response status: {response.status_code}")
136
+ logging.info(f"Response content: {response.text}")
 
137
 
138
+ if response.status_code == 200:
139
+ bot_reply = response.json().get("answer", "I didn't understand that.")
140
+ elif response.status_code == 401:
141
+ logging.warning("Authorization expired. Re-authenticating...")
142
+ self.authenticate()
143
+ await self.chat_with_ai(update, context)
144
+ return
145
+ elif response.status_code == 422:
146
+ bot_reply = "Error: The API rejected the request. Check payload format."
147
+ else:
148
+ bot_reply = f"Error: {response.status_code} - {response.text}"
149
 
150
+ except Exception as e:
151
+ bot_reply = f"Connection error: {e}"
 
 
 
 
 
 
 
 
 
152
 
153
+ await update.message.reply_text(bot_reply)
 
154
 
 
155
 
156
  def setup_handlers(self):
157
  """Set up Telegram command and message handlers."""