-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmini_app_example.py
More file actions
124 lines (112 loc) · 4.17 KB
/
mini_app_example.py
File metadata and controls
124 lines (112 loc) · 4.17 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import logging
import re
from swibots import (
Client,
BotContext,
CommandEvent,
CallbackQueryEvent,
BotCommand,
AppPage,
Text,
Button,
ButtonGroup,
Image,
VideoPlayer,
Carousel,
Grid,
GridItem,
SearchBar,
Dropdown,
ListItem,
ScreenType,
TextSize,
InlineKeyboardButton,
InlineMarkup,
regexp
)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
TOKEN = "YOUR_BOT_TOKEN_HERE"
app = Client(TOKEN, is_app=True, home_callback="open_app")
app.set_bot_commands([BotCommand("start", "Open the mini app", True)])
@app.on_command("start")
async def start_command(ctx: BotContext[CommandEvent]):
await ctx.event.message.reply_text(
"Welcome to the Mini App Example! Click the button below to open the app.",
inline_markup=InlineMarkup([[
InlineKeyboardButton("Open Mini App", callback_data="open_app", app=True)
]])
)
@app.on_callback_query(regexp('open_app'))
async def open_app(ctx: BotContext[CallbackQueryEvent]):
page = AppPage(
screen=ScreenType.SCREEN,
components=[
Text("Welcome to the Mini App Example!", size=TextSize.LARGE),
Image("https://example.com/sample-image.jpg", callback_data="image_clicked"),
ButtonGroup([
Button("Show Video", callback_data="show_video"),
Button("Show Grid", callback_data="show_grid"),
]),
SearchBar(callback_data="search"),
Dropdown(
"Select an option",
options=[
ListItem("Option 1", callback_data="option_1"),
ListItem("Option 2", callback_data="option_2"),
ListItem("Option 3", callback_data="option_3"),
]
),
]
)
await ctx.event.answer(callback=page)
@app.on_callback_query(regexp('show_video'))
async def show_video(ctx: BotContext[CallbackQueryEvent]):
page = AppPage(
screen=ScreenType.SCREEN,
components=[
Text("Video Player Example", size=TextSize.MEDIUM),
VideoPlayer("https://example.com/sample-video.mp4", title="Sample Video"),
Button("Back to Home", callback_data="open_app"),
]
)
await ctx.event.answer(callback=page)
@app.on_callback_query(regexp('show_grid'))
async def show_grid(ctx: BotContext[CallbackQueryEvent]):
page = AppPage(
screen=ScreenType.SCREEN,
components=[
Text("Grid Example", size=TextSize.MEDIUM),
Grid(
title="Sample Grid",
options=[
GridItem("Item 1", "https://example.com/icon1.png", callback_data="grid_item_1"),
GridItem("Item 2", "https://example.com/icon2.png", callback_data="grid_item_2"),
GridItem("Item 3", "https://example.com/icon3.png", callback_data="grid_item_3"),
GridItem("Item 4", "https://example.com/icon4.png", callback_data="grid_item_4"),
]
),
Button("Back to Home", callback_data="open_app"),
]
)
await ctx.event.answer(callback=page)
@app.on_callback_query(regexp('search'))
async def handle_search(ctx: BotContext[CallbackQueryEvent]):
query = ctx.event.details.get("searchQuery")
if query:
await ctx.event.answer(f"You searched for: {query}", show_alert=True)
else:
await ctx.event.answer("Please enter a search query", show_alert=True)
@app.on_callback_query(regexp('option_.*'))
async def handle_dropdown(ctx: BotContext[CallbackQueryEvent]):
option = ctx.event.callback_data.split("_")[1]
await ctx.event.answer(f"You selected Option {option}", show_alert=True)
@app.on_callback_query(regexp('grid_item_.*'))
async def handle_grid_item(ctx: BotContext[CallbackQueryEvent]):
item = ctx.event.callback_data.split("_")[-1]
await ctx.event.answer(f"You clicked Grid Item {item}", show_alert=True)
@app.on_callback_query(regexp('image_clicked'))
async def handle_image_click(ctx: BotContext[CallbackQueryEvent]):
await ctx.event.answer("You clicked the image!", show_alert=True)
if __name__ == "__main__":
app.run()