我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用telegram.InlineQueryResultArticle()。
def send_inline_query_error(bot: telegram.Bot, error_text: str, query_id: str): bot.answer_inline_query( inline_query_id=query_id, is_personal=True, cache_time=0, results=[telegram.InlineQueryResultArticle( extentions.TextHelper.get_random_id(), '??????', telegram.InputTextMessageContent(error_text, parse_mode='HTML'), description=error_text )] )
def answer(bot, update, entries, offset='', switch_pm=None): if not switch_pm: switch_pm = (_('Click for help.'), 'help_inline') results = [] for entry in entries: try: thumb = entry['mainPicture']['urlThumb'] except KeyError: thumb = '' content = content_parser(entry, info=True, inline=True, bot_name=bot.username) if 'songType' in entry: description = _('{artist}\n{type} song').format(artist=entry['artistString'], type=entry['songType']) if 'favoritedTimes' in entry: description += ' ' + _('with {favorites} favourites').format(favorites=entry['favoritedTimes']) results.append(InlineQueryResultArticle( id=uuid4(), title=Emoji.MUSICAL_NOTE + ' ' + entry['name'], description=description, thumb_url=thumb, input_message_content=InputTextMessageContent(content, parse_mode=ParseMode.HTML, disable_web_page_preview=True), reply_markup=song_keyboard(entry, inline=True) )) elif 'artistType' in entry: results.append(InlineQueryResultArticle( id=uuid4(), title=Emoji.MICROPHONE + ' ' + entry['name'], description='{type}'.format(type=entry['artistType']), thumb_url=thumb, input_message_content=InputTextMessageContent(content, parse_mode=ParseMode.HTML, disable_web_page_preview=True), reply_markup=artist_keyboard(entry, inline=True) )) elif 'discType' in entry: description = '{artist}\n{type}'.format(artist=entry['artistString'], type=entry['discType']) results.append(InlineQueryResultArticle( id=uuid4(), title=Emoji.OPTICAL_DISC + ' ' + entry['name'], description=description, thumb_url=thumb, input_message_content=InputTextMessageContent(content, parse_mode=ParseMode.HTML, disable_web_page_preview=True), reply_markup=album_keyboard(entry, inline=True) )) update.inline_query.answer(results=results, cache_time=INLINE_CACHE_TIME, is_personal=True, next_offset=offset, switch_pm_text=switch_pm[0], switch_pm_parameter=switch_pm[1])
def test_inline_bot(self): # create some handlers and add them def escape_markdown(text): """Helper function to escape telegram markup symbols""" escape_chars = '\*_`\[' return re.sub(r'([%s])' % escape_chars, r'\\\1', text) def inlinequery(bot, update): query = update.inline_query.query results = list() results.append(InlineQueryResultArticle(id=uuid4(), title="Caps", input_message_content=InputTextMessageContent( query.upper()))) results.append(InlineQueryResultArticle(id=uuid4(), title="Bold", input_message_content=InputTextMessageContent( "*%s*" % escape_markdown(query), parse_mode=ParseMode.MARKDOWN))) results.append(InlineQueryResultArticle(id=uuid4(), title="Italic", input_message_content=InputTextMessageContent( "_%s_" % escape_markdown(query), parse_mode=ParseMode.MARKDOWN))) update.inline_query.answer(results) dp = self.updater.dispatcher dp.add_handler(InlineQueryHandler(inlinequery)) self.updater.start_polling() # Now test the handler u1 = self.iqg.get_inline_query(query="test data") self.bot.insertUpdate(u1) data = self.bot.sent_messages[-1] self.assertEqual(len(data['results']), 3) results = data['results'] self.assertEqual(results[0]['title'], "Caps") self.assertEqual(results[0]['input_message_content']['message_text'], "TEST DATA") self.assertEqual(results[1]['title'], "Bold") self.assertEqual(results[1]['input_message_content']['message_text'], "*test data*") self.assertEqual(results[2]['title'], "Italic") self.assertEqual(results[2]['input_message_content']['message_text'], "_test data_") self.updater.stop()