· 6 years ago · Dec 10, 2019, 09:28 PM
1public class Program
2 {
3 public static ulong groupID = 189723110;
4 public static string GroupKey = "9b0d14d6029854d0fb4e33debdcc623c339f1ef1933787bd04e2b7d82ad8e74311aa3d11edb44bf2bd611";
5 public static string AIKey = "1f7285bb34454e02949bfdb7d2d66bdb";
6
7 public static VkApi api = new VkApi();
8 static ApiAi ApiAi;
9
10 public static void Main()
11 {
12 AIConfiguration config = new AIConfiguration("AIKey", SupportedLanguage.Russian);
13 ApiAi = new ApiAi(config);
14 api.Authorize(new ApiAuthParams() { AccessToken = GroupKey });
15
16 while (true)
17 {
18 var s = api.Groups.GetLongPollServer(groupID);
19 var poll = api.Groups.GetBotsLongPollHistory(
20 new BotsLongPollHistoryParams() { Server = s.Server, Ts = s.Ts, Key = s.Key, Wait = 25 });
21 if (poll?.Updates == null) continue;
22 foreach (var a in poll.Updates)
23 {
24 if (a.Type == GroupUpdateType.MessageNew)
25 {
26 ProcessingMessage(a.Message.Text.ToLower(), a.Message.FromId);
27 }
28 }
29 }
30 }
31
32 public static void ProcessingMessage(string userMessage, long? userID)
33 {
34 AIResponse response = ApiAi.TextRequest(userMessage);
35 string answer = response.Result.Fulfillment.Speech;
36 if (answer == "")
37 {
38 answer = "Sorry, я не понимаю :(";
39 }
40 SendMessage(answer, userID);
41 }
42
43 public static void SendMessage(string message, long? userID)
44 {
45 Random rnd = new Random();
46 api.Messages.Send(new MessagesSendParams
47 {
48 RandomId = rnd.Next(),
49 UserId = userID,
50 Domain = "csharpisthebest",
51 GroupId = groupID,
52 Message = message
53 });
54 }
55 }