Spaces:
Paused
Paused
iamgojoof6eyes
commited on
Commit
·
9e0dfb0
1
Parent(s):
c9e2ecc
Changing keyboard genration method
Browse files- Powers/utils/kbhelpers.py +35 -14
Powers/utils/kbhelpers.py
CHANGED
|
@@ -1,18 +1,39 @@
|
|
| 1 |
-
from
|
|
|
|
|
|
|
| 2 |
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
for button in row:
|
| 11 |
-
button = btn(*button)
|
| 12 |
-
line.append(button)
|
| 13 |
-
lines.append(line)
|
| 14 |
-
return InlineKeyboardMarkup(inline_keyboard=lines)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from re import findall
|
| 2 |
+
from pyrogram.types import InlineKeyboardButton as kb
|
| 3 |
+
from pykeyboard import InlineKeyboard
|
| 4 |
|
| 5 |
+
# CREDIT WILLIAM BUTCHER BOT
|
| 6 |
|
| 7 |
+
def is_url(text: str) -> bool:
|
| 8 |
+
regex = r"""(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]
|
| 9 |
+
[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(
|
| 10 |
+
\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\
|
| 11 |
+
()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))""".strip()
|
| 12 |
+
return [x[0] for x in findall(regex, str(text))]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def keyboard(buttons_list, row_width: int = 2):
|
| 15 |
+
"""
|
| 16 |
+
Buttons builder, pass buttons in a list and it will
|
| 17 |
+
return pyrogram.types.IKB object
|
| 18 |
+
Ex: keyboard([["click here", "https://google.com"]])
|
| 19 |
+
if theres, a url, it will make url button, else callback button
|
| 20 |
+
"""
|
| 21 |
+
buttons = InlineKeyboard(row_width=row_width)
|
| 22 |
+
data = [
|
| 23 |
+
(
|
| 24 |
+
kb(text=str(i[0]), callback_data=str(i[1]))
|
| 25 |
+
if not is_url(i[1])
|
| 26 |
+
else kb(text=str(i[0]), url=str(i[1]))
|
| 27 |
+
)
|
| 28 |
+
for i in buttons_list
|
| 29 |
+
]
|
| 30 |
+
buttons.add(*data)
|
| 31 |
+
return buttons
|
| 32 |
|
| 33 |
+
|
| 34 |
+
def ikb(data: dict, row_width: int = 2):
|
| 35 |
+
"""
|
| 36 |
+
Converts a dict to pyrogram buttons
|
| 37 |
+
Ex: dict_to_keyboard({"click here": "this is callback data"})
|
| 38 |
+
"""
|
| 39 |
+
return keyboard(data.items(), row_width=row_width)
|