-
Notifications
You must be signed in to change notification settings - Fork 7
/
VK.Bot.pas
437 lines (382 loc) · 11.9 KB
/
VK.Bot.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
unit VK.Bot;
interface
uses
System.SysUtils, VK.API, VK.Components, VK.GroupEvents, VK.Entity.Message,
VK.Types, VK.Entity.ClientInfo, System.Classes, System.Generics.Collections;
type
TVkBot = class;
TVkBotCallback = reference to procedure(Bot: TVkBot);
TVkBotMessage = reference to procedure(Bot: TVkBot; GroupId: Integer; Message: TVkMessage; ClientInfo: TVkClientInfo);
TVkBotMessageListener = reference to function(Bot: TVkBot; GroupId: Integer; Message: TVkMessage; ClientInfo: TVkClientInfo): Boolean;
TVkBotMessageEdit = reference to procedure(Bot: TVkBot; GroupId: Integer; Message: TVkMessage);
TVkBotError = reference to procedure(Bot: TVkBot; E: Exception; Code: Integer; Text: string);
TVkBotJoin = reference to procedure(Bot: TVkBot; GroupId, UserId: Integer; JoinType: TVkGroupJoinType; const EventId: string);
TVkPeerTypes = set of TVkPeerType;
TMessageListener = record
PeerTypes: TVkPeerTypes;
Method: TVkBotMessageListener;
end;
TMessageListeners = TList<TMessageListener>;
TEventListiner = record
Method: Pointer;
TypeInfo: Pointer;
class function Create(Method, TypeInfo: Pointer): TEventListiner; static;
end;
TEventListeners = class(TList<TEventListiner>)
function ForEach<T>(Proc: TFunc<TEventListiner, Boolean>): Boolean;
end;
TVkBot = class
class var
Instance: TVkBot;
private
FVK: TCustomVK;
FLongPoll: TVkGroupEvents;
FOnInit: TVkBotCallback;
FOnDestroy: TVkBotCallback;
FOnError: TVkBotError;
function GetGroupId: Integer;
function GetToken: string;
procedure SetOnInit(const Value: TVkBotCallback);
procedure SetOnDestroy(const Value: TVkBotCallback);
procedure FOnVkError(Sender: TObject; E: Exception; Code: Integer; Text: string);
procedure SetOnError(const Value: TVkBotError);
procedure SetGroupId(const Value: Integer);
procedure SetToken(const Value: string);
public
class function GetInstance<T: TVkBot>: T; overload;
constructor Create; virtual;
destructor Destroy; override;
function Init: Boolean; virtual;
function Run: Boolean;
procedure Stop;
property API: TCustomVK read FVK;
property GroupId: Integer read GetGroupId write SetGroupId;
property LongPoll: TVkGroupEvents read FLongPoll;
property OnDestroy: TVkBotCallback read FOnDestroy write SetOnDestroy;
property OnError: TVkBotError read FOnError write SetOnError;
property OnInit: TVkBotCallback read FOnInit write SetOnInit;
property Token: string read GetToken write SetToken;
end;
TVkBotChat = class(TVkBot)
private
FSkipOtherBotMessages: Boolean;
FOnMessage: TVkBotMessage;
FOnMessageEdit: TVkBotMessageEdit;
FOnJoin: TVkBotJoin;
FMessageListeners: TMessageListeners;
FListeners: TEventListeners;
procedure FOnNewMessage(Sender: TObject; GroupId: Integer; Message: TVkMessage; ClientInfo: TVkClientInfo; const EventId: string);
procedure FOnEditMessage(Sender: TObject; GroupId: Integer; Message: TVkMessage; const EventId: string);
procedure FOnGroupJoin(Sender: TObject; GroupId, UserId: Integer; JoinType: TVkGroupJoinType; const EventId: string);
procedure SetOnMessageEdit(const Value: TVkBotMessageEdit);
procedure SetOnMessage(const Value: TVkBotMessage);
procedure SetSkipOtherBotMessages(const Value: Boolean);
procedure SetOnJoin(const Value: TVkBotJoin);
function HandleMessageListener(GroupId: Integer; Message: TVkMessage; ClientInfo: TVkClientInfo): Boolean;
public
class function GetInstance(const GroupId: Integer; const Token: string): TVkBotChat; overload;
constructor Create; override;
destructor Destroy; override;
function IsAdmin(PeerId: Integer; UserId: Integer): Boolean;
procedure AddMessageListener(PeerTypes: TVkPeerTypes; Method: TVkBotMessageListener);
procedure AddListener<T>(Method: T);
property MessageListeners: TMessageListeners read FMessageListeners;
property OnJoin: TVkBotJoin read FOnJoin write SetOnJoin;
property OnMessage: TVkBotMessage read FOnMessage write SetOnMessage;
property OnMessageEdit: TVkBotMessageEdit read FOnMessageEdit write SetOnMessageEdit;
property SkipOtherBotMessages: Boolean read FSkipOtherBotMessages write SetSkipOtherBotMessages;
end;
implementation
uses
VK.Bot.Utils, System.Threading, System.StrUtils, VK.Entity.Conversation,
VK.Messages, HGM.ArrayHelper;
{ TVkBot }
constructor TVkBot.Create;
begin
FVK := TCustomVK.Create(nil);
FVK.OnError := FOnVkError;
FLongPoll := TVkGroupEvents.Create(nil);
FLongPoll.VK := FVK;
FLongPoll.LongPollServer.DoSync := False;
end;
destructor TVkBot.Destroy;
begin
if Assigned(FOnDestroy) then
FOnDestroy(Self);
Stop;
FLongPoll.Free;
FVK.Free;
inherited;
end;
procedure TVkBot.FOnVkError(Sender: TObject; E: Exception; Code: Integer; Text: string);
begin
if Assigned(FOnError) then
FOnError(Self, E, Code, Text)
else
Console.AddLine(Text, RED);
end;
function TVkBot.GetGroupId: Integer;
begin
Result := FLongPoll.GroupID;
end;
class function TVkBot.GetInstance<T>: T;
begin
if not Assigned(Instance) then
Instance := T.Create;
Result := T(Instance);
end;
function TVkBot.GetToken: string;
begin
Result := FVK.Token;
end;
function TVkBot.Init: Boolean;
begin
Result := False;
Console.AddText('Initializate... ');
if Assigned(FOnInit) then
FOnInit(Self);
try
Result := FVK.Login;
finally
if Result then
Console.AddLine('I''m ready!', GREEN)
else if API.Token.IsEmpty then
Console.AddLine('Error! Token Need', RED)
else
Console.AddLine('Error!', RED);
end;
end;
function TVkBot.Run: Boolean;
begin
Result := FLongPoll.Start;
end;
procedure TVkBot.SetGroupId(const Value: Integer);
begin
FLongPoll.GroupID := Value;
end;
procedure TVkBot.SetOnDestroy(const Value: TVkBotCallback);
begin
FOnDestroy := Value;
end;
procedure TVkBot.SetOnError(const Value: TVkBotError);
begin
FOnError := Value;
end;
procedure TVkBot.SetOnInit(const Value: TVkBotCallback);
begin
FOnInit := Value;
end;
procedure TVkBot.SetToken(const Value: string);
begin
FVK.Token := Value;
end;
procedure TVkBot.Stop;
begin
FLongPoll.Stop;
end;
{ TVkBotChat }
procedure TVkBotChat.AddListener<T>(Method: T);
begin
FListeners.Add(TEventListiner.Create(@Method, TypeInfo(T)));
if TypeInfo(T) = TypeInfo(TOnWallPostAction) then
begin
end;
if TypeInfo(T) = TypeInfo(TOnGroupMessageNew) then
begin
TOnGroupMessageNew((@Method)^)(nil, 0, nil, nil, '123');
end;
end;
procedure TVkBotChat.AddMessageListener(PeerTypes: TVkPeerTypes; Method: TVkBotMessageListener);
var
Item: TMessageListener;
begin
Item.PeerTypes := PeerTypes;
Item.Method := Method;
FMessageListeners.Add(Item);
end;
constructor TVkBotChat.Create;
begin
inherited;
FSkipOtherBotMessages := False;
FMessageListeners := TMessageListeners.Create;
FListeners := TEventListeners.Create;
LongPoll.OnMessageNew := FOnNewMessage;
LongPoll.OnMessageEdit := FOnEditMessage;
LongPoll.OnGroupJoin := FOnGroupJoin;
end;
destructor TVkBotChat.Destroy;
begin
FMessageListeners.Free;
FListeners.Free;
inherited;
end;
procedure TVkBotChat.FOnEditMessage(Sender: TObject; GroupId: Integer; Message: TVkMessage; const EventId: string);
var
FMessage: TVkMessage;
begin
if FSkipOtherBotMessages and (Message.FromId < 0) then
Exit;
if Assigned(FOnMessage) then
begin
FMessage := Message.Clone<TVkMessage>;
TTask.Run(
procedure
begin
try
FOnMessageEdit(Self, GroupId, FMessage);
finally
FMessage.Free;
end;
end);
end;
end;
procedure TVkBotChat.FOnGroupJoin(Sender: TObject; GroupId, UserId: Integer; JoinType: TVkGroupJoinType; const EventId: string);
begin
if FSkipOtherBotMessages and (UserId < 0) then
Exit;
if Assigned(FOnJoin) then
begin
TTask.Run(
procedure
begin
FOnJoin(Self, GroupId, UserId, JoinType, EventId);
end);
end;
end;
function TVkBotChat.HandleMessageListener(GroupId: Integer; Message: TVkMessage; ClientInfo: TVkClientInfo): Boolean;
var
Listener: TMessageListener;
begin
for Listener in FMessageListeners do
if TVkPeerType.Create(Message.PeerId) in Listener.PeerTypes then
try
if Listener.Method(Self, GroupId, Message, ClientInfo) then
Exit(True);
except
on E: Exception do
Console.AddLine('Error with Listener: "' + E.Message + '"', RED);
end;
Result := False;
end;
function TVkBotChat.IsAdmin(PeerId, UserId: Integer): Boolean;
var
Members: TVkConversationMembers;
Params: TVkParamsConversationMembersGet;
UserIsAdmin, Found: Boolean;
begin
UserIsAdmin := False;
try
Params.PeerId(PeerId);
Params.Extended(False);
Found := False;
API.Walk(
function(Offset: Integer; var Cancel: Boolean): Integer
begin
Params.Count(200);
Params.Offset(Offset);
Result := 0;
if API.Messages.GetConversationMembers(Members, Params) then
try
Result := Length(Members.Items);
TArrayHelp.Walk<TVkConversationMember>(Members.Items,
procedure(const Item: TVkConversationMember; Index: Integer; var Cancel: Boolean)
begin
if Item.MemberId = UserId then
begin
Found := True;
Cancel := True;
UserIsAdmin := Item.IsAdmin;
end;
end);
Cancel := Found;
finally
Members.Free;
end;
end, 200);
Result := UserIsAdmin;
except
Result := False;
end;
end;
procedure TVkBotChat.FOnNewMessage(Sender: TObject; GroupId: Integer; Message: TVkMessage; ClientInfo: TVkClientInfo; const EventId: string);
var
FMessage: TVkMessage;
FClientInfo: TVkClientInfo;
begin
if FSkipOtherBotMessages and Message.IsBotFrom then
Exit;
if Assigned(FOnMessage) or (FMessageListeners.Count > 0) or (FListeners.Count > 0) then
begin
FMessage := Message.Clone<TVkMessage>;
FClientInfo := ClientInfo.Clone<TVkClientInfo>;
TTask.Run(
procedure
begin
try
if FMessageListeners.Count > 0 then
if HandleMessageListener(GroupId, FMessage, FClientInfo) then
Exit;
if FListeners.Count > 0 then
if FListeners.ForEach<TOnGroupMessageNew>(
function(Event: TEventListiner): Boolean
begin
TOnGroupMessageNew((@Event.Method)^)(Self, GroupId, Message, ClientInfo, EventId);
Result := False;
end) then
Exit;
if Assigned(FOnMessage) then
FOnMessage(Self, GroupId, FMessage, FClientInfo);
finally
FMessage.Free;
FClientInfo.Free;
end;
end);
end;
end;
class function TVkBotChat.GetInstance(const GroupId: Integer; const Token: string): TVkBotChat;
begin
if not Assigned(Instance) then
Instance := TVkBotChat.Create;
Result := TVkBotChat(Instance);
Result.GroupId := GroupId;
Result.Token := Token;
end;
procedure TVkBotChat.SetOnJoin(const Value: TVkBotJoin);
begin
FOnJoin := Value;
end;
procedure TVkBotChat.SetOnMessage(const Value: TVkBotMessage);
begin
FOnMessage := Value;
end;
procedure TVkBotChat.SetOnMessageEdit(const Value: TVkBotMessageEdit);
begin
FOnMessageEdit := Value;
end;
procedure TVkBotChat.SetSkipOtherBotMessages(const Value: Boolean);
begin
FSkipOtherBotMessages := Value;
end;
{ TEventListiner }
class function TEventListiner.Create(Method, TypeInfo: Pointer): TEventListiner;
begin
Result.Method := Method;
Result.TypeInfo := TypeInfo;
end;
{ TEventListeners }
function TEventListeners.ForEach<T>(Proc: TFunc<TEventListiner, Boolean>): Boolean;
var
Listener: TEventListiner;
begin
for Listener in Self do
if Listener.TypeInfo = TypeInfo(T) then
try
if Proc(Listener) then
Exit(True);
except
on E: Exception do
Console.AddLine('Error with Listener: "' + E.Message + '"', RED);
end;
Result := False;
end;
end.