示例#1
0
        /// <summary>
        /// Constructor, which uses
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="key"></param>
        /// <param name="callback"></param>
        public VkBot(ulong appId, string token, IncomingMessageCallback callback, int exclusivePeerId = 0)
        {
            ApiAuthParams apiParams = new ApiAuthParams
            {
                ApplicationId = appId,

                Settings = Settings.Messages
            };

            Initialize(token, appId, callback, exclusivePeerId);
        }
示例#2
0
        /// <summary>
        /// Simple constructor for a new bot.
        /// </summary>
        /// <param name="appId">application id provided by vk.com</param>
        /// <param name="email">vk.com login</param>
        /// <param name="pass">vk.com password</param>
        /// <param name="callback">Delegate. Executed every time bot recieves incoming message</param>
        public VkBot(ulong appId, string email, string pass, IncomingMessageCallback callback, int exclusivePeerId = 0)
        {
            ApiAuthParams apiParams = new ApiAuthParams
            {
                ApplicationId = appId,
                Login         = email,
                Password      = pass,
                Settings      = Settings.Messages
            };

            Initialize(apiParams, appId, callback, exclusivePeerId);
        }
示例#3
0
        private void Initialize(dynamic apiParams, ulong appId, IncomingMessageCallback callback, int exclusivePeerId)
        {
            vk = new VkApi();
            vk.Authorize(apiParams);

            var firstResponse = vk.Messages.GetLongPollServer();

            //Console.WriteLine("key: " + firstResponse.Key + "\nserver: " + firstResponse.Server + "\nts: " + firstResponse.Ts);
            Console.WriteLine("Ready.");

            server = new LongPollServer(firstResponse);

            if (exclusivePeerId != 0)
            {
                this.exclusivePeerId = exclusivePeerId;
                this.answerAll       = false;
            }

            server.StartPollingAsync(x =>
            {
                //get new messages, detailed info on format: https://vk.com/dev/using_longpoll
                var newMsgs = ((JArray)x["updates"])
                              .Where(t => (int)t[0] == 4 &&
                                     (answerAll ?                                              // if it answers all
                                      (int)t[3] != vk.UserId :                                 // then answer everybody except yourself
                                      ((int)t[3] == vk.UserId || (int)t[3] == exclusivePeerId) // else answer only yourself or exclusive peer
                                     )                                                         //TODO: probably logic bug here. needs fix
                                     )
                              .Select(c => c).ToList();

                //actually got new messages
                if (newMsgs.Count != 0)
                {
                    try
                    {
                        var psr = new PollServerResponse(x);
                        // apply rules one by one looking for one, which applies here
                        rules.ForEach(rule => rule.Apply(psr));
                        callback(this, psr);
                    }
                    catch (FormatException)
                    {
                        //sometimes it happens. just nevermind :)
                        Console.WriteLine("Known issue. Couldn't parse server response to struct.\n");
                    }
                }
            });
        }
示例#4
0
        /// <summary>
        /// Constructor. Inputs vk.com account credentials from console
        /// </summary>
        /// <param name="appId">application id provided by vk.com</param>
        /// <param name="callback">Delegate. Executed every time bot recieves incoming message</param>
        public VkBot(ulong appId, IncomingMessageCallback callback, int exclusivePeerId = 0)
        {
            Console.WriteLine("Logging in to vk.com");

            Console.Write("E-mail: ");
            string email = Console.ReadLine();

            string pass = "";

            #region inputing password
            Console.Write("Enter your password: "******"*");
                }
                else
                {
                    if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
                    {
                        pass = pass.Substring(0, (pass.Length - 1));
                        Console.Write("\b \b");
                    }
                }
            }
            // Stops Receving Keys Once Enter is Pressed
            while (key.Key != ConsoleKey.Enter);
            Console.WriteLine();
            #endregion

            ApiAuthParams apiParams = new ApiAuthParams
            {
                ApplicationId = appId,
                Login         = email,
                Password      = pass,
                Settings      = Settings.Messages
            };

            Initialize(apiParams, appId, callback, exclusivePeerId);
        }