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.Types.Helpers.pas
201 lines (179 loc) · 7.14 KB
/
TelegramBotApi.Types.Helpers.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
{***************************************************************************}
{ }
{ 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.Types.Helpers;
interface
uses
TelegramBotApi.Types.Enums;
type
TAllowedUpdatesHelper = record helper for TAllowedUpdates
public
function ToString: string;
end;
TtgChatActionHelper = record helper for TtgChatAction
public
function ToString: string;
end;
TtgMessageEntityTypeHelper = record helper for TtgMessageEntityType
public
function ToString: string;
class function FromString(const AValue: string): TtgMessageEntityType; static;
end;
TtgParseModeHelper = record helper for TtgParseMode
private const
TG_PARSE_MODES: array of string = ['', 'Markdown', 'MarkdownV2', 'HTML'];
public
function ToString: string;
class function FromString(const AValue: string): TtgParseMode; static;
end;
implementation
uses
System.Generics.Collections,
System.SysUtils,
System.Rtti;
{ TAllowedUpdatesHelper }
function TAllowedUpdatesHelper.ToString: string;
var
LAllowed: TList<string>;
begin
LAllowed := TList<string>.Create;
try
if TAllowedUpdate.Message in Self then
LAllowed.Add('"message"');
if TAllowedUpdate.Edited_message in Self then
LAllowed.Add('"edited_message"');
if TAllowedUpdate.Channel_post in Self then
LAllowed.Add('"channel_post"');
if TAllowedUpdate.Edited_channel_post in Self then
LAllowed.Add('"edited_channel_post"');
if TAllowedUpdate.Inline_query in Self then
LAllowed.Add('"inline_query"');
if TAllowedUpdate.Chosen_inline_result in Self then
LAllowed.Add('"chosen_inline_result"');
if TAllowedUpdate.Callback_query in Self then
LAllowed.Add('"callback_query"');
Result := '[' + Result.Join(',', LAllowed.ToArray) + ']';
finally
LAllowed.Free;
end;
end;
{ TtgMessageEntityTypeHelper }
class function TtgMessageEntityTypeHelper.FromString(const AValue: string): TtgMessageEntityType;
var
LStore: TDictionary<string, TtgMessageEntityType>;
begin
LStore := TDictionary<string, TtgMessageEntityType>.Create;
try
LStore.Add('mention', TtgMessageEntityType.Mention);
LStore.Add('hashtag', TtgMessageEntityType.Hashtag);
LStore.Add('bot_command', TtgMessageEntityType.BotCommand);
LStore.Add('url', TtgMessageEntityType.Url);
LStore.Add('email', TtgMessageEntityType.Email);
LStore.Add('bold', TtgMessageEntityType.Bold);
LStore.Add('italic', TtgMessageEntityType.Italic);
LStore.Add('code', TtgMessageEntityType.Code);
LStore.Add('pre', TtgMessageEntityType.Pre);
LStore.Add('text_link', TtgMessageEntityType.TextLink);
LStore.Add('text_mention', TtgMessageEntityType.TextMention);
LStore.Add('phone_number', TtgMessageEntityType.PhoneNumber);
LStore.Add('cashtag', TtgMessageEntityType.Cashtag);
LStore.Add('underline', TtgMessageEntityType.Underline);
LStore.Add('strikethrough', TtgMessageEntityType.Strikethrough);
LStore.Add('spoiler', TtgMessageEntityType.Spoiler);
Result := LStore[AValue];
finally
LStore.Free;
end;
end;
function TtgMessageEntityTypeHelper.ToString: string;
var
LStore: TDictionary<TtgMessageEntityType, string>;
begin
LStore := TDictionary<TtgMessageEntityType, string>.Create;
try
LStore.Add(TtgMessageEntityType.Mention, 'mention');
LStore.Add(TtgMessageEntityType.Hashtag, 'hashtag');
LStore.Add(TtgMessageEntityType.BotCommand, 'bot_command');
LStore.Add(TtgMessageEntityType.Url, 'url');
LStore.Add(TtgMessageEntityType.Email, 'email');
LStore.Add(TtgMessageEntityType.Bold, 'bold');
LStore.Add(TtgMessageEntityType.Italic, 'italic');
LStore.Add(TtgMessageEntityType.Code, 'code');
LStore.Add(TtgMessageEntityType.Pre, 'pre');
LStore.Add(TtgMessageEntityType.TextLink, 'text_link');
LStore.Add(TtgMessageEntityType.TextMention, 'text_mention');
LStore.Add(TtgMessageEntityType.PhoneNumber, 'phone_number');
LStore.Add(TtgMessageEntityType.Cashtag, 'cashtag');
LStore.Add(TtgMessageEntityType.Underline, 'underline');
LStore.Add(TtgMessageEntityType.Strikethrough, 'strikethrough');
Result := LStore[Self];
finally
LStore.Free;
end;
end;
{ TtgParseModeHelper }
class function TtgParseModeHelper.FromString(const AValue: string): TtgParseMode;
var
I: Integer;
begin
Result := TtgParseMode.Default;
for I := Low(TG_PARSE_MODES) to High(TG_PARSE_MODES) do
if SameText(TG_PARSE_MODES[I], AValue) then
Exit(TtgParseMode(I));
end;
function TtgParseModeHelper.ToString: string;
begin
Result := TG_PARSE_MODES[Ord(Self)];
end;
{ TtgChatActionHelper }
function TtgChatActionHelper.ToString: string;
begin
case Self of
TtgChatAction.Typing:
Result := 'typing';
TtgChatAction.UploadPhoto:
Result := 'upload_photo';
TtgChatAction.RecordVideo:
Result := 'record_video';
TtgChatAction.UploadVideo:
Result := 'upload_video';
TtgChatAction.RecordVoice:
Result := 'record_voice';
TtgChatAction.UploadVoice:
Result := 'upload_voice';
TtgChatAction.UploadDocument:
Result := 'upload_document';
TtgChatAction.FindLocation:
Result := 'find_location';
TtgChatAction.RecordVideoNote:
Result := 'record_video_note';
TtgChatAction.UploadVideoNote:
Result := 'upload_video_note';
TtgChatAction.ChooseSticker:
Result := 'choose_sticker';
else
raise Exception.Create('Unknown function TtgChatActionHelper.ToString: string;');
end
end;
end.