5
5
import json
6
6
import datetime
7
7
from dotenv import load_dotenv
8
- from telegram import ForceReply , Update , User # Import User
8
+ from telegram import ForceReply , Update , User
9
9
from telegram .ext import Application , CommandHandler , ContextTypes , MessageHandler , filters
10
- from telegram .constants import ParseMode
10
+ from telegram .constants import ParseMode , ChatAction
11
11
from pymongo import MongoClient
12
12
from pymongo .errors import ConnectionFailure , ConfigurationError
13
13
@@ -143,7 +143,6 @@ async def save_chat_data(chat_id: int, history: list, conversation_id: str | Non
143
143
logger .debug (f"Saved in-memory data for chat { chat_id_str } with user_info: { bool (user_info )} " )
144
144
145
145
146
-
147
146
async def start (update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
148
147
"""Send a message when the command /start is issued."""
149
148
user = update .effective_user
@@ -183,7 +182,7 @@ async def generate_answer(question: str, messages: list, conversation_id: str |
183
182
headers = {
184
183
"Content-Type" : "application/json; charset=utf-8"
185
184
}
186
- timeout = 120.0
185
+ timeout = 120.0 # Set a reasonable timeout for the API call
187
186
default_error_msg = "Sorry, I couldn't get an answer from the backend service."
188
187
189
188
try :
@@ -227,6 +226,13 @@ async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
227
226
question = update .message .text
228
227
logger .info (f"Received message from user { user .id } ({ user .username or user .first_name } ) in chat_id { chat_id } " )
229
228
229
+ # --- Send Typing Action ---
230
+ try :
231
+ await context .bot .send_chat_action (chat_id = chat_id , action = ChatAction .TYPING )
232
+ logger .debug (f"Sent typing action to chat { chat_id } " )
233
+ except Exception as e :
234
+ logger .warning (f"Failed to send typing action to chat { chat_id } : { e } " )
235
+ # This is non-critical, we can continue processing even if typing doesn't show
230
236
231
237
user_info_dict = {
232
238
"id" : user .id ,
@@ -244,6 +250,7 @@ async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
244
250
245
251
current_history .append ({"role" : "user" , "content" : question })
246
252
253
+ # This is the potentially long-running call that we show typing for
247
254
response_doc = await generate_answer (question , current_history , current_conversation_id )
248
255
answer = response_doc ["answer" ]
249
256
new_conversation_id = response_doc ["conversation_id" ] # Use the ID returned by API
@@ -252,6 +259,7 @@ async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
252
259
253
260
await save_chat_data (chat_id , current_history , new_conversation_id , user_info_dict )
254
261
262
+ # Sending the reply will automatically stop the typing indicator
255
263
try :
256
264
await update .message .reply_text (answer , parse_mode = ParseMode .MARKDOWN )
257
265
except Exception as e :
0 commit comments