This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
TelegramBotApi.UpdateParser.pas
117 lines (98 loc) · 4.48 KB
/
TelegramBotApi.UpdateParser.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
{***************************************************************************}
{ }
{ TelegaPi }
{ }
{ Copyright (C) 2021 Maxim Sysoev }
{ }
{ https://t.me/CloudAPI }
{ }
{ }
{***************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{***************************************************************************}
unit TelegramBotApi.UpdateParser;
interface
uses
TelegramBotApi.Types;
type
TTgBotUpdateParser = class(TObject)
private
protected
procedure EventParser(AUpdates: TArray<TtgUpdate>); virtual;
procedure TypeUpdate(AUpdate: TtgUpdate); virtual;
// Ñîáûòèÿ
procedure DoOnUpdates(AUpdates: TArray<TtgUpdate>); virtual; abstract;
procedure DoOnUpdate(AUpdate: TtgUpdate); virtual; abstract;
procedure DoOnMessage(AMessage: TTgMessage); virtual; abstract;
procedure DoOnInlineQuery(AInlineQuery: TtgInlineQuery); virtual; abstract;
procedure DoOnChosenInlineResult(AChosenInlineResult: TtgChosenInlineResult); virtual; abstract;
procedure DoOnCallbackQuery(ACallbackQuery: TtgCallbackQuery); virtual; abstract;
procedure DoOnEditedMessage(AEditedMessage: TTgMessage); virtual; abstract;
procedure DoOnChannelPost(AChannelPost: TTgMessage); virtual; abstract;
procedure DoOnEditedChannelPost(AEditedChannelPost: TTgMessage); virtual; abstract;
procedure DoOnShippingQuery(AShippingQuery: TtgShippingQuery); virtual; abstract;
procedure DoOnPreCheckoutQuery(APreCheckoutQuery: TtgPreCheckoutQuery); virtual; abstract;
public
procedure ParseResponse(const JSON: string);
end;
implementation
uses
System.Generics.Collections,
System.SysUtils,
TelegramBotApi.Client,
TelegramBotApi.Types.Enums;
{ TTgBotUpdateParser }
procedure TTgBotUpdateParser.EventParser(AUpdates: TArray<TtgUpdate>);
var
LUpdate: TtgUpdate;
begin
DoOnUpdates(AUpdates);
for LUpdate in AUpdates do
begin
DoOnUpdate(LUpdate);
TypeUpdate(LUpdate);
end;
end;
procedure TTgBotUpdateParser.ParseResponse(const JSON: string);
var
LUpdates: ItgResponse<TArray<TtgUpdate>>;
begin
LUpdates := TTelegramBotApi.GetUpdates(JSON);
EventParser(LUpdates.Result);
end;
procedure TTgBotUpdateParser.TypeUpdate(AUpdate: TtgUpdate);
begin
case AUpdate.&Type of
TtgUpdateType.MessageUpdate:
DoOnMessage(AUpdate.Message);
TtgUpdateType.InlineQueryUpdate:
DoOnInlineQuery(AUpdate.InlineQuery);
TtgUpdateType.ChosenInlineResultUpdate:
DoOnChosenInlineResult(AUpdate.ChosenInlineResult);
TtgUpdateType.CallbackQueryUpdate:
DoOnCallbackQuery(AUpdate.CallbackQuery);
TtgUpdateType.EditedMessage:
DoOnEditedMessage(AUpdate.EditedMessage);
TtgUpdateType.ChannelPost:
DoOnChannelPost(AUpdate.ChannelPost);
TtgUpdateType.EditedChannelPost:
DoOnEditedChannelPost(AUpdate.EditedChannelPost);
TtgUpdateType.ShippingQueryUpdate:
DoOnShippingQuery(AUpdate.ShippingQuery);
TtgUpdateType.PreCheckoutQueryUpdate:
DoOnPreCheckoutQuery(AUpdate.PreCheckoutQuery);
end;
end;
end.