Skip to content

Commit 3f93e38

Browse files
committed
feat: typing animation
1 parent de659fe commit 3f93e38

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

bot.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import json
66
import datetime
77
from dotenv import load_dotenv
8-
from telegram import ForceReply, Update, User # Import User
8+
from telegram import ForceReply, Update, User
99
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
10-
from telegram.constants import ParseMode
10+
from telegram.constants import ParseMode, ChatAction
1111
from pymongo import MongoClient
1212
from pymongo.errors import ConnectionFailure, ConfigurationError
1313

@@ -143,7 +143,6 @@ async def save_chat_data(chat_id: int, history: list, conversation_id: str | Non
143143
logger.debug(f"Saved in-memory data for chat {chat_id_str} with user_info: {bool(user_info)}")
144144

145145

146-
147146
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
148147
"""Send a message when the command /start is issued."""
149148
user = update.effective_user
@@ -183,7 +182,7 @@ async def generate_answer(question: str, messages: list, conversation_id: str |
183182
headers = {
184183
"Content-Type": "application/json; charset=utf-8"
185184
}
186-
timeout = 120.0
185+
timeout = 120.0 # Set a reasonable timeout for the API call
187186
default_error_msg = "Sorry, I couldn't get an answer from the backend service."
188187

189188
try:
@@ -227,6 +226,13 @@ async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
227226
question = update.message.text
228227
logger.info(f"Received message from user {user.id} ({user.username or user.first_name}) in chat_id {chat_id}")
229228

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
230236

231237
user_info_dict = {
232238
"id": user.id,
@@ -244,6 +250,7 @@ async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
244250

245251
current_history.append({"role": "user", "content": question})
246252

253+
# This is the potentially long-running call that we show typing for
247254
response_doc = await generate_answer(question, current_history, current_conversation_id)
248255
answer = response_doc["answer"]
249256
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:
252259

253260
await save_chat_data(chat_id, current_history, new_conversation_id, user_info_dict)
254261

262+
# Sending the reply will automatically stop the typing indicator
255263
try:
256264
await update.message.reply_text(answer, parse_mode=ParseMode.MARKDOWN)
257265
except Exception as e:

0 commit comments

Comments
 (0)