Skip to content

Commit 26575dc

Browse files
Added support for launching Web Apps from inline query results by replacing the parameters switch_pm_text and switch_pm_parameter of the method answerInlineQuery with the parameter button of type InlineQueryResultsButton.
1 parent be69feb commit 26575dc

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

telebot/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4521,7 +4521,8 @@ def answer_inline_query(
45214521
is_personal: Optional[bool]=None,
45224522
next_offset: Optional[str]=None,
45234523
switch_pm_text: Optional[str]=None,
4524-
switch_pm_parameter: Optional[str]=None) -> bool:
4524+
switch_pm_parameter: Optional[str]=None,
4525+
button: Optional[types.InlineQueryResultsButton]=None) -> bool:
45254526
"""
45264527
Use this method to send answers to an inline query. On success, True is returned.
45274528
No more than 50 results per query are allowed.
@@ -4557,11 +4558,18 @@ def answer_inline_query(
45574558
:param switch_pm_text: Parameter for the start message sent to the bot when user presses the switch button
45584559
:type switch_pm_text: :obj:`str`
45594560
4561+
:param button: A JSON-serialized object describing a button to be shown above inline query results
4562+
:type button: :obj:`types.InlineQueryResultsButton`
4563+
45604564
:return: On success, True is returned.
45614565
:rtype: :obj:`bool`
45624566
"""
4567+
if not button and (switch_pm_text or switch_pm_parameter):
4568+
logger.warning("switch_pm_text and switch_pm_parameter are deprecated for answer_inline_query. Use button instead.")
4569+
button = types.InlineQueryResultsButton(text=switch_pm_text, start_parameter=switch_pm_parameter)
4570+
45634571
return apihelper.answer_inline_query(self.token, inline_query_id, results, cache_time, is_personal, next_offset,
4564-
switch_pm_text, switch_pm_parameter)
4572+
button)
45654573

45664574
def answer_callback_query(
45674575
self, callback_query_id: int,

telebot/apihelper.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ def answer_callback_query(token, callback_query_id, text=None, show_alert=None,
16141614

16151615

16161616
def answer_inline_query(token, inline_query_id, results, cache_time=None, is_personal=None, next_offset=None,
1617-
switch_pm_text=None, switch_pm_parameter=None):
1617+
button=None):
16181618
method_url = 'answerInlineQuery'
16191619
payload = {'inline_query_id': inline_query_id, 'results': _convert_list_json_serializable(results)}
16201620
if cache_time is not None:
@@ -1623,10 +1623,8 @@ def answer_inline_query(token, inline_query_id, results, cache_time=None, is_per
16231623
payload['is_personal'] = is_personal
16241624
if next_offset is not None:
16251625
payload['next_offset'] = next_offset
1626-
if switch_pm_text:
1627-
payload['switch_pm_text'] = switch_pm_text
1628-
if switch_pm_parameter:
1629-
payload['switch_pm_parameter'] = switch_pm_parameter
1626+
if button is not None:
1627+
payload["button"] = button.to_json()
16301628
return _make_request(token, method_url, params=payload, method='post')
16311629

16321630

telebot/async_telebot.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5371,7 +5371,8 @@ async def answer_inline_query(
53715371
is_personal: Optional[bool]=None,
53725372
next_offset: Optional[str]=None,
53735373
switch_pm_text: Optional[str]=None,
5374-
switch_pm_parameter: Optional[str]=None) -> bool:
5374+
switch_pm_parameter: Optional[str]=None,
5375+
button: Optional[types.InlineQueryResultsButton]=None) -> bool:
53755376
"""
53765377
Use this method to send answers to an inline query. On success, True is returned.
53775378
No more than 50 results per query are allowed.
@@ -5407,11 +5408,18 @@ async def answer_inline_query(
54075408
:param switch_pm_text: Parameter for the start message sent to the bot when user presses the switch button
54085409
:type switch_pm_text: :obj:`str`
54095410
5411+
:param button: A JSON-serialized object describing a button to be shown above inline query results
5412+
:type button: :obj:`types.InlineQueryResultsButton`
5413+
54105414
:return: On success, True is returned.
54115415
:rtype: :obj:`bool`
54125416
"""
5417+
5418+
if not button and (switch_pm_text or switch_pm_parameter):
5419+
logger.warning("switch_pm_text and switch_pm_parameter are deprecated for answer_inline_query. Use button instead.")
5420+
button = types.InlineQueryResultsButton(text=switch_pm_text, start_parameter=switch_pm_parameter)
54135421
return await asyncio_helper.answer_inline_query(self.token, inline_query_id, results, cache_time, is_personal, next_offset,
5414-
switch_pm_text, switch_pm_parameter)
5422+
button)
54155423

54165424
async def answer_callback_query(
54175425
self, callback_query_id: int,

telebot/asyncio_helper.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ async def answer_callback_query(token, callback_query_id, text=None, show_alert=
16041604

16051605

16061606
async def answer_inline_query(token, inline_query_id, results, cache_time=None, is_personal=None, next_offset=None,
1607-
switch_pm_text=None, switch_pm_parameter=None):
1607+
button=None):
16081608
method_url = 'answerInlineQuery'
16091609
payload = {'inline_query_id': inline_query_id, 'results': await _convert_list_json_serializable(results)}
16101610
if cache_time is not None:
@@ -1613,10 +1613,10 @@ async def answer_inline_query(token, inline_query_id, results, cache_time=None,
16131613
payload['is_personal'] = is_personal
16141614
if next_offset is not None:
16151615
payload['next_offset'] = next_offset
1616-
if switch_pm_text:
1617-
payload['switch_pm_text'] = switch_pm_text
1618-
if switch_pm_parameter:
1619-
payload['switch_pm_parameter'] = switch_pm_parameter
1616+
if button is not None:
1617+
payload["button"] = button.to_json()
1618+
1619+
16201620
return await _process_request(token, method_url, params=payload, method='post')
16211621

16221622

telebot/types.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7681,4 +7681,53 @@ def de_json(cls, json_string):
76817681
return cls(**obj)
76827682

76837683
def __init__(self, name: str):
7684-
self.name: str = name
7684+
self.name: str = name
7685+
7686+
7687+
class InlineQueryResultsButton(JsonSerializable, Dictionaryable):
7688+
"""
7689+
This object represents a button to be shown above inline query results.
7690+
You must use exactly one of the optional fields.
7691+
7692+
Telegram documentation: https://core.telegram.org/bots/api#inlinequeryresultsbutton
7693+
7694+
:param text: Label text on the button
7695+
:type text: :obj:`str`
7696+
7697+
:param web_app: Optional. Description of the Web App that will be launched when the user presses the button.
7698+
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.
7699+
:type web_app: :class:`telebot.types.WebAppInfo`
7700+
7701+
:param start_parameter: Optional. Deep-linking parameter for the /start message sent to the bot when a user presses the button.
7702+
1-64 characters, only A-Z, a-z, 0-9, _ and - are allowed.
7703+
Example: An inline bot that sends YouTube videos can ask the user to connect the bot to their YouTube account to adapt search
7704+
results accordingly. To do this, it displays a 'Connect your YouTube account' button above the results, or even before showing
7705+
any. The user presses the button, switches to a private chat with the bot and, in doing so, passes a start parameter that instructs
7706+
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
7707+
where they wanted to use the bot's inline capabilities.
7708+
:type start_parameter: :obj:`str`
7709+
7710+
:return: Instance of the class
7711+
:rtype: :class:`InlineQueryResultsButton`
7712+
"""
7713+
7714+
def __init__(self, text: str, web_app: Optional[WebAppInfo]=None, start_parameter: Optional[str]=None) -> None:
7715+
self.text: str = text
7716+
self.web_app: Optional[WebAppInfo] = web_app
7717+
self.start_parameter: Optional[str] = start_parameter
7718+
7719+
7720+
def to_dict(self) -> dict:
7721+
json_dict = {
7722+
'text': self.text
7723+
}
7724+
7725+
if self.web_app is not None:
7726+
json_dict['web_app'] = self.web_app.to_dict()
7727+
if self.start_parameter is not None:
7728+
json_dict['start_parameter'] = self.start_parameter
7729+
7730+
return json_dict
7731+
7732+
def to_json(self) -> str:
7733+
return json.dumps(self.to_dict())

0 commit comments

Comments
 (0)