pyTelegramBotAPI/examples/inline_example.py

75 lines
2.8 KiB
Python
Raw Permalink Normal View History

2018-09-05 08:32:19 +03:00
# This example show how to write an inline mode telegram bot use pyTelegramBotAPI.
2016-01-05 16:54:47 +03:00
import logging
2018-08-17 12:54:26 +03:00
import sys
import time
import telebot
2016-01-05 09:58:19 +03:00
from telebot import types
2016-04-14 12:43:45 +03:00
API_TOKEN = '<TOKEN>'
2016-01-05 09:58:19 +03:00
bot = telebot.TeleBot(API_TOKEN)
2016-01-05 16:54:47 +03:00
telebot.logger.setLevel(logging.DEBUG)
2016-01-05 09:58:19 +03:00
@bot.inline_handler(lambda query: query.query == 'text')
2016-01-05 16:54:47 +03:00
def query_text(inline_query):
2016-01-05 09:58:19 +03:00
try:
2016-04-14 12:43:45 +03:00
r = types.InlineQueryResultArticle('1', 'Result1', types.InputTextMessageContent('hi'))
r2 = types.InlineQueryResultArticle('2', 'Result2', types.InputTextMessageContent('hi'))
2016-01-05 16:54:47 +03:00
bot.answer_inline_query(inline_query.id, [r, r2])
except Exception as e:
print(e)
2016-04-14 12:43:45 +03:00
@bot.inline_handler(lambda query: query.query == 'photo1')
2016-01-05 16:54:47 +03:00
def query_photo(inline_query):
try:
r = types.InlineQueryResultPhoto('1',
'https://raw.githubusercontent.com/eternnoir/pyTelegramBotAPI/master/examples/detailed_example/kitten.jpg',
2016-04-14 12:43:45 +03:00
'https://raw.githubusercontent.com/eternnoir/pyTelegramBotAPI/master/examples/detailed_example/kitten.jpg',
input_message_content=types.InputTextMessageContent('hi'))
2016-01-05 16:54:47 +03:00
r2 = types.InlineQueryResultPhoto('2',
'https://raw.githubusercontent.com/eternnoir/pyTelegramBotAPI/master/examples/detailed_example/rooster.jpg',
2016-01-05 17:23:00 +03:00
'https://raw.githubusercontent.com/eternnoir/pyTelegramBotAPI/master/examples/detailed_example/rooster.jpg')
2016-04-14 12:43:45 +03:00
bot.answer_inline_query(inline_query.id, [r, r2], cache_time=1)
2016-01-05 09:58:19 +03:00
except Exception as e:
print(e)
2016-01-05 17:23:00 +03:00
@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',
2016-01-05 17:23:00 +03:00
'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)
2016-01-05 09:58:19 +03:00
def default_query(inline_query):
try:
2016-07-17 15:54:26 +03:00
r = types.InlineQueryResultArticle('1', 'default', types.InputTextMessageContent('default'))
2016-01-05 09:58:19 +03:00
bot.answer_inline_query(inline_query.id, [r])
except Exception as e:
print(e)
def main_loop():
2021-09-28 19:17:09 +03:00
bot.infinity_polling()
2016-01-05 09:58:19 +03:00
while 1:
time.sleep(3)
if __name__ == '__main__':
try:
main_loop()
except KeyboardInterrupt:
print('\nExiting by user request.\n')
2016-01-05 09:58:19 +03:00
sys.exit(0)