Exemplo n.º 1
0
        private static void newEvents(IMessageLongPollWatcher watcher, LongPollHistoryResponse history)
        {
            List <long>    ignoreList = new List <long>();
            List <Message> messages   = new List <Message>();

            if (history.History.Count > 0)
            {
                foreach (var item in history.History)
                {
                    if (item[0] == 2 || item[0] == 5)
                    {
                        if (item[2] == 131200 || item[2] == 128 ||
                            item[0] == 5)
                        {
                            ignoreList.Add(item[1]);
                        }
                    }
                }
                foreach (var message in history.Messages)
                {
                    if (ignoreList.Contains(Convert.ToInt64(message.Id)))
                    {
                        continue;
                    }
                    messages.Add(message);
                }
                newMessages(messages);
            }
        }
Exemplo n.º 2
0
        private LongPollHistoryResponse GetLongPoolHistory()
        {
            if (!Ts.HasValue)
            {
                GetLongPoolServer(null);
            }
            MessagesGetLongPollHistoryParams rp = new MessagesGetLongPollHistoryParams();

            rp.Ts  = Ts.Value;
            rp.Pts = Pts;

            int c = 0;
            LongPollHistoryResponse history = null;
            string errorLog = "";

            while (c < 5 && history == null)
            {
                c++;
                try
                {
                    history = vkAcc.Messages.GetLongPollHistory(rp);
                }
                catch (TooManyRequestsException)
                {
                    Thread.Sleep(150);
                    c--;
                }
                catch (Exception ex)
                {
                    DataBaseCenter        dataBase  = DataBaseCenter.Create();
                    System.Data.DataTable dataTable = dataBase.GetDataTable("SELECT значение FROM Настройки WHERE название='vk'");
                    string[] str = dataTable.Rows[0].ItemArray[0].ToString().Split("|".ToCharArray());
                    ulong    appId;
                    ulong.TryParse(str[0], out appId);
                    vkAcc.Authorize(new ApiAuthParams
                    {
                        ApplicationId = appId,
                        Login         = str[1],
                        Password      = str[2],
                        AccessToken   = str[3],
                        Settings      = Settings.All
                    });
                    try { GetLongPoolServer(null); } catch { };
                }
            }

            if (history != null)
            {
                Pts = history.NewPts;
                foreach (var m in history.Messages)
                {
                    m.FromId = m.Type == VkNet.Enums.MessageType.Sended ? vkAcc.UserId : m.UserId;
                }
            }

            return(history);
        }
Exemplo n.º 3
0
 private void _SaveHistory(LongPollHistoryResponse response)
 {
     if (response == null)
     {
         Debug.WriteLine("_SaveHistory got successful response, but it is NULL");
     }
     else
     {
         App.Current.UpdatesService.PutHistory(response.LongPollHistory);
     }
 }
Exemplo n.º 4
0
 private void RunEventHandlers(LongPollHistoryResponse historyResponse)
 {
     foreach (var update in historyResponse.Messages)
     {
         if (update == null)
         {
             continue;
         }
         NewMessageReceived?.Invoke(update);
     }
 }
Exemplo n.º 5
0
        public void GetMessages()
        {
            var longPoll = MainWindow.api.Messages.GetLongPollServer(needPts: true);

            try
            {
                LongPollHistoryResponse serv = MainWindow.api.Messages.GetLongPollHistory(@params: new MessagesGetLongPollHistoryParams()
                {
                    Pts = longPoll.Pts
                });
                int mescount = 0;
                while (true)
                {
                    try
                    {
                        serv = MainWindow.api.Messages.GetLongPollHistory(@params: new MessagesGetLongPollHistoryParams()
                        {
                            Pts = longPoll.Pts
                        });
                        //MessageBox.Show(serv.Messages[serv.Messages.Count-1].Text);
                        if (serv.Messages.Count != mescount)
                        {
                            Message LastMessage = serv.Messages[serv.Messages.Count - 1];
                            if (LastMessage.Type == VkNet.Enums.MessageType.Received && ActiveUserId == LastMessage.FromId)
                            {
                                Dispatcher.BeginInvoke(new ThreadStart(delegate { Chat.Document.Blocks.Add(new Paragraph(new Run(LastMessage.FromId.ToString() + LastMessage.Text))); }));

                                /*long? message_id = serv.Messages[serv.Messages.Count - 1].Id;
                                 * var idlist = new List<ulong>();
                                 * idlist.Add((ulong)message_id);
                                 * var Message = MainWindow.api.Messages.GetById(messageIds: idlist, fields: Enumerable.Empty<string>());
                                 * Attachment documentAttachment = Message[0].Attachments.First(x => x.Type == typeof(Photo));
                                 * string uri = ((Photo)documentAttachment.Instance).Sizes[0].Url.ToString();
                                 * MessageBox.Show(uri);*/
                                mescount = serv.Messages.Count;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.ToString());
                        longPoll = MainWindow.api.Messages.GetLongPollServer(needPts: true);
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
Exemplo n.º 6
0
        static async void WatchAsync(object state)
        {
            LongPollHistoryResponse history = await GetLongPoolHistoryAsync();

            if (history.Messages.Count > 0)
            {
                CurrentSleepSteps = 1;
                NewMessages?.Invoke(vkapi, history.Messages);
            }
            else if (CurrentSleepSteps < MaxSleepSteps)
            {
                CurrentSleepSteps++;
            }
            WatchTimer.Change(CurrentSleepSteps * StepSleepTime, Timeout.Infinite);
        }
Exemplo n.º 7
0
        private LongPollHistoryResponse GetLongPoolHistory()
        {
            if (!Ts.HasValue)
            {
                GetLongPoolServer(null);
            }
            MessagesGetLongPollHistoryParams rp = new MessagesGetLongPollHistoryParams();

            rp.Ts  = Ts.Value;
            rp.Pts = Pts;

            int c = 0;
            LongPollHistoryResponse history = null;
            string errorLog = "";

            while (c < 5 && history == null)
            {
                c++;
                try
                {
                    history = _account.Messages.GetLongPollHistory(rp);
                }
                catch (TooManyRequestsException)
                {
                    Thread.Sleep(150);
                    c--;
                }
                catch (Exception ex)
                {
                    errorLog += string.Format("{0} - {1}{2}", c, ex.Message, Environment.NewLine);
                }
            }

            if (history != null)
            {
                Pts = history.NewPts;
                foreach (var m in history.Messages)
                {
                    m.FromId = m.Type == VkNet.Enums.MessageType.Sended ? _account.UserId : m.UserId;
                }
            }
            else
            {
                throw new NotImplementedException(errorLog);
            }

            return(history);
        }
Exemplo n.º 8
0
        static LongPollHistoryResponse GetLongPoolHistory()
        {
            if (!Ts.HasValue)
            {
                GetLongPoolServer(null);
            }
            MessagesGetLongPollHistoryParams rp = new MessagesGetLongPollHistoryParams();

            rp.Ts  = Ts.Value;
            rp.Pts = Pts;
            int i = 0;
            LongPollHistoryResponse history = null;
            string errorLog = "";

            while (i < 5 && history == null)
            {
                i++;
                try
                {
                    history = vkapi.Messages.GetLongPollHistory(rp);
                }
                catch (TooManyRequestsException)
                {
                    Thread.Sleep(150);
                    i--;
                }
                catch (Exception ex)
                {
                    errorLog += string.Format("{0} - {1}{2}", i, ex.Message, Environment.NewLine);
                }
            }

            if (history != null)
            {
                Pts = history.NewPts;
                foreach (var m in history.Messages)
                {
                    m.FromId = m.Type == MessageType.Sended ? vkapi.UserId : m.UserId;
                }
            }
            else
            {
                ColorMessage(errorLog, ConsoleColor.Red);
            }
            return(history);
        }
        private void _ParseResponse(string response)
        {
            try
            {
                response = _FixResponseArrayString(response);

                LongPollHistoryResponse history = SerializeHelper.Deserialise <LongPollHistoryResponse>(response);

                _callback(history);
            }
            catch
            {
                Debug.WriteLine("Parse response from MessagesGetLongPollHistory failed.");

                _callback(null);
            }
        }
Exemplo n.º 10
0
        private static LongPollHistoryResponse GetLongPoolHistory()
        {
            if (!ts.HasValue)
            {
                GetLongPoolServer(null);
            }
            var pollHistoryParams = new MessagesGetLongPollHistoryParams {
                Ts = ts.Value, Pts = pts
            };
            var i = 0;
            LongPollHistoryResponse history = null;
            var errorLog = "";

            while (i < 5 && history == null)
            {
                i++;
                try
                {
                    history = vkApi.Messages.GetLongPollHistory(pollHistoryParams);
                }
                catch (TooManyRequestsException)
                {
                    Thread.Sleep(150);
                    i--;
                }
                catch (Exception ex)
                {
                    errorLog += $"{i} - {ex.Message}{Environment.NewLine}";
                }
            }

            if (history == null)
            {
                return(history);
            }
            pts = history.NewPts;
            foreach (var message in history.Messages)
            {
                message.FromId = message.Type == MessageType.Sended ? vkApi.UserId : message.FromId;
            }
            return(history);
        }
Exemplo n.º 11
0
        public void Response()
        {
            while (DateTime.Now - LastResponse < TimeSpan.FromSeconds(1 / Api.RequestsPerSecond + 0.01))
            {
                ;
            }
            LastResponse = DateTime.Now;
            Api.Account.SetOnline(false);

            LongPollHistoryResponse longpoll = Api.Messages.GetLongPollHistory(LongPollParams);

            LongPollParams.Pts = longpoll.NewPts;

            longpoll.Messages.OrderBy(new Func <VkNet.Model.Message, DateTime>(func));

            foreach (var message in longpoll.Messages)
            {
                if (message.Type == MessageType.Sended)
                {
                    continue;
                }
                Message msg = new Message();
                msg.ChatId = message.ChatId.GetValueOrDefault(-message.UserId.GetValueOrDefault());
                msg.Text   = message.Body;
                User from = Api.Users.Get(message.UserId.GetValueOrDefault(), ProfileFields.All);
                msg.From = "<a href=\"https://vk.com/id" + from.Id.ToString() + "\">" + from.FirstName + " " + from.LastName + "</a>";
                foreach (VkNet.Model.Attachments.Attachment attachment in message.Attachments)
                {
                    msg.Attachments.Add(TransformAttachment(attachment));
                }
                if (LongPollParams.MaxMsgId < message.Id)
                {
                    LongPollParams.MaxMsgId = message.Id.GetValueOrDefault();
                }
                MessageCallblack(msg);
                Api.Messages.MarkAsRead(message.Id.GetValueOrDefault());
            }
        }