2016-01-04 17:31:06 +01:00
|
|
|
#!/usr/bin/env python
|
2023-09-09 23:12:30 +02:00
|
|
|
# pylint: disable=unused-argument
|
2019-02-13 13:38:07 +01:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
2017-10-20 20:24:00 +02:00
|
|
|
|
2019-02-13 13:38:07 +01:00
|
|
|
"""
|
2022-06-09 17:08:54 +02:00
|
|
|
Don't forget to enable inline mode with @BotFather
|
|
|
|
|
2016-01-04 17:31:06 +01:00
|
|
|
First, a few handler functions are defined. Then, those functions are passed to
|
2022-04-24 12:38:09 +02:00
|
|
|
the Application and registered at their respective places.
|
2016-01-04 17:31:06 +01:00
|
|
|
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.
|
|
|
|
"""
|
2018-09-21 08:57:01 +02:00
|
|
|
import logging
|
2022-04-24 12:38:09 +02:00
|
|
|
from html import escape
|
2022-05-05 09:27:54 +02:00
|
|
|
from uuid import uuid4
|
2016-01-04 17:31:06 +01:00
|
|
|
|
2021-10-19 18:28:19 +02:00
|
|
|
from telegram import InlineQueryResultArticle, InputTextMessageContent, Update
|
|
|
|
from telegram.constants import ParseMode
|
2022-05-12 19:36:25 +02:00
|
|
|
from telegram.ext import Application, CommandHandler, ContextTypes, InlineQueryHandler
|
2016-01-04 17:31:06 +01:00
|
|
|
|
|
|
|
# Enable logging
|
2016-05-22 18:26:57 +02:00
|
|
|
logging.basicConfig(
|
|
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
|
|
|
)
|
2023-06-07 22:32:04 +02:00
|
|
|
# set higher logging level for httpx to avoid all GET and POST requests being logged
|
|
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
|
|
|
2016-01-04 17:31:06 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-10-11 20:10:21 +02:00
|
|
|
# Define a few command handlers. These usually take the two arguments update and
|
2022-04-24 12:38:09 +02:00
|
|
|
# context.
|
2022-05-12 19:36:25 +02:00
|
|
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Send a message when the command /start is issued."""
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text("Hi!")
|
2016-01-04 17:31:06 +01:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2017-10-20 20:24:00 +02:00
|
|
|
"""Send a message when the command /help is issued."""
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text("Help!")
|
2016-01-04 17:31:06 +01:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def inline_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2022-04-24 12:38:09 +02:00
|
|
|
"""Handle the inline query. This is run when you type: @botusername <query>"""
|
2016-04-22 15:36:46 +02:00
|
|
|
query = update.inline_query.query
|
2021-03-28 18:53:44 +02:00
|
|
|
|
2023-03-25 19:18:04 +01:00
|
|
|
if not query: # empty query should not be handled
|
2021-03-28 18:53:44 +02:00
|
|
|
return
|
|
|
|
|
2017-10-20 20:24:00 +02:00
|
|
|
results = [
|
|
|
|
InlineQueryResultArticle(
|
2021-03-13 16:21:03 +01:00
|
|
|
id=str(uuid4()),
|
|
|
|
title="Caps",
|
|
|
|
input_message_content=InputTextMessageContent(query.upper()),
|
2017-10-20 20:24:00 +02:00
|
|
|
),
|
|
|
|
InlineQueryResultArticle(
|
2021-03-13 16:21:03 +01:00
|
|
|
id=str(uuid4()),
|
2017-10-20 20:24:00 +02:00
|
|
|
title="Bold",
|
|
|
|
input_message_content=InputTextMessageContent(
|
2022-04-24 12:38:09 +02:00
|
|
|
f"<b>{escape(query)}</b>", parse_mode=ParseMode.HTML
|
2017-10-20 20:24:00 +02:00
|
|
|
),
|
2020-10-09 17:22:07 +02:00
|
|
|
),
|
2017-10-20 20:24:00 +02:00
|
|
|
InlineQueryResultArticle(
|
2021-03-13 16:21:03 +01:00
|
|
|
id=str(uuid4()),
|
2017-10-20 20:24:00 +02:00
|
|
|
title="Italic",
|
|
|
|
input_message_content=InputTextMessageContent(
|
2022-04-24 12:38:09 +02:00
|
|
|
f"<i>{escape(query)}</i>", parse_mode=ParseMode.HTML
|
2020-10-09 17:22:07 +02:00
|
|
|
),
|
|
|
|
),
|
2017-10-20 20:24:00 +02:00
|
|
|
]
|
2016-04-22 15:36:46 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.inline_query.answer(results)
|
2016-01-04 17:31:06 +01:00
|
|
|
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
def main() -> None:
|
2021-05-27 20:34:58 +02:00
|
|
|
"""Run the bot."""
|
2022-04-24 12:38:09 +02:00
|
|
|
# Create the Application and pass it your bot's token.
|
|
|
|
application = Application.builder().token("TOKEN").build()
|
2016-01-04 17:31:06 +01:00
|
|
|
|
|
|
|
# on different commands - answer in Telegram
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(CommandHandler("start", start))
|
|
|
|
application.add_handler(CommandHandler("help", help_command))
|
2016-01-04 17:31:06 +01:00
|
|
|
|
2023-07-09 11:22:08 +02:00
|
|
|
# on inline queries - show corresponding inline results
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(InlineQueryHandler(inline_query))
|
2016-01-04 17:31:06 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Run the bot until the user presses Ctrl-C
|
2023-06-04 17:11:58 +02:00
|
|
|
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
2016-01-04 17:31:06 +01:00
|
|
|
|
2016-05-22 18:26:57 +02:00
|
|
|
|
2016-01-04 17:31:06 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|