aboutsummaryrefslogtreecommitdiff
path: root/bot.py
diff options
context:
space:
mode:
authorclarkzjw <[email protected]>2023-02-20 01:03:57 -0800
committerclarkzjw <[email protected]>2023-02-20 01:03:57 -0800
commit76597b5c619649c2457cb876a2c749e9c7a4a248 (patch)
tree80e89c7547aad3986b9f1f6afe900b3d1a6e5756 /bot.py
parentedbb47990eac81a1c81a0f0b0f1e63d2d8f7e619 (diff)
downloadswarm2fediverse-76597b5c619649c2457cb876a2c749e9c7a4a248.tar.gz
add python-telegram-bot inline keyboard example
Diffstat (limited to 'bot.py')
-rw-r--r--bot.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/bot.py b/bot.py
new file mode 100644
index 0000000..517f810
--- /dev/null
+++ b/bot.py
@@ -0,0 +1,79 @@
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"""
6Basic example for a bot that uses inline keyboards. For an in-depth explanation, check out
7 https://github.com/python-telegram-bot/python-telegram-bot/wiki/InlineKeyboard-Example.
8"""
9import logging
10
11from telegram import __version__ as TG_VER
12
13try:
14 from telegram import __version_info__
15except ImportError:
16 __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
17
18if __version_info__ < (20, 0, 0, "alpha", 1):
19 raise RuntimeError(
20 f"This example is not compatible with your current PTB version {TG_VER}. To view the "
21 f"{TG_VER} version of this example, "
22 f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
23 )
24from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
25from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes
26
27# Enable logging
28logging.basicConfig(
29 format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
30)
31logger = logging.getLogger(__name__)
32
33
34async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
35 """Sends a message with three inline buttons attached."""
36 keyboard = [
37 [
38 InlineKeyboardButton("Option 1", callback_data="1"),
39 InlineKeyboardButton("Option 2", callback_data="2"),
40 ],
41 [InlineKeyboardButton("Option 3", callback_data="3")],
42 ]
43
44 reply_markup = InlineKeyboardMarkup(keyboard)
45
46 await update.message.reply_text("Please choose:", reply_markup=reply_markup)
47
48
49async def button(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
50 """Parses the CallbackQuery and updates the message text."""
51 query = update.callback_query
52
53 # CallbackQueries need to be answered, even if no notification to the user is needed
54 # Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
55 await query.answer()
56
57 await query.edit_message_text(text=f"Selected option: {query.data}")
58
59
60async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
61 """Displays info on how to use the bot."""
62 await update.message.reply_text("Use /start to test this bot.")
63
64
65def main() -> None:
66 """Run the bot."""
67 # Create the Application and pass it your bot's token.
68 application = Application.builder().token("TOKEN").build()
69
70 application.add_handler(CommandHandler("start", start))
71 application.add_handler(CallbackQueryHandler(button))
72 application.add_handler(CommandHandler("help", help_command))
73
74 # Run the bot until the user presses Ctrl-C
75 application.run_polling()
76
77
78if __name__ == "__main__":
79 main()
Powered by cgit v1.2.3 (git 2.41.0)