Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V2] add inlinecallback to buttoncallback event #4252

Open
wants to merge 13 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
23 changes: 19 additions & 4 deletions client/src/telethon/_impl/client/events/queries.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Dict, Optional, Self
from typing import TYPE_CHECKING, Dict, Optional, Self, Union

from ...tl import abcs, functions, types
from ..types import Chat, Message
Expand All @@ -22,7 +22,7 @@ class ButtonCallback(Event):
def __init__(
self,
client: Client,
update: types.UpdateBotCallbackQuery,
update: Union[types.UpdateBotCallbackQuery, types.UpdateInlineBotCallbackQuery],
chat_map: Dict[int, Chat],
):
self._client = client
Expand All @@ -33,7 +33,10 @@ def __init__(
def _try_from_update(
cls, client: Client, update: abcs.Update, chat_map: Dict[int, Chat]
) -> Optional[Self]:
if isinstance(update, types.UpdateBotCallbackQuery) and update.data is not None:
if (
isinstance(update, (types.UpdateBotCallbackQuery, types.UpdateInlineBotCallbackQuery))
and update.data is not None
):
return cls._create(client, update, chat_map)
else:
return None
Expand All @@ -43,6 +46,16 @@ def data(self) -> bytes:
assert self._raw.data is not None
return self._raw.data

@property
def chat(self) -> Optional[Chat]:
"""
The :term:`chat` when the message was sent.
Only available if the event was triggered by a button under a usual message, not an inline one.
Lonami marked this conversation as resolved.
Show resolved Hide resolved
"""
if isinstance(self._raw, types.UpdateInlineBotCallbackQuery):
return None
return self._chat_map.get(peer_id(self._raw.peer))

async def answer(
self,
text: Optional[str] = None,
Expand Down Expand Up @@ -75,8 +88,10 @@ async def get_message(self) -> Optional[Message]:
"""
Get the :class:`~telethon.types.Message` containing the button that was clicked.

If the message is too old and is no longer accessible, :data:`None` is returned instead.
If the message is inline, or too old and is no longer accessible, :data:`None` is returned instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't the purpose of using InputMessageCallbackQuery the fact that it can work? functions.messages.get_messages doesn't need a chat. CherryPickedList does require one, so I see the problem.

But we should be able to fetch the message without a chat. So maybe we need to rethink this method's implementation.

"""
if isinstance(self._raw, types.UpdateInlineBotCallbackQuery):
return None

pid = peer_id(self._raw.peer)
chat = self._chat_map.get(pid)
Expand Down
2 changes: 1 addition & 1 deletion client/src/telethon/_impl/client/types/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _from_raw(
@property
def chat(self) -> Chat:
"""
The chat where messages are sent in this dialog.
The :term:`chat` where messages are sent in this dialog.
"""
return self._chat_map[peer_id(self._raw.peer)]

Expand Down
2 changes: 1 addition & 1 deletion client/src/telethon/_impl/client/types/draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _from_raw(
@property
def chat(self) -> Chat:
"""
The chat where the draft is saved.
The :term:`chat` where the draft is saved.

This is also the chat where the message will be sent to by :meth:`send`.
"""
Expand Down