-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinline_keyboard.py
72 lines (55 loc) · 1.84 KB
/
inline_keyboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import logging
import os
from dotenv import load_dotenv
from swibots import (
BotApp,
BotContext,
CallbackQueryEvent,
CommandEvent,
BotCommand,
InlineMarkup,
InlineKeyboardButton,
regexp,
)
env_file = os.path.join(os.path.dirname(__file__), "..", "..", ".env")
load_dotenv(env_file)
TOKEN = os.getenv("TOKEN")
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# initialize the app and register commands
app = BotApp(
TOKEN,
).set_bot_commands([BotCommand("buttons", "Get Buttons")])
# register buttons command
@app.on_command("buttons")
async def buttons_handler(ctx: BotContext[CommandEvent]):
try:
m = ctx.event.message
message = f"Please select an option:"
inline_keyboard = [
[
InlineKeyboardButton(text="Option 1", callback_data="option1"),
InlineKeyboardButton(text="Option 2", callback_data="option2"),
],
[
InlineKeyboardButton(text="Option 3", callback_data="option3"),
InlineKeyboardButton(text="Option 4", callback_data="option4"),
],
]
inline_markup = InlineMarkup(inline_keyboard=inline_keyboard)
await ctx.event.reply_text(message, inline_markup=inline_markup)
except Exception as e:
logger.exception(f"Error while processing event: {e}")
raise e
# handle callback query
@app.on_callback_query(regexp(r"option(\d)"))
async def callback_query_handler(ctx: BotContext[CallbackQueryEvent]):
try:
message = ctx.event.message
await message.edit_text(
f"Option with data: {ctx.event.message.matches[0].group(1)} selected!" # The option number is given back
)
except Exception as e:
logger.exception(f"Error while processing event: {e}")
raise e
app.run()