· 6 years ago · Jan 05, 2020, 03:44 PM
1static class Bot
2 {
3 static Timer CheckPool;
4
5 public static void Initialization(string AppToken, ulong GroupId)
6 {
7 Api = new VkApi();
8 Api.Authorize(new ApiAuthParams() { AccessToken = AppToken });
9 RandomId = new Random();
10 CheckPool = new Timer(1)
11 {
12 AutoReset = true,
13 Enabled = true
14 };
15 CheckPool.Elapsed += CheckPool_Tick;
16 Response = Api.Groups.GetLongPollServer(GroupId);
17 }
18
19 static void CheckPool_Tick(object sender, ElapsedEventArgs e)
20 {
21 CheckPool.Stop();
22
23 var Poll = Api.Groups.GetBotsLongPollHistory
24 (
25 new BotsLongPollHistoryParams()
26 {
27 Server = Response.Server,
28 Ts = Response.Ts,
29 Key = Response.Key,
30 Wait = 1
31 }
32 );
33
34 if (Poll?.Updates != null)
35 foreach (var Event in Poll.Updates)
36 if (Event.Type == GroupUpdateType.MessageNew)
37 GetMessage((long)Event.Message.UserId, Event.Message.Body);
38
39 Response.Ts = Poll.Ts;
40 CheckPool.Start();
41 }
42
43 static void GetMessage(long UserId, string Text)
44 {
45 Log.Write(UserId + ": " + Text);
46 SendMessage(UserId, Text);
47 }
48 static void SendMessage(long UserId, string Text)
49 {
50 Api.Messages.Send(new MessagesSendParams()
51 {
52 UserId = UserId,
53 Message = Text,
54 RandomId = RandomId.Next()
55 });
56 }
57
58 static Random RandomId;
59 static VkApi Api;
60 static LongPollServerResponse Response;
61 }