def on_query(bot: Bot, update: Update): # This constant is defined by the Bot API. MAX_RESULTS = 50 inline_query = update.inline_query if not inline_query: return # If query is empty - return random stickers. return_random = not inline_query.query logger.info("Inline query received", from_id=inline_query.from_user.id, from_name=inline_query.from_user.first_name, text=inline_query.query) if return_random: stickers = random_stickers(MAX_RESULTS) else: stickers = search_stickers(inline_query.query) if len(stickers) > MAX_RESULTS: stickers = stickers[:MAX_RESULTS] results = [InlineQueryResultCachedSticker(fid, fid) for fid in stickers] cache_time = 600 if return_random: # Do not cache random results. cache_time = 0 bot.answer_inline_query(inline_query.id, results, cache_time=cache_time)
def inline_query_callback(bot: telegram.Bot, update: telegram.Update): query = update.inline_query if re.search('стикер', query.query) is not None: results: typing.List[telegram.InlineQueryResultCachedSticker] = [] for sticker_file_id in DB().get_stickers_with_limit(50): results.append( telegram.InlineQueryResultCachedSticker( id=sticker_file_id[30:], sticker_file_id=sticker_file_id, ), ) bot.answer_inline_query(inline_query_id=query.id, results=results) # TODO: make it work properly elif re.match('голос', query.query) is not None: text = query.query[5:] voice_filename = tts_api_request.get_voice_file(text=text) with open(voice_filename, 'rb') as f: voice_result = telegram.InlineQueryResultVoice( id=voice_filename[-6:], voice_url=f, ) bot.answer_inline_query( inline_query_id=query.id, results=[voice_result], ) os.remove(voice_filename)
def inline_bot(bot: Bot, update): query = update.inline_query.query if not query: return results = [ InlineQueryResultArticle(id=uuid4(), title="Caps", input_message_content=InputTextMessageContent( query.upper())), InlineQueryResultArticle(id=uuid4(), title="Bold", input_message_content=InputTextMessageContent( "*{}*".format(escape_markdown(query)), parse_mode=ParseMode.MARKDOWN)), InlineQueryResultArticle(id=uuid4(), title="Italic", input_message_content=InputTextMessageContent( "_{}_".format(escape_markdown(query)), parse_mode=ParseMode.MARKDOWN)) ] bot.answer_inline_query(update.inline_query.id, results)
def inlinequery(bot: Bot, update: Update): query = update.inline_query.query.replace(" ", "") user = update.inline_query.from_user name = '' if user.username: name = user.username else: name = user.first_name results = list() if dice_notation.is_single_die(query): title = '{0} rolled {1}'.format(name, query) advantage_total, advantage_rolls = dice_notation.handicap( 'disadvantage', query) results.append( InlineQueryResultArticle( id=uuid4(), title="Roll {0} with advantage".format(query), input_message_content=InputTextMessageContent( roll_responder('{0} with advantage'.format(title), advantage_total, advantage_rolls), disable_web_page_preview=True, parse_mode='HTML'))) disadvantage_total, disadvantage_rolls = dice_notation.handicap( 'disadvantage', query) results.append( InlineQueryResultArticle( id=uuid4(), title="Roll {0} with disadvantage".format(query), input_message_content=InputTextMessageContent( roll_responder('{0} with disadvantage'.format(title), disadvantage_total, disadvantage_rolls), disable_web_page_preview=True, parse_mode='HTML'))) if dice_notation.is_dice_notation(query): title = '{0} rolled {1}'.format(name, query) result, rolls = grammar.evaluate(query) results.append( InlineQueryResultArticle( id=uuid4(), title="Roll {0}".format(query), input_message_content=InputTextMessageContent( roll_responder(title, result, rolls), disable_web_page_preview=True, parse_mode='HTML'))) else: results.append( InlineQueryResultArticle( id=uuid4(), title="Invalid Roll", input_message_content=InputTextMessageContent( 'Query: {0}\n\n{1}'.format(query, INVALID_DICE_NOTATION_MSG), disable_web_page_preview=True, parse_mode='HTML'))) bot.answer_inline_query(update.inline_query.id, results, cache_time=0)