· 4 years ago · Jul 06, 2021, 09:56 AM
1// Api.cs
2
3using System;
4using System.Collections.Generic;
5using System.Net.Http;
6using System.Threading.Tasks;
7using Newtonsoft.Json;
8
9namespace EasyKushka
10{
11 class VkResponse
12 {
13 public IDictionary<string, string> response;
14 }
15
16 public class Api
17 {
18 private readonly static HttpClient HttpClient = new HttpClient();
19 private readonly string ApiVersion;
20 private readonly string SecretKey;
21
22 private const string BaseUri = "https://api.vk.com/";
23 private const string MethodUri = "https://api.vk.com/method/";
24
25 public Api(string apiVersion, string secretKey)
26 {
27 (ApiVersion, SecretKey) = (apiVersion, secretKey);
28 }
29
30 public async Task<HttpResponseMessage> CallMethod(string methodName, Dictionary<string, string> @params)
31 {
32 @params.Add("access_token", SecretKey);
33 @params.Add("v", ApiVersion);
34
35 UriBuilder baseUri = new UriBuilder(MethodUri + methodName);
36
37 var content = new FormUrlEncodedContent(@params);
38 var apiRes = await HttpClient.PostAsync(baseUri.Uri, content);
39 if (!apiRes.IsSuccessStatusCode)
40 {
41 throw new ArgumentException(await apiRes.Content.ReadAsStringAsync());
42 }
43
44 return apiRes;
45 }
46
47 public async Task MessagesSend(string peerId, string message)
48 {
49 await CallMethod("messages.send", new Dictionary<string, string>()
50 {
51 {"peer_id", peerId},
52 {"message", message},
53 {"random_id", new Random().Next(Int32.MinValue, Int32.MaxValue).ToString()}
54 });
55 }
56
57 public async Task<HttpResponseMessage> GroupsGetLongPollSettings(string groupId)
58 {
59 return await CallMethod("groups.getLongPollSettings", new Dictionary<string, string>()
60 {
61 {"group_id", groupId},
62 });
63 }
64
65 public async Task GroupsSetLongPollSettings(string groupId, Dictionary<string, string> @params)
66 {
67 @params.Add("group_id", groupId);
68 await CallMethod("groups.setLongPollSettings", @params);
69 }
70
71 public async Task<LongPollInfo> GroupsGetLongPollServer(string groupId)
72 {
73 var apiRes = await CallMethod("groups.getLongPollServer", new Dictionary<string, string>()
74 {
75 {"group_id", groupId}
76 });
77 var vkResponse = JsonConvert.DeserializeObject<VkResponse>(await apiRes.Content.ReadAsStringAsync());
78 LongPollInfo lpinfo = new LongPollInfo();
79 lpinfo.key = vkResponse.response["key"];
80 lpinfo.server = vkResponse.response["server"];
81 lpinfo.ts = vkResponse.response["ts"];
82 return lpinfo;
83 }
84 }
85}
86
87// LongPoll.cs
88
89using System;
90using System.Collections.Generic;
91using System.Net.Http;
92using Newtonsoft.Json;
93using System.Threading.Tasks;
94namespace EasyKushka
95{
96 public class LongPollInfo
97 {
98 public string key;
99 public string server;
100 public string ts;
101 }
102 public class LongPoll
103 {
104 private readonly static HttpClient HttpClient = new HttpClient();
105 private readonly Api VkApi;
106 private readonly string GroupId;
107 private string Event;
108
109 public LongPoll(Api vkApi, string groupId)
110 {
111 VkApi = vkApi;
112 GroupId = groupId;
113 }
114
115
116 public async Task<HttpResponseMessage> GetEvents(LongPollInfo lpinfo, string waitTime)
117 {
118 UriBuilder baseUri = new UriBuilder(lpinfo.server);
119 var json = JsonConvert.SerializeObject(lpinfo);
120 var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
121 dictionary.Add("act", "a_check");
122 dictionary.Add("wait", waitTime);
123 dictionary.Add("mode", "2");
124 dictionary.Add("version", "2");
125 var content = new FormUrlEncodedContent(dictionary);
126 var resultApi = await HttpClient.PostAsync(baseUri.Uri, content);
127 Console.WriteLine(await resultApi.Content.ReadAsStringAsync());
128 return resultApi;
129 // $https://{server}?act=a_check&key={key}&ts={ts}&wait=25&mode=2&version=2
130 }
131 }
132}
133
134// Program.cs
135using System;
136using System.Collections.Generic;
137using System.Net;
138using System.Net.Http;
139using Newtonsoft.Json;
140using System.Threading.Tasks;
141namespace EasyKushka
142{
143 class Program
144 {
145 static async Task Main(string[] args)
146 {
147 Api api = new Api("5.131", "90367ca154c6e29bde05193361b81af9701ed7e342c71813f53e36d6a9eb6773476eae66a5254eb124c3d");
148
149 await api.GroupsSetLongPollSettings("205136689", new Dictionary<string, string>()
150 {
151 {"message_event", "1"}
152 });
153 await api.GroupsGetLongPollSettings("205136689");
154 LongPoll lp = new LongPoll(api, "205136689");
155 LongPollInfo lpinfo = await api.GroupsGetLongPollServer("205136689");
156
157 Console.WriteLine(lpinfo.key + " " + lpinfo.server + " " + lpinfo.ts);
158 await lp.GetEvents(lpinfo, "10");
159
160
161 }
162 }
163}