Update bot_telegram.py
Browse files- bot_telegram.py +39 -35
bot_telegram.py
CHANGED
|
@@ -105,53 +105,57 @@ 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 |
-
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
|
| 122 |
-
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
}
|
| 129 |
|
| 130 |
-
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 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 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
|
|
|
| 154 |
|
|
|
|
| 155 |
|
| 156 |
def setup_handlers(self):
|
| 157 |
"""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 |
+
"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."""
|