Skip to content

Commit b52ea26

Browse files
authored
Merge pull request #3 from John-Lin/fix-type-folded-msg
fix: type and folded message
2 parents 71d0bc4 + 5fde8a2 commit b52ea26

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

bot/telegram.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import logging
44
from typing import Any
55

6+
from telegram import ReplyParameters
67
from telegram import Update
8+
from telegram.constants import ParseMode
79
from telegram.ext import Application
810
from telegram.ext import CommandHandler
911
from telegram.ext import ContextTypes
@@ -34,6 +36,7 @@ async def run(self) -> None:
3436
# inits bot, update, persistence
3537
await self.application.initialize()
3638
await self.application.start()
39+
assert self.application.updater is not None, "Updater is None"
3740
await self.application.updater.start_polling(allowed_updates=Update.ALL_TYPES)
3841
await self.initialize_agent()
3942

@@ -48,6 +51,7 @@ async def run(self) -> None:
4851
async def cleanup(self) -> None:
4952
"""Clean up resources."""
5053
try:
54+
assert self.application.updater is not None
5155
await self.application.updater.stop()
5256
await self.application.stop()
5357
await self.application.shutdown()
@@ -65,16 +69,24 @@ async def initialize_agent(self) -> None:
6569

6670
async def help_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
6771
"""Send a message when the command /help is issued."""
72+
if update.message is None:
73+
return
74+
# chat_id = update.message.chat_id
6875
await update.message.reply_text("Help!")
6976

7077
async def handle_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
7178
await self._procress_message(update, context)
7279

7380
async def _procress_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
7481
"""Process incoming messages and generate responses."""
75-
# Get or create conversation context
82+
83+
if update.message is None or update.effective_chat is None:
84+
logging.warning("Update has no message or chat — ignored")
85+
return
86+
7687
chat_id = update.message.chat_id
7788

89+
# Get or create conversation context
7890
if chat_id not in self.conversations:
7991
self.conversations[chat_id] = {"messages": []}
8092

@@ -100,7 +112,16 @@ async def _procress_message(self, update: Update, context: ContextTypes.DEFAULT_
100112

101113
# Add assistant response to conversation history
102114
self.conversations[chat_id]["messages"].append({"role": "assistant", "content": agent_resp})
103-
await update.message.reply_text(agent_resp)
115+
if len(agent_resp) < 200:
116+
await update.message.reply_text(text=agent_resp, do_quote=True)
117+
else:
118+
# Send the response in a quote block
119+
await context.bot.send_message(
120+
chat_id=update.effective_chat.id,
121+
text=f"<blockquote expandable>{agent_resp}</blockquote>",
122+
parse_mode=ParseMode.HTML,
123+
reply_parameters=ReplyParameters(message_id=update.message.message_id),
124+
)
104125
except Exception as e:
105126
error_message = f"I'm sorry, I encountered an error: {str(e)}"
106127
logging.error(f"Error processing message: {e}", exc_info=True)

0 commit comments

Comments
 (0)