mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-22 22:45:09 +01:00
f77f4b0cf7
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
96 lines
3.1 KiB
Python
96 lines
3.1 KiB
Python
#!/usr/bin/env python
|
|
# pylint: disable=unused-argument
|
|
# This program is dedicated to the public domain under the CC0 license.
|
|
|
|
"""
|
|
Don't forget to enable inline mode with @BotFather
|
|
|
|
First, a few handler functions are defined. Then, those functions are passed to
|
|
the Application and registered at their respective places.
|
|
Then, the bot is started and runs until we press Ctrl-C on the command line.
|
|
|
|
Usage:
|
|
Basic inline bot example. Applies different text transformations.
|
|
Press Ctrl-C on the command line or send a signal to the process to stop the
|
|
bot.
|
|
"""
|
|
import logging
|
|
from html import escape
|
|
from uuid import uuid4
|
|
|
|
from telegram import InlineQueryResultArticle, InputTextMessageContent, Update
|
|
from telegram.constants import ParseMode
|
|
from telegram.ext import Application, CommandHandler, ContextTypes, InlineQueryHandler
|
|
|
|
# Enable logging
|
|
logging.basicConfig(
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
|
)
|
|
# set higher logging level for httpx to avoid all GET and POST requests being logged
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# Define a few command handlers. These usually take the two arguments update and
|
|
# context.
|
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|
"""Send a message when the command /start is issued."""
|
|
await update.message.reply_text("Hi!")
|
|
|
|
|
|
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|
"""Send a message when the command /help is issued."""
|
|
await update.message.reply_text("Help!")
|
|
|
|
|
|
async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|
"""Handle the inline query. This is run when you type: @botusername <query>"""
|
|
query = update.inline_query.query
|
|
|
|
if not query: # empty query should not be handled
|
|
return
|
|
|
|
results = [
|
|
InlineQueryResultArticle(
|
|
id=str(uuid4()),
|
|
title="Caps",
|
|
input_message_content=InputTextMessageContent(query.upper()),
|
|
),
|
|
InlineQueryResultArticle(
|
|
id=str(uuid4()),
|
|
title="Bold",
|
|
input_message_content=InputTextMessageContent(
|
|
f"<b>{escape(query)}</b>", parse_mode=ParseMode.HTML
|
|
),
|
|
),
|
|
InlineQueryResultArticle(
|
|
id=str(uuid4()),
|
|
title="Italic",
|
|
input_message_content=InputTextMessageContent(
|
|
f"<i>{escape(query)}</i>", parse_mode=ParseMode.HTML
|
|
),
|
|
),
|
|
]
|
|
|
|
await update.inline_query.answer(results)
|
|
|
|
|
|
def main() -> None:
|
|
"""Run the bot."""
|
|
# Create the Application and pass it your bot's token.
|
|
application = Application.builder().token("TOKEN").build()
|
|
|
|
# on different commands - answer in Telegram
|
|
application.add_handler(CommandHandler("start", start))
|
|
application.add_handler(CommandHandler("help", help_command))
|
|
|
|
# on inline queries - show corresponding inline results
|
|
application.add_handler(InlineQueryHandler(inline_query))
|
|
|
|
# Run the bot until the user presses Ctrl-C
|
|
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|