-
Notifications
You must be signed in to change notification settings - Fork 8
/
example_download.py
180 lines (147 loc) · 5.53 KB
/
example_download.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from swibots import (
BotApp,
BotCommand,
BotContext,
CallbackQueryEvent,
CommandEvent,
InlineMarkup,
InlineKeyboardButton,
regexp,
CommunityUpdatedEvent,
MessageEvent,
Message,
DownloadProgress,
)
import logging
logging.basicConfig(level=logging.INFO)
TOKEN = "YOUR_TOKEN_HERE"
app = BotApp(
TOKEN, "A cool bot with annotations and everything you could possibly want :)"
).set_bot_commands(
[
BotCommand("echo", "Echoes the message", True),
BotCommand("buttons", "Shows buttons", True),
BotCommand("buttonfull", "Shows buttons", True),
BotCommand("back", "Shows buttons", True),
BotCommand("upload", "Reply with media", True),
]
)
@app.on_command("buttons")
async def buttons_handler(ctx: BotContext[CommandEvent]):
message = f"Please select an option:"
inline_keyboard = [
[
InlineKeyboardButton(text="Option 18", callback_data="option18"),
InlineKeyboardButton(text="Option 19", callback_data="option19"),
InlineKeyboardButton(text="Option 20", callback_data="option20"),
],
[
InlineKeyboardButton(text="Option 33", callback_data="option33"),
InlineKeyboardButton(text="Option 43", callback_data="option43"),
InlineKeyboardButton(text="Option 63", callback_data="option63"),
InlineKeyboardButton(text="Option 73", callback_data="option73"),
],
[
InlineKeyboardButton(text="Optioniablek", callback_data="option53"),
InlineKeyboardButton(text="Unitedstates", callback_data="option03"),
],
[
InlineKeyboardButton(text="Go Back on Press", callback_data="option543"),
],
[
InlineKeyboardButton(
text="Go Back on Press for more Movies haha", callback_data="option543"
),
],
]
inline_markup = InlineMarkup(
inline_keyboard=inline_keyboard,
)
await ctx.event.message.reply_text(message, inline_markup=inline_markup)
@app.on_command("test")
async def test_handler(ctx: BotContext[CommandEvent]):
await ctx.event.message.reply_text("Test command received")
@app.on_command("buttonfull")
async def buttons_handler(ctx: BotContext[CommandEvent]):
message = f"Please select an option:"
inline_keyboard1 = [
[
InlineKeyboardButton(text="Option 1111", callback_data="option1"),
InlineKeyboardButton(text="Option 1112", callback_data="option2"),
InlineKeyboardButton(text="Option 1115", callback_data="option5"),
],
[
InlineKeyboardButton(text="Option 18", callback_data="option18"),
InlineKeyboardButton(text="Option 19", callback_data="option19"),
InlineKeyboardButton(text="Option 20", callback_data="option20"),
],
[
InlineKeyboardButton(text="Optioniablek", callback_data="option53"),
InlineKeyboardButton(text="Unitedstates", callback_data="option03"),
],
[
InlineKeyboardButton(text="Go Back on Press", callback_data="option543"),
],
[
InlineKeyboardButton(
text="Go Back on Press for more Movies haha", callback_data="option543"
),
],
]
inline_markup = InlineMarkup(
inline_keyboard=inline_keyboard1,
)
await ctx.event.message.reply_text(message, inline_markup=inline_markup)
@app.on_command("echo")
async def buttons_handler(ctx: BotContext[CommandEvent]):
message = "Please select an option:"
inline_keyboard1 = [
[
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"),
],
]
await ctx.event.message.reply_text(
"Please select an option (echo):",
inline_markup=InlineMarkup(
inline_keyboard=inline_keyboard1,
),
)
@app.on_callback_query()
async def query_callback_handler(ctx: BotContext[CallbackQueryEvent]):
m = ctx.event.message
await m.edit_text(f"Thank you! I received your callback: {ctx.event.callback_data}")
@app.on_message()
async def message_handler(ctx: BotContext[MessageEvent]):
# m = await ctx.prepare_response_message(ctx.event.message)
# m.message = f"Thank you! I received your message: {ctx.event.message.message}"
# await ctx.send_message(m)
print(f"Downloading received message: {ctx.event.message.id}")
message: Message = ctx.event.message
if message.is_media:
print(message.media_link)
await message.download(
in_memory=False, block=False, progress=handle_download_progress
)
async def handle_download_progress(progress: DownloadProgress):
print(f"Downloaded {progress.downloaded} of {progress.total}")
@app.on_community_update()
async def community_update_handler(ctx: BotContext[CommunityUpdatedEvent]):
print(ctx.event.community_id + " was updated")
# app.run()
async def show_upload_progress(obj):
print(f"Uploaded {obj.current} of {obj.readed}")
if obj == 0:
return
@app.on_command("upload")
async def upload_handler(ctx: BotContext[CommandEvent]):
params = ctx.event.params
r = await ctx.event.message.reply_media(
f"Here is your file {params}", document=params, progress=show_upload_progress
)
print(r)
app.run()