2016-04-12 06:23:52 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
2016-04-12 06:23:52 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2017-07-23 22:33:08 +02:00
|
|
|
"""This module contains an object that represents a Telegram InlineKeyboardMarkup."""
|
2024-10-24 20:48:49 +02:00
|
|
|
from collections.abc import Sequence
|
|
|
|
from typing import TYPE_CHECKING, Optional
|
2020-10-31 16:33:34 +01:00
|
|
|
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram._inline.inlinekeyboardbutton import InlineKeyboardButton
|
|
|
|
from telegram._telegramobject import TelegramObject
|
2022-01-19 18:00:21 +01:00
|
|
|
from telegram._utils.markup import check_keyboard_type
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram._utils.types import JSONDict
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram import Bot
|
2016-04-14 01:38:45 +02:00
|
|
|
|
|
|
|
|
2022-01-19 18:00:21 +01:00
|
|
|
class InlineKeyboardMarkup(TelegramObject):
|
2017-07-23 22:33:08 +02:00
|
|
|
"""
|
|
|
|
This object represents an inline keyboard that appears right next to the message it belongs to.
|
2016-04-21 14:21:12 +02:00
|
|
|
|
2020-07-14 21:33:56 +02:00
|
|
|
Objects of this class are comparable in terms of equality. Two objects of this class are
|
2022-01-19 18:00:21 +01:00
|
|
|
considered equal, if their size of :attr:`inline_keyboard` and all the buttons are equal.
|
2020-07-14 21:33:56 +02:00
|
|
|
|
2023-02-08 17:43:14 +01:00
|
|
|
.. figure:: https://core.telegram.org/file/464001863/110f3/I47qTXAD9Z4.120010/e0\
|
|
|
|
ea04f66357b640ec
|
|
|
|
:align: center
|
|
|
|
|
|
|
|
An inline keyboard on a message
|
|
|
|
|
|
|
|
.. seealso::
|
2024-07-12 16:33:42 +02:00
|
|
|
Another kind of keyboard would be the :class:`telegram.ReplyKeyboardMarkup`.
|
2023-02-08 17:43:14 +01:00
|
|
|
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
Examples:
|
|
|
|
* :any:`Inline Keyboard 1 <examples.inlinekeyboard>`
|
|
|
|
* :any:`Inline Keyboard 2 <examples.inlinekeyboard2>`
|
2022-08-27 11:46:51 +02:00
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
Args:
|
2022-12-15 15:00:36 +01:00
|
|
|
inline_keyboard (Sequence[Sequence[:class:`telegram.InlineKeyboardButton`]]): Sequence of
|
|
|
|
button rows, each represented by a sequence of :class:`~telegram.InlineKeyboardButton`
|
|
|
|
objects.
|
|
|
|
|
|
|
|
.. versionchanged:: 20.0
|
|
|
|
|sequenceclassargs|
|
2016-04-21 14:21:12 +02:00
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
Attributes:
|
2024-10-24 20:48:49 +02:00
|
|
|
inline_keyboard (tuple[tuple[:class:`telegram.InlineKeyboardButton`]]): Tuple of
|
2022-12-15 15:00:36 +01:00
|
|
|
button rows, each represented by a tuple of :class:`~telegram.InlineKeyboardButton`
|
|
|
|
objects.
|
|
|
|
|
|
|
|
.. versionchanged:: 20.0
|
|
|
|
|tupleclassattrs|
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2016-04-21 14:21:12 +02:00
|
|
|
"""
|
2016-04-14 01:38:45 +02:00
|
|
|
|
2021-08-19 22:01:10 +02:00
|
|
|
__slots__ = ("inline_keyboard",)
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2022-10-07 11:51:53 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-12-15 15:00:36 +01:00
|
|
|
inline_keyboard: Sequence[Sequence[InlineKeyboardButton]],
|
2022-10-07 11:51:53 +02:00
|
|
|
*,
|
2023-05-18 07:57:59 +02:00
|
|
|
api_kwargs: Optional[JSONDict] = None,
|
2022-10-07 11:51:53 +02:00
|
|
|
):
|
|
|
|
super().__init__(api_kwargs=api_kwargs)
|
2022-01-19 18:00:21 +01:00
|
|
|
if not check_keyboard_type(inline_keyboard):
|
2021-09-15 17:04:47 +02:00
|
|
|
raise ValueError(
|
2022-12-15 15:00:36 +01:00
|
|
|
"The parameter `inline_keyboard` should be a sequence of sequences of "
|
|
|
|
"InlineKeyboardButtons"
|
2021-09-15 17:04:47 +02:00
|
|
|
)
|
2016-04-14 01:38:45 +02:00
|
|
|
# Required
|
2024-10-24 20:48:49 +02:00
|
|
|
self.inline_keyboard: tuple[tuple[InlineKeyboardButton, ...], ...] = tuple(
|
2023-02-02 18:55:07 +01:00
|
|
|
tuple(row) for row in inline_keyboard
|
|
|
|
)
|
2016-04-14 01:38:45 +02:00
|
|
|
|
2021-04-30 10:09:21 +02:00
|
|
|
self._id_attrs = (self.inline_keyboard,)
|
|
|
|
|
2022-12-15 15:00:36 +01:00
|
|
|
self._freeze()
|
|
|
|
|
2019-08-23 21:20:41 +02:00
|
|
|
@classmethod
|
2024-07-01 19:59:54 +02:00
|
|
|
def de_json(
|
|
|
|
cls, data: Optional[JSONDict], bot: Optional["Bot"] = None
|
|
|
|
) -> Optional["InlineKeyboardMarkup"]:
|
2021-05-27 09:38:17 +02:00
|
|
|
"""See :meth:`telegram.TelegramObject.de_json`."""
|
2019-08-23 21:20:41 +02:00
|
|
|
if not data:
|
|
|
|
return None
|
2020-10-06 19:28:40 +02:00
|
|
|
|
2019-08-23 21:20:41 +02:00
|
|
|
keyboard = []
|
|
|
|
for row in data["inline_keyboard"]:
|
|
|
|
tmp = []
|
|
|
|
for col in row:
|
2020-10-06 19:28:40 +02:00
|
|
|
btn = InlineKeyboardButton.de_json(col, bot)
|
|
|
|
if btn:
|
|
|
|
tmp.append(btn)
|
2019-08-23 21:20:41 +02:00
|
|
|
keyboard.append(tmp)
|
|
|
|
|
|
|
|
return cls(keyboard)
|
|
|
|
|
2019-01-04 21:04:45 +01:00
|
|
|
@classmethod
|
2021-01-30 11:38:54 +01:00
|
|
|
def from_button(cls, button: InlineKeyboardButton, **kwargs: object) -> "InlineKeyboardMarkup":
|
2019-01-04 21:04:45 +01:00
|
|
|
"""Shortcut for::
|
|
|
|
|
|
|
|
InlineKeyboardMarkup([[button]], **kwargs)
|
|
|
|
|
|
|
|
Return an InlineKeyboardMarkup from a single InlineKeyboardButton
|
|
|
|
|
|
|
|
Args:
|
|
|
|
button (:class:`telegram.InlineKeyboardButton`): The button to use in the markup
|
|
|
|
|
|
|
|
"""
|
2022-10-07 11:51:53 +02:00
|
|
|
return cls([[button]], **kwargs) # type: ignore[arg-type]
|
2019-01-04 21:04:45 +01:00
|
|
|
|
|
|
|
@classmethod
|
2020-10-06 19:28:40 +02:00
|
|
|
def from_row(
|
2023-01-01 14:24:30 +01:00
|
|
|
cls, button_row: Sequence[InlineKeyboardButton], **kwargs: object
|
2020-10-06 19:28:40 +02:00
|
|
|
) -> "InlineKeyboardMarkup":
|
2019-01-04 21:04:45 +01:00
|
|
|
"""Shortcut for::
|
|
|
|
|
|
|
|
InlineKeyboardMarkup([button_row], **kwargs)
|
|
|
|
|
|
|
|
Return an InlineKeyboardMarkup from a single row of InlineKeyboardButtons
|
|
|
|
|
|
|
|
Args:
|
2023-01-01 14:24:30 +01:00
|
|
|
button_row (Sequence[:class:`telegram.InlineKeyboardButton`]): The button to use
|
|
|
|
in the markup
|
|
|
|
|
|
|
|
.. versionchanged:: 20.0
|
|
|
|
|sequenceargs|
|
2019-01-04 21:04:45 +01:00
|
|
|
|
|
|
|
"""
|
2022-10-07 11:51:53 +02:00
|
|
|
return cls([button_row], **kwargs) # type: ignore[arg-type]
|
2019-01-04 21:04:45 +01:00
|
|
|
|
|
|
|
@classmethod
|
2020-10-06 19:28:40 +02:00
|
|
|
def from_column(
|
2023-01-01 14:24:30 +01:00
|
|
|
cls, button_column: Sequence[InlineKeyboardButton], **kwargs: object
|
2020-10-06 19:28:40 +02:00
|
|
|
) -> "InlineKeyboardMarkup":
|
2019-01-04 21:04:45 +01:00
|
|
|
"""Shortcut for::
|
|
|
|
|
|
|
|
InlineKeyboardMarkup([[button] for button in button_column], **kwargs)
|
|
|
|
|
|
|
|
Return an InlineKeyboardMarkup from a single column of InlineKeyboardButtons
|
|
|
|
|
|
|
|
Args:
|
2023-01-01 14:24:30 +01:00
|
|
|
button_column (Sequence[:class:`telegram.InlineKeyboardButton`]): The button to use
|
|
|
|
in the markup
|
|
|
|
|
|
|
|
.. versionchanged:: 20.0
|
|
|
|
|sequenceargs|
|
2019-01-04 21:04:45 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
button_grid = [[button] for button in button_column]
|
2022-10-07 11:51:53 +02:00
|
|
|
return cls(button_grid, **kwargs) # type: ignore[arg-type]
|