diff --git a/telebot/__init__.py b/telebot/__init__.py index c1aaec0..0579325 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -4521,7 +4521,8 @@ class TeleBot: is_personal: Optional[bool]=None, next_offset: Optional[str]=None, switch_pm_text: Optional[str]=None, - switch_pm_parameter: Optional[str]=None) -> bool: + switch_pm_parameter: Optional[str]=None, + button: Optional[types.InlineQueryResultsButton]=None) -> bool: """ Use this method to send answers to an inline query. On success, True is returned. No more than 50 results per query are allowed. @@ -4557,11 +4558,18 @@ class TeleBot: :param switch_pm_text: Parameter for the start message sent to the bot when user presses the switch button :type switch_pm_text: :obj:`str` + :param button: A JSON-serialized object describing a button to be shown above inline query results + :type button: :obj:`types.InlineQueryResultsButton` + :return: On success, True is returned. :rtype: :obj:`bool` """ + if not button and (switch_pm_text or switch_pm_parameter): + logger.warning("switch_pm_text and switch_pm_parameter are deprecated for answer_inline_query. Use button instead.") + button = types.InlineQueryResultsButton(text=switch_pm_text, start_parameter=switch_pm_parameter) + return apihelper.answer_inline_query(self.token, inline_query_id, results, cache_time, is_personal, next_offset, - switch_pm_text, switch_pm_parameter) + button) def answer_callback_query( self, callback_query_id: int, diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 57102f9..a07f1ef 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -1614,7 +1614,7 @@ def answer_callback_query(token, callback_query_id, text=None, show_alert=None, def answer_inline_query(token, inline_query_id, results, cache_time=None, is_personal=None, next_offset=None, - switch_pm_text=None, switch_pm_parameter=None): + button=None): method_url = 'answerInlineQuery' payload = {'inline_query_id': inline_query_id, 'results': _convert_list_json_serializable(results)} if cache_time is not None: @@ -1623,10 +1623,8 @@ def answer_inline_query(token, inline_query_id, results, cache_time=None, is_per payload['is_personal'] = is_personal if next_offset is not None: payload['next_offset'] = next_offset - if switch_pm_text: - payload['switch_pm_text'] = switch_pm_text - if switch_pm_parameter: - payload['switch_pm_parameter'] = switch_pm_parameter + if button is not None: + payload["button"] = button.to_json() return _make_request(token, method_url, params=payload, method='post') diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index a9d2ac8..1a86024 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -5371,7 +5371,8 @@ class AsyncTeleBot: is_personal: Optional[bool]=None, next_offset: Optional[str]=None, switch_pm_text: Optional[str]=None, - switch_pm_parameter: Optional[str]=None) -> bool: + switch_pm_parameter: Optional[str]=None, + button: Optional[types.InlineQueryResultsButton]=None) -> bool: """ Use this method to send answers to an inline query. On success, True is returned. No more than 50 results per query are allowed. @@ -5407,11 +5408,18 @@ class AsyncTeleBot: :param switch_pm_text: Parameter for the start message sent to the bot when user presses the switch button :type switch_pm_text: :obj:`str` + :param button: A JSON-serialized object describing a button to be shown above inline query results + :type button: :obj:`types.InlineQueryResultsButton` + :return: On success, True is returned. :rtype: :obj:`bool` """ + + if not button and (switch_pm_text or switch_pm_parameter): + logger.warning("switch_pm_text and switch_pm_parameter are deprecated for answer_inline_query. Use button instead.") + button = types.InlineQueryResultsButton(text=switch_pm_text, start_parameter=switch_pm_parameter) return await asyncio_helper.answer_inline_query(self.token, inline_query_id, results, cache_time, is_personal, next_offset, - switch_pm_text, switch_pm_parameter) + button) async def answer_callback_query( self, callback_query_id: int, diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index 507aec0..5a615b3 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -1604,7 +1604,7 @@ async def answer_callback_query(token, callback_query_id, text=None, show_alert= async def answer_inline_query(token, inline_query_id, results, cache_time=None, is_personal=None, next_offset=None, - switch_pm_text=None, switch_pm_parameter=None): + button=None): method_url = 'answerInlineQuery' payload = {'inline_query_id': inline_query_id, 'results': await _convert_list_json_serializable(results)} if cache_time is not None: @@ -1613,10 +1613,10 @@ async def answer_inline_query(token, inline_query_id, results, cache_time=None, payload['is_personal'] = is_personal if next_offset is not None: payload['next_offset'] = next_offset - if switch_pm_text: - payload['switch_pm_text'] = switch_pm_text - if switch_pm_parameter: - payload['switch_pm_parameter'] = switch_pm_parameter + if button is not None: + payload["button"] = button.to_json() + + return await _process_request(token, method_url, params=payload, method='post') diff --git a/telebot/types.py b/telebot/types.py index 8a4460e..9f5a507 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -7681,4 +7681,53 @@ class BotName(JsonDeserializable): return cls(**obj) def __init__(self, name: str): - self.name: str = name \ No newline at end of file + self.name: str = name + + +class InlineQueryResultsButton(JsonSerializable, Dictionaryable): + """ + This object represents a button to be shown above inline query results. + You must use exactly one of the optional fields. + + Telegram documentation: https://core.telegram.org/bots/api#inlinequeryresultsbutton + + :param text: Label text on the button + :type text: :obj:`str` + + :param web_app: Optional. Description of the Web App that will be launched when the user presses the button. + The Web App will be able to switch back to the inline mode using the method web_app_switch_inline_query inside the Web App. + :type web_app: :class:`telebot.types.WebAppInfo` + + :param start_parameter: Optional. Deep-linking parameter for the /start message sent to the bot when a user presses the button. + 1-64 characters, only A-Z, a-z, 0-9, _ and - are allowed. + Example: An inline bot that sends YouTube videos can ask the user to connect the bot to their YouTube account to adapt search + results accordingly. To do this, it displays a 'Connect your YouTube account' button above the results, or even before showing + any. The user presses the button, switches to a private chat with the bot and, in doing so, passes a start parameter that instructs + the bot to return an OAuth link. Once done, the bot can offer a switch_inline button so that the user can easily return to the chat + where they wanted to use the bot's inline capabilities. + :type start_parameter: :obj:`str` + + :return: Instance of the class + :rtype: :class:`InlineQueryResultsButton` + """ + + def __init__(self, text: str, web_app: Optional[WebAppInfo]=None, start_parameter: Optional[str]=None) -> None: + self.text: str = text + self.web_app: Optional[WebAppInfo] = web_app + self.start_parameter: Optional[str] = start_parameter + + + def to_dict(self) -> dict: + json_dict = { + 'text': self.text + } + + if self.web_app is not None: + json_dict['web_app'] = self.web_app.to_dict() + if self.start_parameter is not None: + json_dict['start_parameter'] = self.start_parameter + + return json_dict + + def to_json(self) -> str: + return json.dumps(self.to_dict()) \ No newline at end of file