Skip to content

Commit 3fa4839

Browse files
committed
SendHtml: support button style= and icon=
1 parent e10568f commit 3fa4839

1 file changed

Lines changed: 29 additions & 15 deletions

File tree

src/Telegram.Bot/Extensions/FormatExtensions.cs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -588,20 +588,22 @@ private static ReplyMarkup ParseHtmlKeyboard(ReadOnlySpan<char> keyboard)
588588
else if (keyboard.StartsWith("</row>", StringComparison.OrdinalIgnoreCase)) { }
589589
else if (CheckHtmlArg(ref keyboard, "<button text=\"", out var text))
590590
{
591+
IKeyboardButton button;
591592
if (inline != null)
592593
{
594+
InlineKeyboardButton ikb;
593595
if (CheckHtmlArg(ref keyboard, "url=\"", out var url))
594-
inline.AddButton(InlineKeyboardButton.WithUrl(text, url));
596+
inline.AddButton(ikb = InlineKeyboardButton.WithUrl(text, url));
595597
else if (CheckHtmlArg(ref keyboard, "callback=\"", out var data))
596-
inline.AddButton(InlineKeyboardButton.WithCallbackData(text, data));
598+
inline.AddButton(ikb = InlineKeyboardButton.WithCallbackData(text, data));
597599
else if (CheckHtmlArg(ref keyboard, "app=\"", out var app))
598-
inline.AddButton(InlineKeyboardButton.WithWebApp(text, app));
600+
inline.AddButton(ikb = InlineKeyboardButton.WithWebApp(text, app));
599601
else if (CheckHtmlArg(ref keyboard, "copy=\"", out var copy))
600-
inline.AddButton(InlineKeyboardButton.WithCopyText(text, copy));
602+
inline.AddButton(ikb = InlineKeyboardButton.WithCopyText(text, copy));
601603
else if (CheckHtmlArg(ref keyboard, "switch_inline=\"", out var query))
602604
if (CheckHtmlArg(ref keyboard, "target=\"", out var target))
603605
if (Enum.TryParse<SwitchInlineTarget>(target, ignoreCase: true, out var targets))
604-
inline.AddButton(InlineKeyboardButton.WithSwitchInlineQueryChosenChat(text, new()
606+
inline.AddButton(ikb = InlineKeyboardButton.WithSwitchInlineQueryChosenChat(text, new()
605607
{
606608
Query = query,
607609
AllowUserChats = targets.HasFlag(SwitchInlineTarget.User),
@@ -610,27 +612,39 @@ private static ReplyMarkup ParseHtmlKeyboard(ReadOnlySpan<char> keyboard)
610612
AllowChannelChats = targets.HasFlag(SwitchInlineTarget.Channel),
611613
}));
612614
else
613-
inline.AddButton(InlineKeyboardButton.WithSwitchInlineQuery(text, query));
615+
inline.AddButton(ikb = InlineKeyboardButton.WithSwitchInlineQuery(text, query));
614616
else
615-
inline.AddButton(InlineKeyboardButton.WithSwitchInlineQueryCurrentChat(text, query));
617+
inline.AddButton(ikb = InlineKeyboardButton.WithSwitchInlineQueryCurrentChat(text, query));
616618
else
617619
throw new FormatException("Unrecognized inline <button> type");
620+
button = ikb;
618621
}
619622
else if (reply != null)
620623
{
621-
if (keyboard[0] is '>' or '/')
622-
reply.AddButton(text);
623-
else if (CheckHtmlArg(ref keyboard, "request_contact", out _))
624-
reply.AddButton(KeyboardButton.WithRequestContact(text));
624+
KeyboardButton kb;
625+
if (CheckHtmlArg(ref keyboard, "request_contact", out _))
626+
reply.AddButton(kb = KeyboardButton.WithRequestContact(text));
625627
else if (CheckHtmlArg(ref keyboard, "request_location", out _))
626-
reply.AddButton(KeyboardButton.WithRequestLocation(text));
628+
reply.AddButton(kb = KeyboardButton.WithRequestLocation(text));
627629
else if (CheckHtmlArg(ref keyboard, "request_poll=\"", out var pollType))
628-
reply.AddButton(KeyboardButton.WithRequestPoll(text, pollType is "" or "any" ? (PollType?)null : Enum.Parse<PollType>(pollType, ignoreCase: true)));
630+
reply.AddButton(kb = KeyboardButton.WithRequestPoll(text, pollType is "" or "any" ? (PollType?)null : Enum.Parse<PollType>(pollType, ignoreCase: true)));
629631
//TO-DO: support request_users and request_chat?
630632
else if (CheckHtmlArg(ref keyboard, "app=\"", out var app))
631-
reply.AddButton(KeyboardButton.WithWebApp(text, app));
633+
reply.AddButton(kb = KeyboardButton.WithWebApp(text, app));
632634
else
633-
throw new FormatException("Unrecognized reply <button> type");
635+
reply.AddButton(kb = new KeyboardButton(text));
636+
button = kb;
637+
}
638+
else
639+
throw new FormatException("Invalid keyboard");
640+
while (true)
641+
{
642+
if (CheckHtmlArg(ref keyboard, "style=\"", out var style))
643+
button.Style = Enum.Parse<KeyboardButtonStyle>(style, true);
644+
else if (CheckHtmlArg(ref keyboard, "icon=\"", out var emojiId))
645+
button.IconCustomEmojiId = emojiId;
646+
else
647+
break;
634648
}
635649
}
636650
keyboard = keyboard[(keyboard.IndexOf('>') + 1)..].Trim();

0 commit comments

Comments
 (0)