1- from telegram .models import CallbackQuery , Message , Update
1+ from telegram .models import Message , Update
22
33from service .models import Connection , MessageKeyboardButton , Trigger
44
@@ -34,14 +34,16 @@ def __init__(self, bot: Bot) -> None:
3434 async def _get_wait_trigger_connections (
3535 self , update : Update , context : HandlerContext
3636 ) -> list [Connection ] | None :
37+ message : Message | None = update .message
3738 user_storage : Storage | None = context .user_storage
3839
39- if not user_storage :
40- return None
41-
42- message : Message | None = update .effective_message
43-
44- if not message or not message .text :
40+ if not (
41+ message
42+ and (user := message .user )
43+ and user .id != self .bot .telegram_id
44+ and message .text
45+ and user_storage
46+ ):
4547 return None
4648
4749 trigger_id : int | None = await user_storage .get ('expected_trigger_id' )
@@ -50,32 +52,30 @@ async def _get_wait_trigger_connections(
5052 return None
5153
5254 trigger : Trigger = await self .bot .service .get_trigger (id = trigger_id )
53- connections : list [Connection ] = []
5455
55- if trigger .command and message .text .startswith ('/' ) and len (message .text ) > 1 :
56+ if TYPE_CHECKING :
57+ connections : list [Connection ]
58+
59+ if (
60+ (trigger_command := trigger .command )
61+ and message .text .startswith ('/' )
62+ and len (message .text ) > 1
63+ ):
5664 command , _ , payload = message .text .removeprefix ('/' ).partition (' ' )
5765
58- if (
59- trigger .command .payload and payload != trigger .command .payload
60- ) or command != trigger .command .command :
66+ if not (
67+ command == trigger_command .command
68+ and (not trigger_command .payload or payload == trigger_command .payload )
69+ ):
6170 return None
6271
6372 connections = trigger .source_connections
64- elif (
65- (
66- trigger .message
67- and trigger .message .text
68- and (
69- message .text
70- == (
71- await replace_text_variables (
72- trigger .message .text , context .variables
73- )
74- )
75- )
73+ elif (trigger_message := trigger .message ) and (
74+ not trigger_message .text
75+ or (
76+ message .text
77+ == await replace_text_variables (trigger_message .text , context .variables )
7678 )
77- or trigger .message
78- and not trigger .message .text
7979 ):
8080 connections = trigger .source_connections
8181 else :
@@ -136,9 +136,14 @@ async def _get_message_triggers(
136136 async def _get_trigger_connections (
137137 self , update : Update , context : HandlerContext
138138 ) -> list [Connection ] | None :
139- message : Message | None = update .effective_message
139+ message : Message | None = update .message
140140
141- if not message or not message .text :
141+ if not (
142+ message
143+ and (user := message .user )
144+ and user .id != self .bot .telegram_id
145+ and message .text
146+ ):
142147 return None
143148
144149 return list (
@@ -159,18 +164,19 @@ async def _get_trigger_connections(
159164 async def _get_command_keyboard_button_connections (
160165 self , update : Update , context : HandlerContext
161166 ) -> list [Connection ] | None :
162- event_message : Message | None = update .effective_message
163- callback_query : CallbackQuery | None = update .callback_query
164-
165167 buttons : list [MessageKeyboardButton ] = []
166168
167- if callback_query and callback_query .data and callback_query .data .isdigit ():
169+ if (
170+ (callback_query := update .callback_query )
171+ and callback_query .data
172+ and callback_query .data .isdigit ()
173+ ):
168174 buttons = await self .bot .service .get_messages_keyboard_buttons (
169175 id = int (callback_query .data )
170176 )
171- elif event_message and event_message .text :
177+ elif ( message := update . message ) and message .text :
172178 buttons = await self .bot .service .get_messages_keyboard_buttons (
173- text = event_message .text
179+ text = message .text
174180 )
175181 else :
176182 return None
0 commit comments