Skip to content

Commit 52eb623

Browse files
committed
Add style field support for keyboard buttons
1 parent 729cf5d commit 52eb623

File tree

2 files changed

+112
-46
lines changed

2 files changed

+112
-46
lines changed

test/testtelegram.pas

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ TTestReactionUpdates=class(TTestCase)
8585
procedure ParseMessageReactionCountUpdate;
8686
end;
8787

88+
{ TTestSenderTypes }
89+
90+
TTestSenderTypes=class(TTestCase)
91+
published
92+
procedure KeyboardButtonsStyle;
93+
end;
94+
8895
{ TTestReceiveLongPollingBase }
8996
{ Test receiving updates via longpolling from the test bot. Please send test messages to the bot
9097
immediately before running the test! }
@@ -467,6 +474,38 @@ procedure TTestMessageQuote.ParseMessageExternalReply;
467474
end;
468475
end;
469476

477+
{ TTestSenderTypes }
478+
479+
procedure TTestSenderTypes.KeyboardButtonsStyle;
480+
var
481+
aReplyMarkup: TReplyMarkup;
482+
aReplyButtons: TKeyboardButtons;
483+
aInlineButtons: TInlineKeyboardButtons;
484+
aReplyButton: TKeyboardButton;
485+
aInlineButton: TInlineKeyboardButton;
486+
begin
487+
aReplyMarkup:=TReplyMarkup.Create;
488+
try
489+
aReplyButtons:=aReplyMarkup.CreateReplyKeyboard.Add;
490+
aReplyButton:=TKeyboardButton.Create('Reply button');
491+
aReplyButton.Style:='primary';
492+
aReplyButtons.Add(aReplyButton);
493+
494+
aInlineButtons:=aReplyMarkup.CreateInlineKeyBoard.Add;
495+
aInlineButton:=TInlineKeyboardButton.Create('Inline button');
496+
aInlineButton.Callback_Data:='cb';
497+
aInlineButton.Style:='secondary';
498+
aInlineButtons.Add(aInlineButton);
499+
500+
AssertEquals('primary', aReplyButton.Style);
501+
AssertEquals('secondary', aInlineButton.Style);
502+
AssertTrue('Reply keyboard JSON should contain style', Pos('"style" : "primary"', aReplyMarkup.AsJSON)>0);
503+
AssertTrue('Inline keyboard JSON should contain style', Pos('"style" : "secondary"', aReplyMarkup.AsJSON)>0);
504+
finally
505+
aReplyMarkup.Free;
506+
end;
507+
end;
508+
470509
{ TTestSender }
471510

472511
procedure TTestSender.SetUp;
@@ -794,6 +833,6 @@ procedure TTestSender.getChatAdministators;
794833

795834
initialization
796835
RegisterTests([TTestSender, TTestSenderProcedure, TTestProxySender, TTestReceiveLongPolling,
797-
TTestPayments, TTestReactionUpdates, TTestMessageQuote]);
836+
TTestPayments, TTestReactionUpdates, TTestMessageQuote, TTestSenderTypes]);
798837

799838
end.

tgsendertypes.pas

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,23 @@ TReplyMarkupClass=class of TReplyMarkup;
104104

105105
{ TKeyboardButton }
106106

107-
TKeyboardButton = class(TJSONObject)
108-
private
109-
function GetRequestContact: Boolean;
110-
function GetRequestLocation: Boolean;
111-
function Gettext: String;
112-
procedure SetRequestContact(AValue: Boolean);
113-
procedure SetRequestLocation(AValue: Boolean);
114-
procedure Settext(AValue: String);
115-
public
116-
constructor Create(const AText: String);
117-
property text: String read Gettext write Settext;
118-
property RequestContact: Boolean read GetRequestContact write SetRequestContact; // Optional
119-
property RequestLocation: Boolean read GetRequestLocation write SetRequestLocation; // Optional
120-
end;
107+
TKeyboardButton = class(TJSONObject)
108+
private
109+
function GetRequestContact: Boolean;
110+
function GetRequestLocation: Boolean;
111+
function GetStyle: String;
112+
function Gettext: String;
113+
procedure SetRequestContact(AValue: Boolean);
114+
procedure SetRequestLocation(AValue: Boolean);
115+
procedure SetStyle(AValue: String);
116+
procedure Settext(AValue: String);
117+
public
118+
constructor Create(const AText: String);
119+
property text: String read Gettext write Settext;
120+
property Style: String read GetStyle write SetStyle; // Optional
121+
property RequestContact: Boolean read GetRequestContact write SetRequestContact; // Optional
122+
property RequestLocation: Boolean read GetRequestLocation write SetRequestLocation; // Optional
123+
end;
121124

122125
{ TKeyboardButtons }
123126

@@ -140,21 +143,24 @@ TInlineKeyboardButton = class(TJSONObject)
140143
function CheckOptnlNull: Boolean;
141144
procedure CheckOptnlAndSet(const ParamName, ParamValue: String);
142145
function Getcallback_data: String;
143-
function Getswitch_inline_query: String;
144-
function Getswitch_inline_query_current_chat: String;
145-
function Gettext: String;
146-
function Geturl: String;
147-
procedure Setcallback_data(AValue: String);
148-
procedure Setswitch_inline_query(AValue: String);
149-
procedure Setswitch_inline_query_current_chat(AValue: String);
150-
procedure Settext(AValue: String);
146+
function Getswitch_inline_query: String;
147+
function Getswitch_inline_query_current_chat: String;
148+
function GetStyle: String;
149+
function Gettext: String;
150+
function Geturl: String;
151+
procedure Setcallback_data(AValue: String);
152+
procedure SetStyle(AValue: String);
153+
procedure Setswitch_inline_query(AValue: String);
154+
procedure Setswitch_inline_query_current_chat(AValue: String);
155+
procedure Settext(AValue: String);
151156
procedure Seturl(AValue: String);
152157
public
153158
constructor Create(const AText: String);
154-
property text: String read Gettext write Settext;
155-
property url: String read Geturl write Seturl;
156-
property callback_data: String read Getcallback_data write Setcallback_data;
157-
property switch_inline_query: String read Getswitch_inline_query write Setswitch_inline_query;
159+
property text: String read Gettext write Settext;
160+
property Style: String read GetStyle write SetStyle;
161+
property url: String read Geturl write Seturl;
162+
property callback_data: String read Getcallback_data write Setcallback_data;
163+
property switch_inline_query: String read Getswitch_inline_query write Setswitch_inline_query;
158164
property switch_inline_query_current_chat: String read Getswitch_inline_query_current_chat
159165
write Setswitch_inline_query_current_chat;
160166
end;
@@ -854,9 +860,10 @@ implementation
854860
s_RemoveKeyboard = 'remove_keyboard';
855861
s_ResizeKeyboard = 'resize_keyboard';
856862
s_OneTimeKeyboard = 'one_time_keyboard';
857-
s_RequestContact = 'request_contact';
858-
s_RequestLocation = 'request_location';
859-
s_SwitchInlineQuery = 'switch_inline_query';
863+
s_RequestContact = 'request_contact';
864+
s_RequestLocation = 'request_location';
865+
s_Style = 'style';
866+
s_SwitchInlineQuery = 'switch_inline_query';
860867
s_CallbackData = 'callback_data';
861868
s_SwitchInlineQueryCurrentChat = 's_switch_inline_query_current_chat';
862869
s_Selective = 'selective';
@@ -1692,10 +1699,15 @@ function TKeyboardButton.GetRequestLocation: Boolean;
16921699
Result:=Get(s_RequestLocation, False);
16931700
end;
16941701

1695-
function TKeyboardButton.Gettext: String;
1696-
begin
1697-
Result:=Strings[s_text];
1698-
end;
1702+
function TKeyboardButton.Gettext: String;
1703+
begin
1704+
Result:=Strings[s_text];
1705+
end;
1706+
1707+
function TKeyboardButton.GetStyle: String;
1708+
begin
1709+
Result:=Strings[s_Style];
1710+
end;
16991711

17001712
procedure TKeyboardButton.SetRequestContact(AValue: Boolean);
17011713
begin
@@ -1707,10 +1719,15 @@ procedure TKeyboardButton.SetRequestLocation(AValue: Boolean);
17071719
Booleans[s_RequestLocation]:=AValue;
17081720
end;
17091721

1710-
procedure TKeyboardButton.Settext(AValue: String);
1711-
begin
1712-
Strings[s_text]:=AValue;
1713-
end;
1722+
procedure TKeyboardButton.Settext(AValue: String);
1723+
begin
1724+
Strings[s_text]:=AValue;
1725+
end;
1726+
1727+
procedure TKeyboardButton.SetStyle(AValue: String);
1728+
begin
1729+
Strings[s_Style]:=AValue;
1730+
end;
17141731

17151732
constructor TKeyboardButton.Create(const AText: String);
17161733
begin
@@ -1905,20 +1922,30 @@ function TReplyMarkup.CreateReplyKeyboard: TKeyboardButtonArray;
19051922

19061923
{ TInlineKeyboardButton }
19071924

1908-
procedure TInlineKeyboardButton.Settext(AValue: String);
1909-
begin
1910-
Strings[s_text]:=AValue;
1911-
end;
1925+
procedure TInlineKeyboardButton.Settext(AValue: String);
1926+
begin
1927+
Strings[s_text]:=AValue;
1928+
end;
1929+
1930+
procedure TInlineKeyboardButton.SetStyle(AValue: String);
1931+
begin
1932+
Strings[s_Style]:=AValue;
1933+
end;
19121934

19131935
procedure TInlineKeyboardButton.Setcallback_data(AValue: String);
19141936
begin
19151937
CheckOptnlAndSet(s_callbackdata, AValue);
19161938
end;
19171939

1918-
function TInlineKeyboardButton.Geturl: String;
1919-
begin
1920-
Result:=Strings[s_url];
1921-
end;
1940+
function TInlineKeyboardButton.Geturl: String;
1941+
begin
1942+
Result:=Strings[s_url];
1943+
end;
1944+
1945+
function TInlineKeyboardButton.GetStyle: String;
1946+
begin
1947+
Result:=Strings[s_Style];
1948+
end;
19221949

19231950
function TInlineKeyboardButton.CheckOptnlNull: Boolean;
19241951
begin

0 commit comments

Comments
 (0)