public bool SendResponce(LeadCollection leads)
        {
            if (leads.Count == 0)
            {
                return(false);
            }
            var token     = CalculateMD5Hash($"LeadBolid-{DateTime.Now:yyyy-MM-dd}").ToLower();
            var sourceUrl = string.Concat(_settings.SourceUrl, $"?token={token}");
            var client    = new HttpClient {
                BaseAddress = new Uri(sourceUrl)
            };
            var request = new HttpRequestMessage(HttpMethod.Put, sourceUrl);

            request.Content = new FormUrlEncodedContent(getData(leads));
            var response = client.SendAsync(request);

            response.Wait();
            if (!response.IsCompletedSuccessfully)
            {
                return(false);
            }
            var resultText = response.Result.Content.ReadAsStringAsync();

            resultText.Wait();
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject <Result>(resultText.Result);

            if (result == null || string.IsNullOrWhiteSpace(result.Message))
            {
                return(false);
            }

            return(int.TryParse(string.Join("", result.Message.Where(char.IsDigit)), out var updatedCount) && updatedCount > 0);
        }
        public async void Run()
        {
            // Get new batch to check
            _isRunning = true;
            var newLeads = LeadMonitoringSevice.GetRequestedLeads();

            if (newLeads.Count == 0)
            {
                return;
            }
            var allLeads = new LeadCollection(newLeads);

            if (TelegramService.IsConnected && TelegramService.IsAuthorized)
            {
                TelegramService.VerifyNumbers(allLeads, LeadMonitoringSevice.GetNewRequests(newLeads));
                foreach (var lead in allLeads.Leads)
                {
                    if (LeadMonitoringSevice.LeadCachedCollection.TryGetLead(lead.Key, out Lead cachedLead))
                    {
                        lead.Value.IsTelegram   = cachedLead.IsTelegram;
                        lead.Value.IsViber      = cachedLead.IsViber;
                        lead.Value.IsWhatsApp   = cachedLead.IsWhatsApp;
                        lead.Value.TelegramUser = cachedLead.TelegramUser;
                    }
                }

                if (LeadMonitoringSevice.SendResponce(allLeads))
                {
                    // TODO: logger
                    LeadMonitoringSevice.Cache(allLeads.Leads.Values.ToList());
                }
            }

            _isRunning = false;
        }
Пример #3
0
        public bool VerifyNumbers(LeadCollection allLeadCollection, List <LeadRequest> firstTimeRequestedLeads)
        {
            if (!IsConnected || !IsAuthorized)
            {
                throw new InvalidOperationException("Service isn't running. Start service before executing some commands");
                return(false);
            }

            if (firstTimeRequestedLeads.Count == 0)
            {
                return(true);
            }

            try
            {
                var contacts = firstTimeRequestedLeads.Select(lead => new LeadToContactAdapter(lead.Id.ToString(), lead.Phone))
                               .ToList();
                var task = TelegramClient.ImportContactsAsync(contacts);
                task.Wait();
                var imported      = task.Result.Users;
                var toDeleteUsers = new List <TLAbsInputUser>();
                var importedUsers = imported.Select(user => user as TLUser)
                                    .Where(user => user != null && !string.IsNullOrWhiteSpace(user.Username)).ToDictionary(user => user.Phone);
                foreach (var lead in allLeadCollection.Leads)
                {
                    if (importedUsers.TryGetValue(lead.Key, out TLUser importedUser))
                    {
                        toDeleteUsers.Add(new TLInputUser()
                        {
                            AccessHash = importedUser.AccessHash ?? 0, UserId = importedUser.Id
                        });
                        lead.Value.IsTelegram   = true;
                        lead.Value.TelegramUser = importedUser.Username;
                    }
                }

                if (toDeleteUsers.Count > 0)
                {
                    TelegramClient.DeleteContactsAsync(toDeleteUsers).Wait();
                }

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }
            return(true);
        }
        private IEnumerable <KeyValuePair <string, string> > getData(LeadCollection leads)
        {
            var iterator = 0;

            foreach (var lead in leads.Leads)
            {
                yield return(new KeyValuePair <string, string>($"socials[{iterator}][lead]", $"{lead.Value.Id}"));

                yield return(new KeyValuePair <string, string>($"socials[{iterator}][telegram]", $"{lead.Value.IsTelegram}"));

                yield return(new KeyValuePair <string, string>($"socials[{iterator}][viber]", $"{lead.Value.IsViber}"));

                yield return(new KeyValuePair <string, string>($"socials[{iterator}][whatsapp]", $"{lead.Value.IsWhatsApp}"));

                iterator++;
            }
        }
 public LeadMonitoringSevice(Settings settings)
 {
     _settings            = settings;
     LeadCachedCollection = new LeadCollection(new List <LeadRequest>());
 }