-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathhelpers.py
More file actions
190 lines (151 loc) · 5.04 KB
/
helpers.py
File metadata and controls
190 lines (151 loc) · 5.04 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
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
181
182
183
184
185
186
187
188
189
190
# Hydrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2020-present Cezar H. <https://github.com/usernein>
# Copyright (C) 2023-present Hydrogram <https://hydrogram.org>
#
# This file is part of Hydrogram.
#
# Hydrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Hydrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Hydrogram. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
from hydrogram.types import (
ForceReply,
InlineKeyboardButton,
InlineKeyboardMarkup,
KeyboardButton,
ReplyKeyboardMarkup,
)
def ikb(rows: list[list[str | tuple[str, str]]] | None = None) -> InlineKeyboardMarkup:
"""
Create an InlineKeyboardMarkup from a list of lists of buttons.
Parameters:
rows (List[List[Union[str, Tuple[str, str]]]]):
List of lists of buttons. Defaults to empty list.
Returns:
:obj:`~hydrogram.types.InlineKeyboardMarkup`: An InlineKeyboardMarkup object.
"""
if rows is None:
rows = []
lines = []
for row in rows:
line = []
for button in row:
button = btn(button, button) if isinstance(button, str) else btn(*button)
line.append(button)
lines.append(line)
return InlineKeyboardMarkup(inline_keyboard=lines)
def btn(text: str, value: str, type="callback_data") -> InlineKeyboardButton:
"""
Create an InlineKeyboardButton.
Parameters:
text (str):
Text of the button.
value (str):
Value of the button.
type (str):
Type of the button. Defaults to "callback_data".
Returns:
:obj:`~hydrogram.types.InlineKeyboardButton`: An InlineKeyboardButton object.
"""
return InlineKeyboardButton(text, **{type: value})
# The inverse of ikb()
def bki(keyboard: InlineKeyboardButton) -> list[list[str | tuple[str, str]]]:
"""
Create a list of lists of buttons from an InlineKeyboardMarkup.
Parameters:
keyboard (:obj:`~hydrogram.types.InlineKeyboardMarkup`):
An InlineKeyboardMarkup object.
Returns:
List of lists of buttons.
"""
lines = []
for row in keyboard.inline_keyboard:
line = []
for button in row:
button = ntb(button)
line.append(button)
lines.append(line)
return lines
def ntb(button: InlineKeyboardButton) -> list:
"""
Create a button list from an InlineKeyboardButton.
Parameters:
button (:obj:`~hydrogram.types.InlineKeyboardButton`):
An InlineKeyboardButton object.
Returns:
``list``: A button list.
"""
for btn_type in [
"callback_data",
"url",
"switch_inline_query",
"switch_inline_query_current_chat",
"callback_game",
]:
value = getattr(button, btn_type)
if value:
break
button = [button.text, value]
if btn_type != "callback_data":
button.append(btn_type)
return button
def kb(rows=None, **kwargs) -> ReplyKeyboardMarkup:
"""
Create a ReplyKeyboardMarkup from a list of lists of buttons.
Parameters:
rows (List[List[str]]):
List of lists of buttons. Defaults to an empty list.
kwargs:
Other arguments to pass to ReplyKeyboardMarkup.
Returns:
:obj:`~hydrogram.types.ReplyKeyboardMarkup`: A ReplyKeyboardMarkup object.
"""
if rows is None:
rows = []
lines = []
for row in rows:
line = []
for button in row:
button_type = type(button)
if isinstance(button_type, str):
button = KeyboardButton(button)
elif isinstance(button_type, dict):
button = KeyboardButton(**button)
line.append(button)
lines.append(line)
return ReplyKeyboardMarkup(keyboard=lines, **kwargs)
kbtn = KeyboardButton
"""
Create a KeyboardButton.
"""
def force_reply(selective=True) -> ForceReply:
"""
Create a ForceReply.
Parameters:
selective (bool):
Whether the reply should be selective. Defaults to True.
Returns:
:obj:`~hydrogram.types.ForceReply`: A ForceReply object.
"""
return ForceReply(selective=selective)
def array_chunk(input_array, size) -> list[list]:
"""
Split an array into chunks.
Parameters:
input_array (list):
The array to split.
size (int):
The size of each chunk.
Returns:
list: A list of chunks.
"""
return [input_array[i : i + size] for i in range(0, len(input_array), size)]