· 6 years ago · Dec 11, 2019, 07:12 PM
1using System;
2using VkNet;
3using VkNet.Enums.Filters;
4using VkNet.Exception;
5using VkNet.Model;
6using Microsoft.Extensions.DependencyInjection;
7using VkNet.Enums.SafetyEnums;
8using ApiAiSDK;
9using ApiAiSDK.Model;
10using System.Data.Entity;
11using VkNet.Model.RequestParams;
12using System.Collections.Generic;
13using System.Linq;
14
15namespace VkBot
16{
17 public class Program
18 {
19 public static ulong groupID = 189723110;
20 public static string GroupKey = "9b0d14d6029854d0fb4e33debdcc623c339f1ef1933787bd04e2b7d82ad8e74311aa3d11edb44bf2bd611";
21 public static string AIKey = "1f7285bb34454e02949bfdb7d2d66bdb";
22
23 public static VkApi api = new VkApi();
24 static ApiAi ApiAi;
25 static long[] users;
26
27 public static void Main()
28 {
29 AIConfiguration config = new AIConfiguration("AIKey", SupportedLanguage.Russian);
30 ApiAi = new ApiAi(config);
31 api.Authorize(new ApiAuthParams() { AccessToken = GroupKey });
32 while (true)
33 {
34 var s = api.Groups.GetLongPollServer(groupID);
35 var poll = api.Groups.GetBotsLongPollHistory(
36 new BotsLongPollHistoryParams() { Server = s.Server, Ts = s.Ts, Key = s.Key, Wait = 25 });
37 if (poll?.Updates == null) continue;
38 foreach (var a in poll.Updates)
39 {
40 if (a.Type == GroupUpdateType.MessageNew)
41 {
42 //ProcessingMessage(a.Message.Text.ToLower(), a.Message.FromId);
43 SendMessage("Hello", Вот сюда нужно вставить ID, но как его узнать, я не знаю);
44 Console.WriteLine(api.UserId);
45 }
46 }
47 }
48 }
49
50 public static void ProcessingMessage(string userMessage, long? userID)
51 {
52 AIResponse response = ApiAi.TextRequest(userMessage);
53 string answer = response.Result.Fulfillment.Speech;
54 if (answer == "")
55 {
56 answer = "Sorry, я не понимаю :(";
57 }
58 SendMessage(answer, userID);
59 }
60
61 public static void SendMessage(string message, long? userID)
62 {
63 Random rnd = new Random();
64 api.Messages.Send(new MessagesSendParams
65 {
66 RandomId = rnd.Next(),
67 UserId = userID,
68 GroupId = groupID,
69 Message = message
70 });
71 }
72 }
73}