forked from ricardoelices/TGBotLazarus
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtgtypes.pas
1346 lines (1168 loc) · 42 KB
/
tgtypes.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit tgtypes;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fpJSON, jsonparser, fgl;
type
TTelegramUpdateObj = class;
TTelegramMessageObj = class;
TTelegramMessageEntityObj = class;
TTelegramChatMemberUpdated = class;
TTelegramInlineQueryObj = class;
TTelegramChosenInlineResultObj = class;
TTelegramUserObj = class;
TTelegramChatObj = class;
TCallbackQueryObj = class;
TTelegramBusinessConnectionObj = class;
TTelegramLocation = class;
TTelegramContact = class;
TArrayOfPhotoSize = class(TJSONArray);
TTelegramPhotoSize = class;
TTelegramPreCheckOutQuery = class;
TTelegramSuccessfulPayment = class;
TTelegramUpdateObjList = specialize TFPGObjectList<TTelegramMessageEntityObj>;
TTelegramPhotoSizeList = specialize TFPGObjectList<TTelegramPhotoSize>;
TTelegramVideo = class;
TTelegramVoice = class;
TTelegramAudio = class;
TTelegramDocument = class;
TUpdateType = (utMessage, utEditedMessage, utChannelPost, utEditedChannelPost, utInlineQuery,
utChosenInlineResult, utCallbackQuery, utShippingQuery, utPreCheckoutQuery, utMyChatMember, utChatMember,
utBusinessConnection, utBusinessMessage, utUnknown);
TChatType = (ctPrivate, ctGroup, ctSuperGroup, ctChannel, ctUnknown);
TChatMemberStatus = (msCreator, msAdministrator, msMember, msRestricted, msLeft, msKicked, msUnknown);
TUpdateSet = set of TUpdateType;
{ TTelegramObj }
TTelegramObj = class
private
fJSON: TJSONObject;
public
constructor Create(JSONObject: TJSONObject); virtual; // Caution! The JSONObject must be released separately
destructor Destroy; override;
class function CreateFromJSONObject(JSONObject: TJSONObject): TTelegramObj;
function AsString: String;
end;
{ TTelegramUpdateObj }
TTelegramUpdateObj = class(TTelegramObj)
private
{ Once in this object (according to the description in Telegram bot API) can be present only one
parameter then the field FUpdateParameter will be one }
FUpdateParameter: TTelegramObj;
fUpdateId: Integer;
FUpdateType: TUpdateType;
function GetBusinessConnection: TTelegramBusinessConnectionObj;
function GetBusinessMessage: TTelegramMessageObj;
function GetCallbackQuery: TCallbackQueryObj;
function GetChannelPost: TTelegramMessageObj;
function GetChatMember: TTelegramChatMemberUpdated;
function GetChosenInlineResult: TTelegramChosenInlineResultObj;
function GetEditedChannelPost: TTelegramMessageObj;
function GetEditedMessage: TTelegramMessageObj;
function GetInlineQuery: TTelegramInlineQueryObj;
function GetMessage: TTelegramMessageObj;
function GetMyChatMember: TTelegramChatMemberUpdated;
function GetPreCheckoutQuery: TTelegramPreCheckOutQuery;
function ParseUpdateParameter: TUpdateType;
public
constructor Create(JSONObject: TJSONObject); override;
function Clone: TTelegramUpdateObj;
destructor Destroy; override;
property UpdateId: Integer read fUpdateId;
property UpdateType: TUpdateType read FUpdateType;
property Message: TTelegramMessageObj read GetMessage;
property EditedMessage: TTelegramMessageObj read GetEditedMessage;
property CallbackQuery: TCallbackQueryObj read GetCallbackQuery;
property InlineQuery: TTelegramInlineQueryObj read GetInlineQuery;
property ChosenInlineResult: TTelegramChosenInlineResultObj read GetChosenInlineResult;
property ChannelPost: TTelegramMessageObj read GetChannelPost;
property EditedChannelPost: TTelegramMessageObj read GetEditedChannelPost;
property PreCheckoutQuery: TTelegramPreCheckOutQuery read GetPreCheckoutQuery;
property BusinessConnection: TTelegramBusinessConnectionObj read GetBusinessConnection;
property BusinessMessage: TTelegramMessageObj read GetBusinessMessage;
property MyChatMember: TTelegramChatMemberUpdated read GetMyChatMember;
property ChatMember: TTelegramChatMemberUpdated read GetChatMember;
end;
TContentType = (cntUnknown, cntText, cntPhoto, cntVideo, cntAudio, cntVoice, cntDocument, cntLocation, cntContact);
{ TTelegramMessageObj }
TTelegramMessageObj = class(TTelegramObj)
private
FAudio: TTelegramAudio;
FBusinessConnectionID: String;
FCaption: String;
FChat: TTelegramChatObj;
FContact: TTelegramContact;
FDate: Int64;
FDocument: TTelegramDocument;
FForwardFrom: TTelegramUserObj;
FForwardFromChat: TTelegramChatObj;
FForwardFromMessageID: LongInt;
FFrom: TTelegramUserObj;
FIsTopicMessage: Boolean;
FLocation: TTelegramLocation;
FMediaGroupID: String;
fMessageId: Integer;
fChatId: Int64;
FMessageThreadID: Integer;
FPhoto: TTelegramPhotoSizeList;
FReplyToMessage: TTelegramMessageObj;
FSuccessfulPayment: TTelegramSuccessfulPayment;
FViaBot: TTelegramUserObj;
FVideo: TTelegramVideo;
fText: string;
fEntities: TTelegramUpdateObjList;
FVoice: TTelegramVoice;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
function ContentFromMessage(out aText: String; out aMedia: String): TContentType;
property Caption: String read FCaption;
property BusinessConnectionID: String read FBusinessConnectionID;
property MessageId: Integer read fMessageId;
property From: TTelegramUserObj read FFrom;
property Date: Int64 read FDate;
property Chat: TTelegramChatObj read FChat;
property ForwardFrom: TTelegramUserObj read FForwardFrom;
property ForwardFromChat: TTelegramChatObj read FForwardFromChat;
property ForwardFromMessageID: Integer read FForwardFromMessageID;
property ChatId: Int64 read fChatId;
property ReplyToMessage: TTelegramMessageObj read FReplyToMessage;
property Text: string read fText;
property Entities: TTelegramUpdateObjList read fEntities;
property Document: TTelegramDocument read FDocument;
property Location: TTelegramLocation read FLocation;
property Photo: TTelegramPhotoSizeList read FPhoto;
property Audio: TTelegramAudio read FAudio;
property Video: TTelegramVideo read FVideo;
property Voice: TTelegramVoice read FVoice;
property Contact: TTelegramContact read FContact;
property SuccessfulPayment: TTelegramSuccessfulPayment read FSuccessfulPayment;
property ViaBot: TTelegramUserObj read FViaBot;
property MediaGroupID: String read FMediaGroupID;
property MessageThreadID: Integer read FMessageThreadID;
property IsTopicMessage: Boolean read FIsTopicMessage;
end;
{ TTelegramMessageEntityObj }
TTelegramMessageEntityObj = class(TTelegramObj)
private
fTypeEntity: string;
fOffset: Integer;
fLength: Integer;
public
constructor Create(JSONObject: TJSONObject); override;
property TypeEntity: string read fTypeEntity;
property Offset: Integer read fOffset;
property Length: Integer read fLength;
end;
{ TCallbackQueryObj }
TCallbackQueryObj = class(TTelegramObj)
private
FChatInstance: String;
FData: String;
FFrom: TTelegramUserObj;
FID: String;
FMessage: TTelegramMessageObj;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property ID: String read FID;
property From: TTelegramUserObj read FFrom;
property Message: TTelegramMessageObj read FMessage;
property ChatInstance: String read FChatInstance;
property Data: String read FData; // optional 1-64 bytes!!!
end;
{ TTelegramBusinessConnectionObj }
TTelegramBusinessConnectionObj = class(TTelegramObj)
private
FCanReply: Boolean;
FDate: Int64;
FID: Int64;
FUser: TTelegramUserObj;
FUserChatID: Int64;
FIsEnabled: Boolean;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property ID: Int64 read FID;
property User: TTelegramUserObj read FUser;
property UserChatID: Int64 read FUserChatID;
property Date: Int64 read FDate; // Unix Time
property CanReply: Boolean read FCanReply;
property IsEnabled: Boolean read FIsEnabled;
end;
{ TTelegramInlineQueryObj }
TTelegramInlineQueryObj = class(TTelegramObj)
private
FFrom: TTelegramUserObj;
FID: String;
FLocation: TTelegramLocation;
FOffset: String;
FQuery: String;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property ID: String read FID;
property From: TTelegramUserObj read FFrom;
property Location: TTelegramLocation read FLocation;
property Query: String read FQuery;
property Offset: String read FOffset;
end;
{ TTelegramChosenInlineResultObj }
TTelegramChosenInlineResultObj = class(TTelegramObj)
private
FFrom: TTelegramUserObj;
FInlineMessageID: String;
FLocation: TTelegramLocation;
FQuery: String;
FResultID: String;
procedure SetFrom(AValue: TTelegramUserObj);
procedure SetInlineMessageID(AValue: String);
procedure SetLocation(AValue: TTelegramLocation);
procedure SetQuery(AValue: String);
procedure SetResultID(AValue: String);
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property ResultID: String read FResultID;
property From: TTelegramUserObj read FFrom;
property Location: TTelegramLocation read FLocation;
property InlineMessageID: String read FInlineMessageID;
property Query: String read FQuery;
end;
{ TTelegramBaseChat }
TTelegramBaseChat=class(TTelegramObj)
private
FFirst_name: String;
FID: Int64;
FLast_name: String;
FUsername: String;
public
constructor Create(JSONObject: TJSONObject); override;
class function GetResolveUserLink(aID: Int64): String;
function GetResolveUserLink: String;
property ID: Int64 read FID;
property First_name: String read FFirst_name;
property Last_name: String read FLast_name;
property Username: String read FUsername;
end;
{ TTelegramUserObj }
TTelegramUserObj = class(TTelegramBaseChat)
private
FIs_bot: Boolean;
FLanguage_code: String;
public
function Clone: TTelegramUserObj;
constructor Create(JSONObject: TJSONObject); override;
property Is_bot: Boolean read FIs_bot;
property Language_code: String read FLanguage_code;
end;
{ TTelegramChatObj }
TTelegramChatObj = class(TTelegramBaseChat)
private
FChatType: TChatType;
FTitle: String;
class function StringToChatType(const TypeString: String): TChatType;
public
function Clone: TTelegramChatObj;
constructor Create(JSONObject: TJSONObject); override;
property ChatType: TChatType read FChatType;
property Title: String read FTitle;
end;
{ TTelegramLocation }
TTelegramLocation = class(TTelegramObj)
private
FLatitude: Double;
FLongitude: Double;
public
constructor Create(JSONObject: TJSONObject); override;
property Longitude: Double read FLongitude write FLongitude;
property Latitude: Double read FLatitude write FLatitude;
end;
{ TTelegramContact }
TTelegramContact = class(TTelegramObj)
private
Ffirst_name: String;
Flast_name: String;
Fphone_number: String;
FUser_id: Int64;
Fvcard: String;
public
constructor Create(JSONObject: TJSONObject); override;
property phone_number: String read Fphone_number write Fphone_number;
property first_name: String read Ffirst_name write Ffirst_name;
property last_name: String read Flast_name write Flast_name;
property user_id: Int64 read FUser_id write Fuser_id;
property vcard: String read Fvcard write Fvcard;
end;
{ TTelegramPhotoSize }
TTelegramPhotoSize = class(TTelegramObj)
private
FFileID: String;
FFileSize: Integer;
FHeight: Integer;
FWidth: Integer;
public
constructor Create(JSONObject: TJSONObject); override;
property FileID: String read FFileID;
property Width: Integer read FWidth;
property Height: Integer read FHeight;
property FileSize: Integer read FFileSize;
end;
{ TTelegramFile }
TTelegramFile = class(TTelegramObj)
private
FFileID: String;
FFilePath: String;
FFileSize: Integer;
public
constructor Create(JSONObject: TJSONObject); override;
class function DownloadLink(const AFilePath, AToken: String): String;
function DownloadLink(const AToken: String): String; overload;
property FileID: String read FFileID;
property FileSize: Integer read FFileSize;
property FilePath: String read FFilePath; // https://api.telegram.org/file/bot<token>/<file_path>
end;
{ TOrderInfo }
TOrderInfo = class(TTelegramObj)
private
FEmail: String;
FName: String;
FPhoneNumber: String;
public
constructor Create(JSONObject: TJSONObject); override;
property Name: String read FName;
property PhoneNumber: String read FPhoneNumber;
property Email: String read FEmail;
end;
{ TTelegramPreCheckOutQuery }
TTelegramPreCheckOutQuery = class(TTelegramObj)
private
FCurrency: String;
FFrom: TTelegramUserObj;
FID: String;
FInvoicePayload: String;
FOrderInfo: TOrderInfo;
FShippingOptionID: String;
FTotalAmount: Integer;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property ID: String read FID; // Unique query identifier
property From: TTelegramUserObj read FFrom; // User who sent the query
property Currency: String read FCurrency; // Three-letter ISO 4217 currency code
{ Total price in the smallest units of the currency (integer, not float/double).
For example, for a price of US$ 1.45 pass amount = 145. See the exp parameter in https://core.telegram.org/bots/payments/currencies.json,
it shows the number of digits past the decimal point for each currency (2 for the majority of currencies). }
property TotalAmount: Integer read FTotalAmount;
property InvoicePayload: String read FInvoicePayload; // Bot specified invoice payload
property ShippingOptionID: String read FShippingOptionID; // Optional. Identifier of the shipping option chosen by the user
property OrderInfo: TOrderInfo read FOrderInfo; // Optional. Order info provided by the user
end;
{ TTelegramSuccessfulPayment }
TTelegramSuccessfulPayment = class(TTelegramObj)
private
FCurrency: String;
FInvoicePayload: String;
FOrderInfo: TOrderInfo;
FProviderPaymentChargeID: String;
FTelegramPaymentChargeID: String;
FTotalAmount: Integer;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property Currency: String read FCurrency;
property TotalAmount: Integer read FTotalAmount;
property InvoicePayload: String read FInvoicePayload;
property OrderInfo: TOrderInfo read FOrderInfo;
property TelegramPaymentChargeID: String read FTelegramPaymentChargeID;
property ProviderPaymentChargeID: String read FProviderPaymentChargeID;
end;
{ TTelegramChatMember }
TTelegramChatMember = class(TTelegramObj)
private
FChatMemberStatus: TChatMemberStatus;
Fis_member: Boolean;
FUser: TTelegramUserObj;
class function StringToStatus(const StatusString: String): TChatMemberStatus;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property User: TTelegramUserObj read FUser;
property is_member: Boolean read Fis_member;
property StatusType: TChatMemberStatus read FChatMemberStatus;
end;
{ TTelegramChatMemberUpdated }
TTelegramChatMemberUpdated = class(TTelegramObj)
private
fChat: TTelegramChatObj;
fDate: Integer;
fNewChatMember: TTelegramChatMember;
fOldChatMember: TTelegramChatMember;
fFrom: TTelegramUserObj;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property Chat: TTelegramChatObj read fChat;
property From: TTelegramUserObj read fFrom;
property Date: Integer read fDate;
property OldChatMember: TTelegramChatMember read fOldChatMember;
property NewChatMember: TTelegramChatMember read fNewChatMember;
end;
{ TTelegramWebhookInfo }
TTelegramWebhookInfo = class(TTelegramObj)
private
FAllowedUpdates: TUpdateSet;
FHasCustomCertificate: Boolean;
FLastErrorDate: Int64;
FLastErrorMessage: String;
FMaxConnections: Integer;
FPendingUpdateCount: Integer;
FUrl: String;
function GetLastErrorDateAsDateTime: TDateTime;
public
constructor Create(JSONObject: TJSONObject); override;
property Url: String read FUrl;
property AllowedUpdates: TUpdateSet read FAllowedUpdates;
property HasCustomCertificate: Boolean read FHasCustomCertificate;
property LastErrorDate: Int64 read FLastErrorDate; // UnixTime
property LastErrorDateAsDateTime: TDateTime read GetLastErrorDateAsDateTime;
property LastErrorMessage: String read FLastErrorMessage;
property MaxConnections: Integer read FMaxConnections;
property PendingUpdateCount: Integer read FPendingUpdateCount;
end;
{ TTelegramVideo }
TTelegramVideo = class(TTelegramObj)
private
FDuration: Integer;
FFileID: String;
FFileName: String;
FFileSize: Integer;
FHeight: Integer;
FMimeType: String;
FThumb: TTelegramPhotoSize;
FWidth: Integer;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property FileID: String read FFileID;
property Width: Integer read FWidth;
property Height: Integer read FHeight;
property Duration: Integer read FDuration;
property Thumb: TTelegramPhotoSize read FThumb;
property MimeType: String read FMimeType;
property FileSize: Integer read FFileSize;
property FileName: String read FFileName;
end;
{ TTelegramDocument }
TTelegramDocument = class(TTelegramObj)
private
FFileID: String;
FFileName: String;
FFileSize: Integer;
FMimeType: String;
FThumb: TTelegramPhotoSize;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property FileID: String read FFileID;
property FileName: String read FFileName;
property MimeType: String read FMimeType;
property FileSize: Integer read FFileSize;
property Thumb: TTelegramPhotoSize read FThumb;
end;
{ TTelegramAudio }
TTelegramAudio = class(TTelegramObj)
private
FDuration: Integer;
FFileID: String;
FFileSize: Integer;
FMimeType: String;
FPerformer: String;
FThumb: TTelegramPhotoSize;
FTitle: String;
public
constructor Create(JSONObject: TJSONObject); override;
destructor Destroy; override;
property FileID: String read FFileID;
property Duration: Integer read FDuration;
property Performer: String read FPerformer;
property Title: String read FTitle;
property MimeType: String read FMimeType;
property FileSize: Integer read FFileSize;
property Thumb: TTelegramPhotoSize read FThumb;
end;
{ TTelegramVoice }
TTelegramVoice = class(TTelegramObj)
private
FDuration: Integer;
FFileID: String;
FFileSize: Integer;
FMimeType: String;
public
constructor Create(JSONObject: TJSONObject); override;
property FileID: String read FFileID;
property Duration: Integer read FDuration;
property MimeType: String read FMimeType;
property FileSize: Integer read FFileSize;
end;
TTelegramObjClass = class of TTelegramObj;
const
TELEGRAM_REQUEST_GETUPDATES = 'getUpdates';
UpdateTypeAliases: array[TUpdateType] of String = ('message', 'edited_message', 'channel_post',
'edited_channel_post', 'inline_query', 'chosen_inline_result', 'callback_query',
'shipping_query', 'pre_checkout_query', 'my_chat_member', 'chat_member', 'business_connection',
'business_message', '');
UpdateTypeClasses: array[TUpdateType] of TTelegramObjClass = (TTelegramMessageObj, TTelegramMessageObj,
TTelegramMessageObj, TTelegramMessageObj, TTelegramInlineQueryObj, TTelegramChosenInlineResultObj,
TCallbackQueryObj, TTelegramObj, TTelegramPreCheckOutQuery, TTelegramChatMemberUpdated,
TTelegramChatMemberUpdated, TTelegramBusinessConnectionObj, TTelegramMessageObj, TTelegramObj);
_nullThrd = 0;
function AllowedUpdatesToJSON(const AllowedUpdates: TUpdateSet): TJSONArray;
const
utAllUpdates = [Low(TUpdateType)..Pred(High(TUpdateType))];
utDefaultUpdates = utAllUpdates-[utChatMember];
implementation
uses
strutils, dateutils
;
const
API_URL_FILE='https://api.telegram.org/file/bot';
s_FileSize = 'file_size';
s_FileName = 'file_name';
s_IsTpcMsg = 'is_topic_message';
s_MsgThrdID ='message_thread_id';
s_BsnsCnctnID='business_connection_id';
function AllowedUpdatesToJSON(const AllowedUpdates: TUpdateSet): TJSONArray;
var
u: TUpdateType;
begin
Result:=TJSONArray.Create;
for u in AllowedUpdates do
Result.Add(UpdateTypeAliases[u]);
end;
function JSONToUpdateSet(const aUpdateSet: TJSONArray): TUpdateSet;
var
aEnum: TJSONEnum;
i: Integer;
begin
Result:=[];
for aEnum in aUpdateSet do
begin
i:=AnsiIndexStr(aEnum.Value.AsString, UpdateTypeAliases);
if i<>-1 then
Include(Result, TUpdateType(i));
end;
if Result=[] then
Result:=utAllUpdates;
end;
{ TTelegramBaseChat }
constructor TTelegramBaseChat.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FID := fJSON.Int64s['id'];
FFirst_name:=fJSON.Get('first_name', EmptyStr);
FLast_name:=fJSON.Get('last_name', EmptyStr);
FUsername:=fJSON.Get('username', EmptyStr);
end;
class function TTelegramBaseChat.GetResolveUserLink(aID: Int64): String;
begin
Result:=Format('tg://user?id=%d', [aID]);
end;
function TTelegramBaseChat.GetResolveUserLink: String;
begin
Result:=GetResolveUserLink(FID);
end;
{ TTelegramContact }
constructor TTelegramContact.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
Fphone_number := fJSON.Strings['phone_number'];
Ffirst_name := fJSON.Strings['first_name'];
Flast_name := fJSON.Get('last_name', EmptyStr);
FUser_id := fJSON.Get('user_id', 0);
Fvcard := fJSON.Get('vcard', EmptyStr);
end;
{ TTelegramWebhookInfo }
function TTelegramWebhookInfo.GetLastErrorDateAsDateTime: TDateTime;
begin
Result:=UnixToDateTime(FLastErrorDate);
end;
constructor TTelegramWebhookInfo.Create(JSONObject: TJSONObject);
var
aJSONArray: TJSONArray;
begin
inherited Create(JSONObject);
FUrl:=fJSON.Strings['url'];
FHasCustomCertificate:=fJSON.Booleans['has_custom_certificate'];
FPendingUpdateCount:=fJSON.Integers['pending_update_count'];
FLastErrorDate:=fJSON.Get('last_error_date', 0);
FLastErrorMessage:=fJSON.Get('last_error_message', EmptyStr);
FMaxConnections:=fJSON.Get('max_connections', 0);
if fJSON.Find('allowed_updates', aJSONArray) then
FAllowedUpdates:=JSONToUpdateSet(aJSONArray)
else
FAllowedUpdates:=[];
end;
{ TTelegramDocument }
constructor TTelegramDocument.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FFileID := fJSON.Strings['file_id'];
FThumb:=TTelegramPhotoSize.CreateFromJSONObject(fJSON.Find('thumb', jtObject) as TJSONObject) as TTelegramPhotoSize;
FMimeType:=fJSON.Get('mime_type', EmptyStr);
FFileSize := fJSON.Get(s_FileSize, 0);
FFileName := fJSON.Get(s_FileName, EmptyStr);
end;
destructor TTelegramDocument.Destroy;
begin
FThumb.Free;
inherited Destroy;
end;
{ TTelegramAudio }
constructor TTelegramAudio.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FFileID := fJSON.Strings['file_id'];
FDuration:=fJSON.Integers['duration'];
FPerformer:=fJSON.Get('performer', EmptyStr);
FTitle:=fJSON.Get('title', EmptyStr);
FThumb:=TTelegramPhotoSize.CreateFromJSONObject(fJSON.Find('thumb', jtObject) as TJSONObject) as TTelegramPhotoSize;
FMimeType:=fJSON.Get('mime_type', EmptyStr);
FFileSize := fJSON.Get(s_FileSize, 0);
end;
destructor TTelegramAudio.Destroy;
begin
FThumb.Free;
inherited Destroy;
end;
{ TTelegramVoice }
constructor TTelegramVoice.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FFileID := fJSON.Strings['file_id'];
FDuration:=fJSON.Integers['duration'];
FMimeType:=fJSON.Get('mime_type', EmptyStr);
FFileSize := fJSON.Get(s_FileSize, 0);
end;
{ TTelegramVideo }
constructor TTelegramVideo.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FFileID := fJSON.Strings['file_id'];
FWidth:=fJSON.Integers['width'];
FHeight:=fJSON.Integers['height'];
FDuration:=fJSON.Integers['duration'];
FThumb:=TTelegramPhotoSize.CreateFromJSONObject(fJSON.Find('thumb', jtObject) as TJSONObject) as TTelegramPhotoSize;
FMimeType:=fJSON.Get('mime_type', EmptyStr);
FFileSize := fJSON.Get(s_FileSize, 0);
FFileName := fJSON.Get(s_FileName, EmptyStr);
end;
destructor TTelegramVideo.Destroy;
begin
FThumb.Free;
inherited Destroy;
end;
{ TTelegramChatMember }
class function TTelegramChatMember.StringToStatus(const StatusString: String
): TChatMemberStatus;
begin
if StatusString='creator' then
Exit(msCreator);
if StatusString='administrator' then
Exit(msAdministrator);
if StatusString='member' then
Exit(msMember);
if StatusString='restricted' then
Exit(msRestricted);
if StatusString='left' then
Exit(msLeft);
if StatusString='kicked' then
Exit(msKicked);
Result:=msUnknown;
end;
constructor TTelegramChatMember.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FUser:=TTelegramUserObj.CreateFromJSONObject(fJSON.Find('user', jtObject) as TJSONObject) as TTelegramUserObj;
Fis_member:=fJSON.Get('is_member', False);
FChatMemberStatus:=StringToStatus(fJSON.Strings['status']);
end;
destructor TTelegramChatMember.Destroy;
begin
FUser.Free;
inherited Destroy;
end;
{ TTelegramChatMemberUpdated }
constructor TTelegramChatMemberUpdated.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
fChat:=TTelegramChatObj.CreateFromJSONObject(fJSON.Find('chat', jtObject) as TJSONObject) as TTelegramChatObj;
fFrom:=TTelegramUserObj.CreateFromJSONObject(fJSON.Find('from', jtObject) as TJSONObject) as TTelegramUserObj;
fDate:=fJSON.Integers['date'];
fOldChatMember:=TTelegramChatMember.CreateFromJSONObject(fJSON.Find('old_chat_member', jtObject) as TJSONObject)
as TTelegramChatMember;
fNewChatMember:=TTelegramChatMember.CreateFromJSONObject(fJSON.Find('new_chat_member', jtObject) as TJSONObject)
as TTelegramChatMember;
end;
destructor TTelegramChatMemberUpdated.Destroy;
begin
fChat.Free;
fFrom.Free;
fOldChatMember.Free;
fNewChatMember.Free;
inherited Destroy;
end;
{ TTelegramSuccessfulPayment }
constructor TTelegramSuccessfulPayment.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FCurrency:=fJSON.Strings['currency'];
FTotalAmount:=fJSON.Integers['total_amount'];
FInvoicePayload:=fJSON.Strings['invoice_payload'];
FOrderInfo:=TOrderInfo.CreateFromJSONObject(
fJSON.Find('order_info', jtObject) as TJSONObject) as TOrderInfo;
FTelegramPaymentChargeID:=fJSON.Strings['telegram_payment_charge_id'];
FProviderPaymentChargeID:=fJSON.Strings['provider_payment_charge_id'];
end;
destructor TTelegramSuccessfulPayment.Destroy;
begin
FreeAndNil(FOrderInfo);
inherited Destroy;
end;
{ TOrderInfo }
constructor TOrderInfo.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FName := fJSON.Get('name', EmptyStr);
FPhoneNumber := fJSON.Get('phone_number', EmptyStr);
FEmail := fJSON.Get('email', EmptyStr);
end;
{ TTelegramPreCheckOutQuery }
constructor TTelegramPreCheckOutQuery.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FID:=fJSON.Strings['id'];
FFrom:=TTelegramUserObj.CreateFromJSONObject(fJSON.Find('from', jtObject) as TJSONObject) as TTelegramUserObj;;
FCurrency:=fJSON.Strings['currency'];
FTotalAmount:=fJSON.Integers['total_amount'];
FInvoicePayload:=fJSON.Strings['invoice_payload'];
FShippingOptionID:=fJSON.Get('shipping_option_id', EmptyStr);
FOrderInfo:=TOrderInfo.CreateFromJSONObject(
fJSON.Find('order_info', jtObject) as TJSONObject) as TOrderInfo;
end;
destructor TTelegramPreCheckOutQuery.Destroy;
begin
FreeAndNil(FOrderInfo);
FreeAndNil(FFrom);
inherited Destroy;
end;
{ TTelegramFile }
constructor TTelegramFile.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FFileID := fJSON.Strings['file_id'];
FFileSize := fJSON.Get(s_FileSize, 0);
FFilePath := fJSON.Get('file_path', '');
end;
class function TTelegramFile.DownloadLink(const AFilePath, AToken: String
): String;
begin
Result:=API_URL_FILE+AToken+'/'+AFilePath;
end;
function TTelegramFile.DownloadLink(const AToken: String): String;
begin
Result:=DownloadLink(FFilePath, AToken);
end;
{ TTelegramPhotoSize }
constructor TTelegramPhotoSize.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FFileID := fJSON.Strings['file_id'];
FWidth := fJSON.Integers['width'];
FHeight := fJSON.Integers['height'];
FFileSize:=fJSON.Get(s_FileSize, 0);
end;
{ TTelegramChatObj }
class function TTelegramChatObj.StringToChatType(const TypeString: String
): TChatType;
begin
if TypeString='private' then
Exit(ctPrivate);
if TypeString='group' then
Exit(ctGroup);
if TypeString='supergroup' then
Exit(ctSuperGroup);
if TypeString='channel' then
Exit(ctChannel);
Result:=ctUnknown;
end;
function TTelegramChatObj.Clone: TTelegramChatObj;
begin
Result:=TTelegramChatObj.Create(fJSON);
end;
constructor TTelegramChatObj.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FChatType:=StringToChatType(fJSON.Strings['type']);
FTitle:=fJSON.Get('title', '');
end;
{ TTelegramChosenInlineResultObj }
procedure TTelegramChosenInlineResultObj.SetFrom(AValue: TTelegramUserObj);
begin
if FFrom=AValue then Exit;
FFrom:=AValue;
end;
procedure TTelegramChosenInlineResultObj.SetInlineMessageID(AValue: String);
begin
if FInlineMessageID=AValue then Exit;
FInlineMessageID:=AValue;
end;
procedure TTelegramChosenInlineResultObj.SetLocation(AValue: TTelegramLocation);
begin
if FLocation=AValue then Exit;
FLocation:=AValue;
end;
procedure TTelegramChosenInlineResultObj.SetQuery(AValue: String);
begin
if FQuery=AValue then Exit;
FQuery:=AValue;
end;
procedure TTelegramChosenInlineResultObj.SetResultID(AValue: String);
begin
if FResultID=AValue then Exit;
FResultID:=AValue;
end;
constructor TTelegramChosenInlineResultObj.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FResultID := fJSON.Strings['result_id'];
FFrom:=TTelegramUserObj.CreateFromJSONObject(fJSON.Find('from', jtObject) as TJSONObject) as TTelegramUserObj;
FLocation:=TTelegramLocation.CreateFromJSONObject(fJSON.Find('location', jtObject) as TJSONObject) as TTelegramLocation;
FInlineMessageID:=fJSON.Get('inline_message_id', '');
FQuery:=fJSON.Strings['query'];
end;
destructor TTelegramChosenInlineResultObj.Destroy;
begin
FreeAndNil(FLocation);
FreeAndNil(FFrom);
inherited Destroy;
end;
{ TTelegramLocation }
constructor TTelegramLocation.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FLongitude := fJSON.Floats['longitude'];
FLatitude := fJSON.Floats['latitude'];
end;
{ TCallbackQueryObj }
constructor TCallbackQueryObj.Create(JSONObject: TJSONObject);
begin
inherited Create(JSONObject);
FID := fJSON.Strings['id'];
FFrom:=TTelegramUserObj.CreateFromJSONObject(fJSON.Find('from', jtObject) as TJSONObject) as TTelegramUserObj;
FMessage := TTelegramMessageObj.CreateFromJSONObject(
fJSON.Find('message', jtObject) as TJSONObject) as TTelegramMessageObj;
FChatInstance:= fJSON.Strings['chat_instance'];
FData:=fJSON.Get('data', '');
end;
destructor TCallbackQueryObj.Destroy;
begin
FMessage.Free;
FFrom.Free;
inherited Destroy;
end;