Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,8 @@ def me(self) -> User:

async def feed_webhook_update(self, update: Update) -> None:
if not (
(
(user := update.effective_user)
and (user.is_bot or await is_valid_user(self, user=user))
and (message := update.effective_message)
and message.text
)
or update.callback_query
or update.pre_checkout_query
(user := update.effective_user)
and (user.id == self.telegram_id or await is_valid_user(self, user=user))
):
return

Expand Down
74 changes: 40 additions & 34 deletions bot/handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from telegram.models import CallbackQuery, Message, Update
from telegram.models import Message, Update

from service.models import Connection, MessageKeyboardButton, Trigger

Expand Down Expand Up @@ -34,14 +34,16 @@ def __init__(self, bot: Bot) -> None:
async def _get_wait_trigger_connections(
self, update: Update, context: HandlerContext
) -> list[Connection] | None:
message: Message | None = update.message
user_storage: Storage | None = context.user_storage

if not user_storage:
return None

message: Message | None = update.effective_message

if not message or not message.text:
if not (
message
and (user := message.user)
and user.id != self.bot.telegram_id
and message.text
and user_storage
):
return None

trigger_id: int | None = await user_storage.get('expected_trigger_id')
Expand All @@ -50,32 +52,30 @@ async def _get_wait_trigger_connections(
return None

trigger: Trigger = await self.bot.service.get_trigger(id=trigger_id)
connections: list[Connection] = []

if trigger.command and message.text.startswith('/') and len(message.text) > 1:
if TYPE_CHECKING:
connections: list[Connection]

if (
(trigger_command := trigger.command)
and message.text.startswith('/')
and len(message.text) > 1
):
command, _, payload = message.text.removeprefix('/').partition(' ')

if (
trigger.command.payload and payload != trigger.command.payload
) or command != trigger.command.command:
if not (
command == trigger_command.command
and (not trigger_command.payload or payload == trigger_command.payload)
):
return None

connections = trigger.source_connections
elif (
(
trigger.message
and trigger.message.text
and (
message.text
== (
await replace_text_variables(
trigger.message.text, context.variables
)
)
)
elif (trigger_message := trigger.message) and (
not trigger_message.text
or (
message.text
== await replace_text_variables(trigger_message.text, context.variables)
)
or trigger.message
and not trigger.message.text
):
connections = trigger.source_connections
else:
Expand Down Expand Up @@ -136,9 +136,14 @@ async def _get_message_triggers(
async def _get_trigger_connections(
self, update: Update, context: HandlerContext
) -> list[Connection] | None:
message: Message | None = update.effective_message
message: Message | None = update.message

if not message or not message.text:
if not (
message
and (user := message.user)
and user.id != self.bot.telegram_id
and message.text
):
return None

return list(
Expand All @@ -159,18 +164,19 @@ async def _get_trigger_connections(
async def _get_command_keyboard_button_connections(
self, update: Update, context: HandlerContext
) -> list[Connection] | None:
event_message: Message | None = update.effective_message
callback_query: CallbackQuery | None = update.callback_query

buttons: list[MessageKeyboardButton] = []

if callback_query and callback_query.data and callback_query.data.isdigit():
if (
(callback_query := update.callback_query)
and callback_query.data
and callback_query.data.isdigit()
):
buttons = await self.bot.service.get_messages_keyboard_buttons(
id=int(callback_query.data)
)
elif event_message and event_message.text:
elif (message := update.message) and message.text:
buttons = await self.bot.service.get_messages_keyboard_buttons(
text=event_message.text
text=message.text
)
else:
return None
Expand Down
36 changes: 20 additions & 16 deletions bot/handlers/message/handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from telegram.constants import MediaGroupLimit
from telegram.enums import InputMediaType
from telegram.models import Chat, Message, ReplyParameters, Update, User
from telegram.models import Chat, Message, ReplyParameters, Update
from telegram.types import KeyboardMarkup

from service.models import Connection
Expand Down Expand Up @@ -100,14 +100,14 @@ async def _send_media_group(
async def _process_message(
self,
chat: Chat,
event_message_id: int | None,
reply_to_event_message_id: int | None,
message: ServiceMessage,
chat_storage: Storage,
variables: Variables,
) -> None:
reply_parameters: ReplyParameters | None = (
ReplyParameters(message_id=event_message_id)
if message.settings.reply_to_user_message and event_message_id
ReplyParameters(message_id=reply_to_event_message_id)
if message.settings.reply_to_user_message and reply_to_event_message_id
else None
)
media: Media = {
Expand Down Expand Up @@ -162,20 +162,28 @@ async def handle(
self, update: Update, message: ServiceMessage, context: HandlerContext
) -> list[Connection] | None:
chat: Chat | None = update.effective_chat
user: User | None = update.effective_user
chat_storage: Storage | None = context.chat_storage

if not chat or not user or not chat_storage:
if not (chat and chat_storage):
return None

event_message: Message | None = update.effective_message
event_message_id: int | None = (
event_message.message_id if event_message else None
reply_to_event_message_id: int | None = (
event_message.message_id
if (
(event_message := update.effective_message)
and (event_message_user := event_message and event_message.user)
and event_message_user.id != self.bot.telegram_id
)
else None
)

tasks: list[Awaitable[Any]] = [
self._process_message(
chat, event_message_id, message, chat_storage, context.variables
chat,
reply_to_event_message_id,
message,
chat_storage,
context.variables,
)
]

Expand All @@ -184,11 +192,7 @@ async def handle(

await asyncio.gather(*tasks)

if (
message.settings.delete_user_message
and event_message_id
and not user.is_bot
):
await self.bot.telegram.delete_message(chat.id, event_message_id)
if message.settings.delete_user_message and reply_to_event_message_id:
await self.bot.telegram.delete_message(chat.id, reply_to_event_message_id)

return message.source_connections
Loading