Skip to content

Commit 4663fe9

Browse files
committed
Add unit test for message external_reply parsing
1 parent 607f64c commit 4663fe9

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

test/testtelegram.pas

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ TTestReceiveLongPolling=class(TTestTelegramClass)
104104
TTestMessageQuote=class(TTestCase)
105105
published
106106
procedure ParseMessageQuote;
107+
procedure ParseMessageExternalReply;
107108
end;
108109

109110
{ TTestPayments }
@@ -423,6 +424,49 @@ procedure TTestMessageQuote.ParseMessageQuote;
423424
end;
424425
end;
425426

427+
procedure TTestMessageQuote.ParseMessageExternalReply;
428+
const
429+
MessageWithExternalReplyJSON =
430+
'{' +
431+
'"update_id":1003,' +
432+
'"message":{' +
433+
'"message_id":56,' +
434+
'"date":1700000300,' +
435+
'"chat":{"id":77,"type":"private","first_name":"Test"},' +
436+
'"from":{"id":88,"is_bot":false,"first_name":"Alice"},' +
437+
'"text":"Reply to external message",' +
438+
'"external_reply":{' +
439+
'"origin":{' +
440+
'"type":"user",' +
441+
'"date":1700000290,' +
442+
'"sender_user":{"id":99,"is_bot":false,"first_name":"Bob"}' +
443+
'}' +
444+
'}' +
445+
'}' +
446+
'}';
447+
var
448+
aUpdateObj: TTelegramUpdateObj;
449+
aMessage: TTelegramMessageObj;
450+
aJSONData: TJSONData;
451+
begin
452+
aJSONData:=GetJSON(MessageWithExternalReplyJSON);
453+
try
454+
aUpdateObj:=TTelegramUpdateObj.Create(aJSONData as TJSONObject);
455+
try
456+
AssertEquals(Ord(utMessage), Ord(aUpdateObj.UpdateType));
457+
aMessage:=aUpdateObj.Message;
458+
AssertNotNull('Message should be assigned', aMessage);
459+
AssertNotNull('ExternalReply should be assigned', aMessage.ExternalReply);
460+
AssertTrue('ExternalReply JSON should contain origin type',
461+
Pos('"type":"user"', aMessage.ExternalReply.AsString)>0);
462+
finally
463+
aUpdateObj.Free;
464+
end;
465+
finally
466+
aJSONData.Free;
467+
end;
468+
end;
469+
426470
{ TTestSender }
427471

428472
procedure TTestSender.SetUp;

tgtypes.pas

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ interface
1111
TTelegramUpdateObj = class;
1212
TTelegramMessageObj = class;
1313
TTelegramTextQuote = class;
14-
TTelegramMessageEntityObj = class;
14+
TTelegramExternalReplyInfo = class;
15+
TTelegramMessageEntityObj = class;
1516
TTelegramChatMemberUpdated = class;
1617
TTelegramInlineQueryObj = class;
1718
TTelegramChosenInlineResultObj = class;
@@ -113,7 +114,8 @@ TTelegramMessageObj = class(TTelegramObj)
113114
FChat: TTelegramChatObj;
114115
FContact: TTelegramContact;
115116
FDate: Int64;
116-
FDocument: TTelegramDocument;
117+
FDocument: TTelegramDocument;
118+
FExternalReply: TTelegramExternalReplyInfo;
117119
FForwardFrom: TTelegramUserObj;
118120
FForwardFromChat: TTelegramChatObj;
119121
FForwardFromMessageID: LongInt;
@@ -151,7 +153,8 @@ TTelegramMessageObj = class(TTelegramObj)
151153
property Quote: TTelegramTextQuote read FQuote;
152154
property Text: string read fText;
153155
property Entities: TTelegramUpdateObjList read fEntities;
154-
property Document: TTelegramDocument read FDocument;
156+
property Document: TTelegramDocument read FDocument;
157+
property ExternalReply: TTelegramExternalReplyInfo read FExternalReply;
155158
property Location: TTelegramLocation read FLocation;
156159
property Photo: TTelegramPhotoSizeList read FPhoto;
157160
property Audio: TTelegramAudio read FAudio;
@@ -165,6 +168,13 @@ TTelegramMessageObj = class(TTelegramObj)
165168
property IsTopicMessage: Boolean read FIsTopicMessage;
166169
end;
167170

171+
{ TTelegramExternalReplyInfo }
172+
173+
TTelegramExternalReplyInfo = class(TTelegramObj)
174+
public
175+
constructor Create(JSONObject: TJSONObject); override;
176+
end;
177+
168178
{ TTelegramTextQuote }
169179

170180
TTelegramTextQuote = class(TTelegramObj)
@@ -1477,8 +1487,10 @@ constructor TTelegramMessageObj.Create(JSONObject: TJSONObject);
14771487
FChat:=TTelegramChatObj.CreateFromJSONObject(fJSON.Find('chat', jtObject) as TJSONObject) as TTelegramChatObj;
14781488
fChatId := fJSON.Objects['chat'].Int64s['id']; // deprecated?
14791489
FFrom:=TTelegramUserObj.CreateFromJSONObject(fJSON.Find('from', jtObject) as TJSONObject) as TTelegramUserObj;
1480-
FDocument := TTelegramDocument.CreateFromJSONObject(fJSON.Find('document', jtObject) as TJSONObject) as TTelegramDocument;
1481-
FVideo := TTelegramVideo.CreateFromJSONObject(fJSON.Find('video', jtObject) as TJSONObject) as TTelegramVideo;
1490+
FDocument := TTelegramDocument.CreateFromJSONObject(fJSON.Find('document', jtObject) as TJSONObject) as TTelegramDocument;
1491+
FExternalReply := TTelegramExternalReplyInfo.CreateFromJSONObject(
1492+
fJSON.Find('external_reply', jtObject) as TJSONObject) as TTelegramExternalReplyInfo;
1493+
FVideo := TTelegramVideo.CreateFromJSONObject(fJSON.Find('video', jtObject) as TJSONObject) as TTelegramVideo;
14821494
FAudio := TTelegramAudio.CreateFromJSONObject(fJSON.Find('audio', jtObject) as TJSONObject) as TTelegramAudio;
14831495
FVoice := TTelegramVoice.CreateFromJSONObject(fJSON.Find('voice', jtObject) as TJSONObject) as TTelegramVoice;
14841496
FViaBot := TTelegramUserObj.CreateFromJSONObject(fJSON.Find('via_bot', jtObject) as TJSONObject) as TTelegramUserObj;
@@ -1531,14 +1543,22 @@ destructor TTelegramMessageObj.Destroy;
15311543
FReplyToMessage.Free;
15321544
FQuote.Free;
15331545
FPhoto.Free;
1534-
FDocument.Free;
1535-
FAudio.Free;
1546+
FDocument.Free;
1547+
FExternalReply.Free;
1548+
FAudio.Free;
15361549
FVideo.Free;
15371550
FVoice.Free;
15381551
fEntities.Free;
15391552
inherited Destroy;
15401553
end;
15411554

1555+
{ TTelegramExternalReplyInfo }
1556+
1557+
constructor TTelegramExternalReplyInfo.Create(JSONObject: TJSONObject);
1558+
begin
1559+
inherited Create(JSONObject);
1560+
end;
1561+
15421562
{ TTelegramTextQuote }
15431563

15441564
constructor TTelegramTextQuote.Create(JSONObject: TJSONObject);

0 commit comments

Comments
 (0)