Search code examples
pythonbotstelegramtelegram-bottelebot

How to do a inline mod with telebot?


I want to make an inline mod for my bot, I wouldn't even try to make it, it still doesn't work, it just loads and the buttons don't pop up.

It would be nice if someone could show how to actually make a working version of the inline part.

I try to run code from examples from telebot on github, but it still doesn't work. This example:

# This example show how to write an inline mode telegram bot use pyTelegramBotAPI.

import logging
import sys
import time
import telebot
from telebot import types

API_TOKEN = ""
bot = telebot.TeleBot(API_TOKEN)
telebot.logger.setLevel(logging.DEBUG)

@bot.inline_handler(lambda query: query.query == 'text')
def query_text(inline_query):
    try:
        r = types.InlineQueryResultArticle('1', 'Result1', types.InputTextMessageContent('hi'))
        r2 = types.InlineQueryResultArticle('2', 'Result2', types.InputTextMessageContent('hi'))
        bot.answer_inline_query(inline_query.id, [r, r2])
    except Exception as e:
        print(e)

@bot.inline_handler(lambda query: query.query == 'photo1')
def query_photo(inline_query):
    try:
        r = types.InlineQueryResultPhoto('1', 'https://raw.githubusercontent.com/eternnoir/pyTelegramBotAPI/master/examples/detailed_example/kitten.jpg', 'https://raw.githubusercontent.com/eternnoir/pyTelegramBotAPI/master/examples/detailed_example/kitten.jpg', input_message_content=types.InputTextMessageContent('hi'))
        r2 = types.InlineQueryResultPhoto('2', 'https://raw.githubusercontent.com/eternnoir/pyTelegramBotAPI/master/examples/detailed_example/rooster.jpg', 'https://raw.githubusercontent.com/eternnoir/pyTelegramBotAPI/master/examples/detailed_example/rooster.jpg')
        bot.answer_inline_query(inline_query.id, [r, r2], cache_time=1)
    except Exception as e:
        print(e)

@bot.inline_handler(lambda query: query.query == 'video')
def query_video(inline_query):
    try:
        r = types.InlineQueryResultVideo('1', 'https://github.com/eternnoir/pyTelegramBotAPI/blob/master/tests/test_data/test_video.mp4?raw=true', 'video/mp4', 'https://raw.githubusercontent.com/eternnoir/pyTelegramBotAPI/master/examples/detailed_example/rooster.jpg', 'Title')
        bot.answer_inline_query(inline_query.id, [r])
    except Exception as e:
        print(e)

@bot.inline_handler(lambda query: len(query.query) == 0)
def default_query(inline_query):
    try:
        r = types.InlineQueryResultArticle('1', 'default', types.InputTextMessageContent('default'))
        bot.answer_inline_query(inline_query.id, [r])
    except Exception as e:
        print(e)

def main_loop():
    bot.infinity_polling()
    while 1:
        time.sleep(3)

if __name__ == '__main__':
    try:
        main_loop()
    except KeyboardInterrupt:
        print('\nExiting by user request.\n')
        sys.exit(0)

As far as I understand, Inline_handler does not respond to requests at all, I also try this:

@bot.inline_handler(lambda query: True):
def answer(query):
    print(query)

And nothing


Solution

  • inline_handler does not work without allowed_updates=True in bot.infinity_polling(). With him everything is ok.