1#!/usr/bin/env python
2# pylint: disable=unused-argument
3# This program is dedicated to the public domain under the CC0 license.
4
5"""
6Don't forget to enable inline mode with @BotFather
7
8First, a few handler functions are defined. Then, those functions are passed to
9the Application and registered at their respective places.
10Then, the bot is started and runs until we press Ctrl-C on the command line.
11
12Usage:
13Basic inline bot example. Applies different text transformations.
14Press Ctrl-C on the command line or send a signal to the process to stop the
15bot.
16"""
17import logging
18from html import escape
19from uuid import uuid4
20
21from telegram import InlineQueryResultArticle, InputTextMessageContent, Update
22from telegram.constants import ParseMode
23from telegram.ext import Application, CommandHandler, ContextTypes, InlineQueryHandler
24
25# Enable logging
26logging.basicConfig(
27 format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
28)
29# set higher logging level for httpx to avoid all GET and POST requests being logged
30logging.getLogger("httpx").setLevel(logging.WARNING)
31
32logger = logging.getLogger(__name__)
33
34
35# Define a few command handlers. These usually take the two arguments update and
36# context.
37async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
38 """Send a message when the command /start is issued."""
39 await update.message.reply_text("Hi!")
40
41
42async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
43 """Send a message when the command /help is issued."""
44 await update.message.reply_text("Help!")
45
46
47async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
48 """Handle the inline query. This is run when you type: @botusername <query>"""
49 query = update.inline_query.query
50
51 if not query: # empty query should not be handled
52 return
53
54 results = [
55 InlineQueryResultArticle(
56 id=str(uuid4()),
57 title="Caps",
58 input_message_content=InputTextMessageContent(query.upper()),
59 ),
60 InlineQueryResultArticle(
61 id=str(uuid4()),
62 title="Bold",
63 input_message_content=InputTextMessageContent(
64 f"<b>{escape(query)}</b>", parse_mode=ParseMode.HTML
65 ),
66 ),
67 InlineQueryResultArticle(
68 id=str(uuid4()),
69 title="Italic",
70 input_message_content=InputTextMessageContent(
71 f"<i>{escape(query)}</i>", parse_mode=ParseMode.HTML
72 ),
73 ),
74 ]
75
76 await update.inline_query.answer(results)
77
78
79def main() -> None:
80 """Run the bot."""
81 # Create the Application and pass it your bot's token.
82 application = Application.builder().token("TOKEN").build()
83
84 # on different commands - answer in Telegram
85 application.add_handler(CommandHandler("start", start))
86 application.add_handler(CommandHandler("help", help_command))
87
88 # on inline queries - show corresponding inline results
89 application.add_handler(InlineQueryHandler(inline_query))
90
91 # Run the bot until the user presses Ctrl-C
92 application.run_polling(allowed_updates=Update.ALL_TYPES)
93
94
95if __name__ == "__main__":
96 main()