Skip to content

Instantly share code, notes, and snippets.

@Bibo-Joshi
Created May 18, 2021 19:04
Show Gist options
  • Save Bibo-Joshi/ff3deb015e027c89e10b2f261d1b5146 to your computer and use it in GitHub Desktop.
Save Bibo-Joshi/ff3deb015e027c89e10b2f261d1b5146 to your computer and use it in GitHub Desktop.
A small study on the more exotic parameters of inline results for Telegram Bots.
"""
Tested with pyhon-telegram-bot>=13.5 (>=13.0 should also do the trick …)
A small study on the more exotic parameters of inline results for Telegram Bots.
Punchline: The effect heavily depends on the client and is not always straight forward.
E.g. when passing a `url` to the `input_message_content` of a `InlineQueryResultArticle`, but set
`hide_url=True`, the `url` just has no effect at all (on official Android and Windows clients)
"""
import itertools
import logging
from uuid import uuid4
from telegram import InlineQueryResultPhoto, InputTextMessageContent, InputLocationMessageContent, \
InputVenueMessageContent, InputContactMessageContent, InputInvoiceMessageContent, \
InlineQueryResultArticle
from telegram.ext import Updater, InlineQueryHandler
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
input_text_message_content = InputTextMessageContent(
message_text='This is an InputTextMessageContent',
)
input_location_message_content = InputLocationMessageContent(45.123853, -123.113603)
input_venue_message_content = InputVenueMessageContent(
45.123853,
-123.113603,
title='Firefox Logo',
address='Amity School District 04j, Oregon, USA'
)
input_contact_message_content = InputContactMessageContent(
'+00 123 456 789',
'John',
'Doe',
vcard=(
'BEGIN:VCARD'
'VERSION:3.0'
'FN;CHARSET=UTF-8:John Doe'
'N;CHARSET=UTF-8:Doe;John;;;'
'TEL;TYPE=HOME,VOICE:+00 123 456 789'
'REV:2021-05-18T17:20:19.067Z'
'END:VCARD'
)
)
# If you want to use this, you'll have to set up payments for your bot
# input_invoice_message_content = InputInvoiceMessageContent()
INPUT_MESSAGE_CONTENTS = {
'text': input_text_message_content,
'location': input_location_message_content,
'venue': input_venue_message_content,
'contact': input_contact_message_content,
# 'invoice': input_invoice_message_content,
}
URL = 'https://python-telegram-bot.org'
PHOTO_URL = 'https://python-telegram-bot.org/static/testfiles/telegram.png'
RESULTS = [
InlineQueryResultPhoto(
id='photo',
photo_url=PHOTO_URL,
thumb_url=PHOTO_URL,
input_message_content=input_text_message_content,
title='This is a title'
)
]
RESULTS.extend(
[
InlineQueryResultArticle(
id=uuid4().hex,
input_message_content=INPUT_MESSAGE_CONTENTS[article_type],
title=f'{article_type} result',
description=f'url={url}, hide_url={hide_url}, thumb={thumb}',
url=URL if url else None,
hide_url=hide_url,
thumb_url=PHOTO_URL if thumb else None,
)
for article_type, url, hide_url, thumb in itertools.product(
INPUT_MESSAGE_CONTENTS.keys(),
(True, False),
(True, False),
(True, False)
)
]
)
updater = Updater('TOKEN')
updater.dispatcher.add_handler(
InlineQueryHandler(
lambda u, _: u.inline_query.answer(
results=RESULTS,
auto_pagination=True,
cache_time=0
)
)
)
updater.start_polling()
updater.idle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment