· 6 years ago · Dec 11, 2019, 07:24 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 while (true)
16 {
17 var s = api.Groups.GetLongPollServer(groupID);
18 var poll = api.Groups.GetBotsLongPollHistory(
19 new BotsLongPollHistoryParams() { Server = s.Server, Ts = s.Ts, Key = s.Key, Wait = 25 });
20 if (poll?.Updates == null) continue;
21 foreach (var a in poll.Updates)
22 {
23 if (a.Type == GroupUpdateType.MessageNew)
24 {
25 ProcessingMessage(a.Message.ReplyMessage.Text.ToLower(), a.Message.UserId);
26 }
27 }
28 }
29 }
30
31 public static void ProcessingMessage(string userMessage, long? userID)
32 {
33 AIResponse response = ApiAi.TextRequest(userMessage);
34 string answer = response.Result.Fulfillment.Speech;
35 if (answer == "")
36 {
37 answer = "Sorry, я не понимаю :(";
38 }
39 SendMessage(answer, userID);
40 }
41
42 public static void SendMessage(string message, long? userID)
43 {
44 Random rnd = new Random();
45 api.Messages.Send(new MessagesSendParams
46 {
47 RandomId = rnd.Next(),
48 UserId = userID,
49 GroupId = groupID,
50 Message = message
51 });
52 }
53 }