private async Task StartTelegram() { try { DataReceiveHelper.Information.AppendLine("Telegram Function Is Started"); _client = await DataReceiveHelper.NewClientAsync(); DataReceiveHelper.Information.AppendLine("New Client Created"); //await client.SetLogStreamAsync(new TdApi.LogStream.LogStreamFile() { Path = "tdlib.log" }); _client.UpdateReceived += Client_UpdateReceived; DataReceiveHelper.Information.AppendLine("Client_UpdateReceived Created"); //await _client.GetChatsAsync(null, Int64.MaxValue, 0, 100); //DataReceiveHelper.Information.AppendLine("GetChats Completed"); } catch (TdException e) { var error = e.Error; DataReceiveHelper.Information.AppendLine("StartTelegram-1 " + error.Message); } catch (Exception ex) { DataReceiveHelper.Information.AppendLine("StartTelegram-2 " + ex.Message); } }
private async void GetOldData() { try { var goldOptionsBuilder = new DbContextOptionsBuilder <TomorrowMeltedGoldContext>(); goldOptionsBuilder.UseSqlServer(_connectionString); var dollarOptionsBuilder = new DbContextOptionsBuilder <TomorrowHeratDollarContext>(); dollarOptionsBuilder.UseSqlServer(_connectionString); TdApi.Chat goldChannel = null; TdApi.Chat dollarChannel = null; using var client = await DataReceiveHelper.NewClientAsync(); await foreach (var chat in GetChannels(client)) { //DataReceiveHelper.Information.AppendLine($"{chat.Title} ID {chat.Id}"); if (chat.Title.Equals(GoldChannelName)) { goldChannel = chat; } if (chat.Title.Equals(DollarChannelName)) { dollarChannel = chat; } } var goldMessageId = goldChannel?.LastMessage.Id ?? 0; var dollarMessageId = dollarChannel?.LastMessage.Id ?? 0; for (var j = 0; j < 10000; j++) { try { if (goldChannel != null) { var messages = await client.GetChatHistoryAsync(goldChannel.Id, goldMessageId, 0, 10); foreach (var message in messages.Messages_) { goldMessageId = message.Id; var messageText = ""; if (message.Content is TdApi.MessageContent.MessageText text) { messageText = text.Text.Text; } var price = FindGoldPrice(messageText); if (price <= 0) { continue; } var tomorrowMeltedGold = new TomorrowMeltedGold { MessageId = message.Id, MessageText = messageText, Date = UnixTimeStampToDateTime(message.Date), Price = price }; await using var goldContext = new TomorrowMeltedGoldContext(goldOptionsBuilder.Options); if (goldContext.TomorrowMeltedGold.All(item => item.MessageId != tomorrowMeltedGold.MessageId)) { goldContext.Add(tomorrowMeltedGold); await goldContext.SaveChangesAsync(); } } } if (dollarChannel != null) { var messages = await client.GetChatHistoryAsync(dollarChannel.Id, dollarMessageId, 0, 10); foreach (var message in messages.Messages_) { dollarMessageId = message.Id; var messageText = ""; if (message.Content is TdApi.MessageContent.MessageText text) { messageText = text.Text.Text; } var price = FindDollarPrice(messageText); if (price <= 0) { continue; } var tomorrowHeratDollar = new TomorrowHeratDollar { MessageId = message.Id, MessageText = messageText, Date = UnixTimeStampToDateTime(message.Date), Price = price }; await using var dollarContext = new TomorrowHeratDollarContext(dollarOptionsBuilder.Options); if (dollarContext.TomorrowHeratDollar.All(item => item.MessageId != tomorrowHeratDollar.MessageId)) { dollarContext.Add(tomorrowHeratDollar); await dollarContext.SaveChangesAsync(); } } } } catch (TdException e) { var error = e.Error; DataReceiveHelper.Information.AppendLine("GetOldData-1-1 " + error.Message); } catch (Exception ex) { DataReceiveHelper.Information.AppendLine("GetOldData-1-2 " + ex.Message); } _logger.LogInformation("DataReceiver Service running at: {time}", DateTimeOffset.Now); DataReceiveHelper.LoopCount++; await Task.Delay(TimeSpan.FromSeconds(_random.Next(3, 7))); } } catch (TdException e) { var error = e.Error; DataReceiveHelper.Information.AppendLine("GetOldData-2-1 " + error.Message); } catch (Exception ex) { DataReceiveHelper.Information.AppendLine("GetOldData-2-2 " + ex.Message); } }