1#!/usr/bin/env python
2# pylint: disable=unused-argument, wrong-import-position
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 __version__ as TG_VER
22
23try:
24 from telegram import __version_info__
25except ImportError:
26 __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
27
28if __version_info__ < (20, 0, 0, "alpha", 1):
29 raise RuntimeError(
30 f"This example is not compatible with your current PTB version {TG_VER}. To view the "
31 f"{TG_VER} version of this example, "
32 f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
33 )
34from telegram import InlineQueryResultArticle, InputTextMessageContent, Update
35from telegram.constants import ParseMode
36from telegram.ext import Application, CommandHandler, ContextTypes, InlineQueryHandler
37
38# Enable logging
39logging.basicConfig(
40 format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
41)
42logger = logging.getLogger(__name__)
43
44
45# Define a few command handlers. These usually take the two arguments update and
46# context.
47async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
48 """Send a message when the command /start is issued."""
49 await update.message.reply_text("Hi!")
50
51
52async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
53 """Send a message when the command /help is issued."""
54 await update.message.reply_text("Help!")
55
56
57async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
58 """Handle the inline query. This is run when you type: @botusername <query>"""
59 query = update.inline_query.query
60
61 if query == "":
62 return
63
64 results = [
65 InlineQueryResultArticle(
66 id=str(uuid4()),
67 title="Caps",
68 input_message_content=InputTextMessageContent(query.upper()),
69 ),
70 InlineQueryResultArticle(
71 id=str(uuid4()),
72 title="Bold",
73 input_message_content=InputTextMessageContent(
74 f"<b>{escape(query)}</b>", parse_mode=ParseMode.HTML
75 ),
76 ),
77 InlineQueryResultArticle(
78 id=str(uuid4()),
79 title="Italic",
80 input_message_content=InputTextMessageContent(
81 f"<i>{escape(query)}</i>", parse_mode=ParseMode.HTML
82 ),
83 ),
84 ]
85
86 await update.inline_query.answer(results)
87
88
89def main() -> None:
90 """Run the bot."""
91 # Create the Application and pass it your bot's token.
92 application = Application.builder().token("TOKEN").build()
93
94 # on different commands - answer in Telegram
95 application.add_handler(CommandHandler("start", start))
96 application.add_handler(CommandHandler("help", help_command))
97
98 # on non command i.e message - echo the message on Telegram
99 application.add_handler(InlineQueryHandler(inline_query))
100
101 # Run the bot until the user presses Ctrl-C
102 application.run_polling()
103
104
105if __name__ == "__main__":
106 main()