diff options
author | clarkzjw <[email protected]> | 2023-02-20 01:03:57 -0800 |
---|---|---|
committer | clarkzjw <[email protected]> | 2023-02-20 01:03:57 -0800 |
commit | 76597b5c619649c2457cb876a2c749e9c7a4a248 (patch) | |
tree | 80e89c7547aad3986b9f1f6afe900b3d1a6e5756 | |
parent | edbb47990eac81a1c81a0f0b0f1e63d2d8f7e619 (diff) | |
download | swarm2fediverse-76597b5c619649c2457cb876a2c749e9c7a4a248.tar.gz |
add python-telegram-bot inline keyboard example
-rw-r--r-- | bot.py | 79 | ||||
-rw-r--r-- | requirements.txt | 12 |
2 files changed, 91 insertions, 0 deletions
@@ -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 | """ | ||
6 | Basic 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 | """ | ||
9 | import logging | ||
10 | |||
11 | from telegram import __version__ as TG_VER | ||
12 | |||
13 | try: | ||
14 | from telegram import __version_info__ | ||
15 | except ImportError: | ||
16 | __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] | ||
17 | |||
18 | if __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 | ) | ||
24 | from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update | ||
25 | from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes | ||
26 | |||
27 | # Enable logging | ||
28 | logging.basicConfig( | ||
29 | format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO | ||
30 | ) | ||
31 | logger = logging.getLogger(__name__) | ||
32 | |||
33 | |||
34 | async 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 | |||
49 | async 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 | |||
60 | async 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 | |||
65 | def 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 | |||
78 | if __name__ == "__main__": | ||
79 | main() | ||
diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..33a4479 --- /dev/null +++ b/requirements.txt | |||
@@ -0,0 +1,12 @@ | |||
1 | anyio==3.6.2 | ||
2 | certifi==2022.12.7 | ||
3 | h11==0.14.0 | ||
4 | h2==4.1.0 | ||
5 | hpack==4.0.0 | ||
6 | httpcore==0.16.3 | ||
7 | httpx==0.23.3 | ||
8 | hyperframe==6.0.1 | ||
9 | idna==3.4 | ||
10 | python-telegram-bot==20.1 | ||
11 | rfc3986==1.5.0 | ||
12 | sniffio==1.3.0 | ||