Update Telegram bot to support URLs in inlineKeyboard (#70445)

* Update Telegram bot to support URLs in inlineKeyboard

Update creation of InlineKeyboardButton to support url case, on top of callbacks

* linting

* Apply suggestions from code review

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Maxim Oei 2023-06-28 12:56:00 +02:00 committed by GitHub
parent 8b563120b5
commit 36b0fc17df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -529,17 +529,24 @@ class TelegramNotificationService:
(text_b2, data_callback_b2), ...] (text_b2, data_callback_b2), ...]
- a string like: `/cmd1, /cmd2, /cmd3` - a string like: `/cmd1, /cmd2, /cmd3`
- or a string like: `text_b1:/cmd1, text_b2:/cmd2` - or a string like: `text_b1:/cmd1, text_b2:/cmd2`
- also supports urls instead of callback commands
""" """
buttons = [] buttons = []
if isinstance(row_keyboard, str): if isinstance(row_keyboard, str):
for key in row_keyboard.split(","): for key in row_keyboard.split(","):
if ":/" in key: if ":/" in key:
# commands like: 'Label:/cmd' become ('Label', '/cmd') # check if command or URL
label = key.split(":/")[0] if key.startswith("https://"):
command = key[len(label) + 1 :] label = key.split(",")[0]
buttons.append( url = key[len(label) + 1 :]
InlineKeyboardButton(label, callback_data=command) buttons.append(InlineKeyboardButton(label, url=url))
) else:
# commands like: 'Label:/cmd' become ('Label', '/cmd')
label = key.split(":/")[0]
command = key[len(label) + 1 :]
buttons.append(
InlineKeyboardButton(label, callback_data=command)
)
else: else:
# commands like: '/cmd' become ('CMD', '/cmd') # commands like: '/cmd' become ('CMD', '/cmd')
label = key.strip()[1:].upper() label = key.strip()[1:].upper()
@ -547,9 +554,12 @@ class TelegramNotificationService:
elif isinstance(row_keyboard, list): elif isinstance(row_keyboard, list):
for entry in row_keyboard: for entry in row_keyboard:
text_btn, data_btn = entry text_btn, data_btn = entry
buttons.append( if data_btn.startswith("https://"):
InlineKeyboardButton(text_btn, callback_data=data_btn) buttons.append(InlineKeyboardButton(text_btn, url=data_btn))
) else:
buttons.append(
InlineKeyboardButton(text_btn, callback_data=data_btn)
)
else: else:
raise TypeError(str(row_keyboard)) raise TypeError(str(row_keyboard))
return buttons return buttons