Spaces:
Paused
Paused
The 422 text meessge error fix
Browse files- bot_telegram.py +15 -4
bot_telegram.py
CHANGED
|
@@ -185,20 +185,31 @@ class TelegramBot:
|
|
| 185 |
try:
|
| 186 |
headers = {
|
| 187 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 188 |
-
"accept": "application/json"
|
|
|
|
| 189 |
}
|
| 190 |
|
| 191 |
-
#
|
|
|
|
| 192 |
response = requests.post(
|
| 193 |
self.ai_url,
|
| 194 |
headers=headers,
|
| 195 |
-
json=
|
| 196 |
)
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
if response.status_code == 200:
|
| 199 |
return response.json().get("answer", "No answer received")
|
| 200 |
else:
|
| 201 |
-
return f"API Error: {response.status_code}"
|
| 202 |
|
| 203 |
except Exception as e:
|
| 204 |
return f"Error: {str(e)}"
|
|
|
|
| 185 |
try:
|
| 186 |
headers = {
|
| 187 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 188 |
+
"accept": "application/json",
|
| 189 |
+
"Content-Type": "application/json"
|
| 190 |
}
|
| 191 |
|
| 192 |
+
# Try JSON format first
|
| 193 |
+
json_payload = {"question": user_message}
|
| 194 |
response = requests.post(
|
| 195 |
self.ai_url,
|
| 196 |
headers=headers,
|
| 197 |
+
json=json_payload
|
| 198 |
)
|
| 199 |
|
| 200 |
+
# If JSON fails, try form-data
|
| 201 |
+
if response.status_code == 422:
|
| 202 |
+
form_payload = {"question": user_message}
|
| 203 |
+
response = requests.post(
|
| 204 |
+
self.ai_url,
|
| 205 |
+
headers={**headers, "Content-Type": "application/x-www-form-urlencoded"},
|
| 206 |
+
data=form_payload
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
if response.status_code == 200:
|
| 210 |
return response.json().get("answer", "No answer received")
|
| 211 |
else:
|
| 212 |
+
return f"API Error: {response.status_code} - {response.text}"
|
| 213 |
|
| 214 |
except Exception as e:
|
| 215 |
return f"Error: {str(e)}"
|