· 6 years ago · Jan 22, 2020, 07:42 PM
1using System;
2using System.Linq;
3using System.IO;
4using System.Threading;
5using System.Threading.Tasks;
6using VkNet;
7using VkNet.Enums.SafetyEnums;
8using VkNet.Exception;
9using VkNet.Model;
10using VkNet.Model.Keyboard;
11using VkNet.Model.RequestParams;
12
13namespace ConsoleApp19
14{
15 class Program
16 {
17 #region Info
18 private static string MyAppToken = "";
19 private static ulong MyGroupId = ;
20 #endregion
21
22 static void Main(string[] args)
23 {
24 var api = new VkApi();
25 api.Authorize(new ApiAuthParams { AccessToken = MyAppToken });
26 var random = new Random();
27 var server = api.Groups.GetLongPollServer(MyGroupId);
28
29 KeyboardBuilder key = new KeyboardBuilder();
30 key.AddButton("Тест", "1", KeyboardButtonColor.Primary, "1");
31 MessageKeyboard keyboard = key.Build();
32
33 Console.WriteLine("Цикл запущен");
34 while (true)
35 {
36 try
37 {
38 var poll = api.Groups.GetBotsLongPollHistory(
39 new BotsLongPollHistoryParams
40 { Server = server.Server, Ts = server.Ts, Key = server.Key, Wait = 1 });
41 if (poll?.Updates == null) continue;
42 server.Ts = poll.Ts;
43 foreach (var update in poll.Updates)
44 {
45 if (update.Type == GroupUpdateType.MessageNew)
46 {
47 var p = api.Users.Get(new long[] {
48 Convert.ToInt32(update.Message.FromId)
49 }).FirstOrDefault();
50 string FNS = $"[id{p.Id}|{p.FirstName}]";
51 Console.WriteLine(new String('=', 70));
52 if (update.Message.PeerId > 1999999999)
53 {
54 Console.WriteLine($"Пользователь: {p.FirstName} {p.LastName} ({p.Id}) (Беседа ид: {update.Message.PeerId})\nСообщение: {update.Message.Text}\nВремя: {update.Message.Date}");
55 }
56 else
57 {
58 Console.WriteLine($"Пользователь: {p.FirstName} {p.LastName} ({p.Id}) (ЛС)\nСообщение: {update.Message.Text}\nВремя: {update.Message.Date}");
59 }
60 Console.WriteLine(new String('=', 70));
61
62 api.Messages.Send(new MessagesSendParams()
63 {
64 RandomId = random.Next(),
65 PeerId = update.Message.PeerId,
66 Keyboard = keyboard,
67 Message = $"{FNS}, {update.Message.Text}"
68 });
69 }
70 }
71 }
72 catch (LongPollException exception)
73 {
74 if (exception is LongPollOutdateException outdateException)
75 {
76 server.Ts = outdateException.Ts;
77 }
78 else
79 {
80 server = api.Groups.GetLongPollServer(MyGroupId);
81 }
82 }
83 catch (Exception e)
84 {
85 Console.WriteLine(e);
86 }
87 }
88 }
89 }
90}