Skip to content

Commit 6da529c

Browse files
authored
Pass correct parse_mode to InlineResults if bot.defaults is None (python-telegram-bot#1763)
* Pass correct parse_mode to InlineResults if bot.defaults is None * Add tests for inlinequeryresults with (default)parse_mode * enhance tests
1 parent bd1b2fb commit 6da529c

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

telegram/bot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,10 @@ def answer_inline_query(self,
15121512

15131513
for res in results:
15141514
if res._has_parse_mode and res.parse_mode == DEFAULT_NONE:
1515-
res.parse_mode = self.defaults.parse_mode
1515+
if self.defaults:
1516+
res.parse_mode = self.defaults.parse_mode
1517+
else:
1518+
res.parse_mode = None
15161519
if res._has_input_message_content and res.input_message_content:
15171520
if (res.input_message_content._has_parse_mode
15181521
and res.input_message_content.parse_mode == DEFAULT_NONE):

tests/test_bot.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828

2929
from telegram import (Bot, Update, ChatAction, TelegramError, User, InlineKeyboardMarkup,
3030
InlineKeyboardButton, InlineQueryResultArticle, InputTextMessageContent,
31-
ShippingOption, LabeledPrice, ChatPermissions, Poll)
31+
ShippingOption, LabeledPrice, ChatPermissions, Poll,
32+
InlineQueryResultDocument)
3233
from telegram.error import BadRequest, InvalidToken, NetworkError, RetryAfter
3334
from telegram.utils.helpers import from_timestamp, escape_markdown
3435

@@ -237,6 +238,67 @@ def test(_, url, data, *args, **kwargs):
237238
switch_pm_text='switch pm',
238239
switch_pm_parameter='start_pm')
239240

241+
def test_answer_inline_query_no_default_parse_mode(self, monkeypatch, bot):
242+
def test(_, url, data, *args, **kwargs):
243+
return data == {'cache_time': 300,
244+
'results': [{'title': 'test_result', 'id': '123', 'type': 'document',
245+
'document_url': 'https://raw.githubusercontent.com/'
246+
'python-telegram-bot/logos/master/logo/png/'
247+
'ptb-logo_240.png', 'mime_type': 'image/png',
248+
'caption': 'ptb_logo'}],
249+
'next_offset': '42', 'switch_pm_parameter': 'start_pm',
250+
'inline_query_id': 1234, 'is_personal': True,
251+
'switch_pm_text': 'switch pm'}
252+
253+
monkeypatch.setattr('telegram.utils.request.Request.post', test)
254+
results = [InlineQueryResultDocument(
255+
id='123',
256+
document_url='https://raw.githubusercontent.com/python-telegram-bot/logos/master/'
257+
'logo/png/ptb-logo_240.png',
258+
title='test_result',
259+
mime_type='image/png',
260+
caption='ptb_logo',
261+
)]
262+
263+
assert bot.answer_inline_query(1234,
264+
results=results,
265+
cache_time=300,
266+
is_personal=True,
267+
next_offset='42',
268+
switch_pm_text='switch pm',
269+
switch_pm_parameter='start_pm')
270+
271+
@pytest.mark.parametrize('default_bot', [{'parse_mode': 'Markdown'}], indirect=True)
272+
def test_answer_inline_query_default_parse_mode(self, monkeypatch, default_bot):
273+
def test(_, url, data, *args, **kwargs):
274+
return data == {'cache_time': 300,
275+
'results': [{'title': 'test_result', 'id': '123', 'type': 'document',
276+
'document_url': 'https://raw.githubusercontent.com/'
277+
'python-telegram-bot/logos/master/logo/png/'
278+
'ptb-logo_240.png', 'mime_type': 'image/png',
279+
'caption': 'ptb_logo', 'parse_mode': 'Markdown'}],
280+
'next_offset': '42', 'switch_pm_parameter': 'start_pm',
281+
'inline_query_id': 1234, 'is_personal': True,
282+
'switch_pm_text': 'switch pm'}
283+
284+
monkeypatch.setattr('telegram.utils.request.Request.post', test)
285+
results = [InlineQueryResultDocument(
286+
id='123',
287+
document_url='https://raw.githubusercontent.com/python-telegram-bot/logos/master/'
288+
'logo/png/ptb-logo_240.png',
289+
title='test_result',
290+
mime_type='image/png',
291+
caption='ptb_logo',
292+
)]
293+
294+
assert default_bot.answer_inline_query(1234,
295+
results=results,
296+
cache_time=300,
297+
is_personal=True,
298+
next_offset='42',
299+
switch_pm_text='switch pm',
300+
switch_pm_parameter='start_pm')
301+
240302
@flaky(3, 1)
241303
@pytest.mark.timeout(10)
242304
def test_get_user_profile_photos(self, bot, chat_id):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy