Skip to content

Commit

Permalink
new util: convert List[List[Tuple]] to InlineKeyboardMarkup
Browse files Browse the repository at this point in the history
  • Loading branch information
z44d committed Nov 18, 2024
1 parent 88fb21f commit 0aa71eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 7 additions & 1 deletion tgram/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from .mention import Mention
from .parsers import markdown_unparse, html_unparse, String
from .parse_mode import get_parse_mode
from .types import message_origin_parse, convert_input_media, reaction_type_parse
from .types import (
message_origin_parse,
convert_input_media,
reaction_type_parse,
convert_to_inline_keyboard_markup,
)

from ..handlers import Handlers
from typing import List
Expand Down Expand Up @@ -37,4 +42,5 @@
"ReadableTime",
"get_parse_mode",
"reaction_type_parse",
"convert_to_inline_keyboard_markup",
]
24 changes: 23 additions & 1 deletion tgram/utils/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import tgram
import os
import re

from typing import Optional, List, Union
from typing import Optional, List, Union, Tuple
from pathlib import Path
from io import BytesIO

Expand Down Expand Up @@ -72,3 +73,24 @@ def reaction_type_parse(
)
for i in x
]


pattern = re.compile(
r"^(https?):\/\/" r"([a-zA-Z0-9.-]+)" r"(\.[a-zA-Z]{2,})" r"(\/[^\s]*)?$"
)


def convert_to_inline_keyboard_markup(v: List[List[Tuple]]):
return tgram.types.InlineKeyboardMarkup(
[
[
tgram.InlineKeyboardButton(
x,
callback_data=y if not re.match(pattern, y) else None,
url=y if re.match(pattern, y) else None,
)
for x, y in z
]
for z in v
]
)

0 comments on commit 0aa71eb

Please sign in to comment.