Skip to content

Commit 2647660

Browse files
committed
Refactor message handling and model definitions: update message text handling in BaseHandler, enhance Context and Message models with optional fields, and streamline BotInput construction for asynchronous processing.
1 parent 976dc7b commit 2647660

10 files changed

Lines changed: 34 additions & 18 deletions

File tree

bot/models/base_handlers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ async def _log_handler_request(
7979
user_handle = person.user_handle
8080
chat_id = context.chat_id
8181
message_details = (
82-
f" with message of length {len(message.text)}" if message.text else ""
82+
f" with message of length {len(message.message_text)}"
83+
if message.message_text
84+
else ""
8385
)
8486
args_prompt = " with args: " + " ".join(args) if args else ""
8587
self.logger.info(
@@ -150,6 +152,7 @@ class CommandPriority(enum.IntEnum):
150152

151153
class BaseCommandHandler(BaseHandler, ABC):
152154
command: str = None
155+
list_priority_order: CommandPriority = CommandPriority.DEFAULT
153156

154157
def __str__(self) -> str:
155158
return f"Command Handler: {self.command}"

bot/models/config/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from .ai_config import AIConfig
21
from .tg_config import TGConfig
32
from .bot_config import BotConfig
43
from .localizer_translations import LocalizerTranslations

bot/models/handlers_input.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import io
22
from datetime import datetime
3-
from typing import Optional
3+
from typing import Optional, List
44

5-
from pydantic import BaseModel
5+
from pydantic import BaseModel, ConfigDict
66

77

88
class Person(BaseModel):
@@ -13,7 +13,7 @@ class Person(BaseModel):
1313

1414
class Context(BaseModel):
1515
chat_id: int
16-
chat_name: str
16+
chat_name: Optional[str] = None
1717
thread_id: Optional[int] = None
1818
is_group: bool = True
1919
is_bot_mentioned: bool = False
@@ -22,6 +22,8 @@ class Context(BaseModel):
2222

2323

2424
class Message(BaseModel):
25+
model_config = ConfigDict(arbitrary_types_allowed=True)
26+
2527
message_text: str
2628
timestamp: datetime
2729
in_file_image: Optional[io.BytesIO] = None
@@ -32,7 +34,7 @@ class BotInput(BaseModel):
3234
person: Person
3335
context: Context
3436
message: Message
35-
args: Optional[str] = None
37+
args: Optional[List[str]] = None
3638

3739

3840
class TranscribedMessage(BaseModel):

bot/models/handlers_response.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import io
22
from typing import Optional, Dict
33
from collections import OrderedDict
4-
from pydantic import BaseModel
4+
from pydantic import BaseModel, ConfigDict
55

66

77
class KeyboardResponse(BaseModel):
@@ -11,6 +11,8 @@ class KeyboardResponse(BaseModel):
1111

1212

1313
class CommandResponse(BaseModel):
14+
model_config = ConfigDict(arbitrary_types_allowed=True)
15+
1416
text: Optional[str] = None
1517
image_url: Optional[str] = None
1618
audio_bytes: Optional[io.BytesIO] = None
@@ -19,5 +21,7 @@ class CommandResponse(BaseModel):
1921

2022

2123
class LocalizedCommandResponse(BaseModel):
24+
model_config = ConfigDict(arbitrary_types_allowed=True)
25+
2226
localized_text: Optional[str] = None
2327
keyboard: Optional[KeyboardResponse] = None

bot/rp_bot/ai_agent/agent_tools/agent.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import io
22
from typing import AsyncIterator, Optional
3-
from pydantic import BaseModel, Field
3+
from pydantic import BaseModel, Field, ConfigDict
44
from omnimodkit import ModelsToolkit
55
from motor.motor_asyncio import AsyncIOMotorDatabase
66
from .agent_toolkit import AIAgentToolkit
@@ -9,6 +9,8 @@
99

1010

1111
class AIAgentStreamingResponse(BaseModel):
12+
model_config = ConfigDict(arbitrary_types_allowed=True)
13+
1214
text_chunk: str = Field(default="")
1315
total_text: str = Field(default="")
1416
image_url: Optional[str] = Field(default=None)

bot/rp_bot/ai_agent/agent_tools/autofact_generation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import List
2-
from langchain_core.pydantic_v1 import BaseModel, Field
2+
from pydantic import BaseModel, Field
33
from .base_tool import BaseTool
44

55

bot/rp_bot/bot.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from .commands import handlers as command_handlers
1212
from .callbacks import handlers as callback_handlers
1313
from .messages import handlers as message_handlers
14-
from .ai_agent.ai import AI
1514
from .db import DB
1615
from .auth import Auth
1716
from .prompt_manager import PromptManager

bot/rp_bot/db_models/chat_modes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import List
2-
from pydantic import BaseModel
2+
from pydantic import BaseModel, ConfigDict
33
from bson import ObjectId
44
from motor.motor_asyncio import AsyncIOMotorDatabase
55

@@ -10,6 +10,8 @@
1010

1111

1212
class ChatModeResponse(BaseModel):
13+
model_config = ConfigDict(arbitrary_types_allowed=True)
14+
1315
id: ObjectId
1416
mode_name: str
1517
mode_description: str

bot/telegram/bot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ async def handle(
110110
"""
111111
Handles the update and sends the response back to the user.
112112
"""
113-
bot_input = get_bot_input(update, context)
113+
bot_input = await get_bot_input(update, context)
114114
await self.push_state(update, context, "sending_text")
115115

116116
if bot_handler.streamable and self.telegram_bot_config.enable_message_streaming:
@@ -215,7 +215,7 @@ async def send_message(
215215
chat_id=chat_id,
216216
text=text,
217217
reply_to_message_id=reply_message_id,
218-
photo=image_url,
218+
# photo=image_url,
219219
parse_mode=parse_mode,
220220
reply_markup=markup,
221221
)

bot/telegram/utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ async def is_group_admin(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
1717
chat_id = update.effective_chat.id
1818
user_id = update.effective_user.id
1919

20+
# In private chats, the user is effectively the "admin" of their own chat
21+
if update.effective_chat.type == constants.ChatType.PRIVATE:
22+
return True
23+
24+
# For group chats, check actual administrators
2025
chat_administrators = await context.bot.get_chat_administrators(chat_id)
2126
return any(admin.user.id == user_id for admin in chat_administrators)
2227

@@ -134,15 +139,15 @@ def get_thread_id(update: Update) -> Optional[int]:
134139
return None
135140

136141

137-
def get_bot_input(update: Update, context: ContextTypes.DEFAULT_TYPE) -> BotInput:
142+
async def get_bot_input(update: Update, context: ContextTypes.DEFAULT_TYPE) -> BotInput:
138143
"""
139144
Get the bot input from the update and context
140145
"""
141146
return BotInput(
142-
person=get_person(update, context),
143-
context=get_context(update, context),
144-
message=get_message(update, context),
145-
args=get_args(update, context),
147+
person=await get_person(update, context),
148+
context=await get_context(update, context),
149+
message=await get_message(update, context),
150+
args=await get_args(update, context),
146151
)
147152

148153

0 commit comments

Comments
 (0)