How can I get all the messages first, then get only new ones #166
Replies: 4 comments 8 replies
-
Could you please clarify what's the particular problem? Do you have issues with receiving the messages (if so, then which ones)? Or with something being "interrupted"? |
Beta Was this translation helpful? Give feedback.
-
Once I need to get all the messages. Once a day I need to receive messages from the last messageId. I do this |
Beta Was this translation helpful? Give feedback.
-
if (message.Content is MessagePhoto messagePhoto)
{
try
{
var largestSize = messagePhoto.Photo.Sizes.OrderByDescending(s => s.Width).FirstOrDefault();
if (largestSize != null)
{
var downloadedFile = await _client.DownloadFileAsync(largestSize.Photo.Id, 1, 0, 0, true);
Thread.Sleep(300);
if (downloadedFile.Local.IsDownloadingCompleted)
{
localImagePath = downloadedFile.Local.Path;
}
}
}
catch (Exception) { }
} After 100 uploaded photos, the consumption of RAM increases literally with each uploaded picture. The program starts with 60mb and then increases to 500. |
Beta Was this translation helpful? Give feedback.
-
The debugger (if you ask about it) indicates a large memory consumption on TdLib.TdApi. public async Task ExtractContentFromMessagesAsync(Message message, long chatId)
{
string dataDir = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "content");
Directory.CreateDirectory(dataDir);
string contentFileName = $"{chatId}.json";
string contentFilePath = System.IO.Path.Combine(dataDir, contentFileName);
List<Content>? _contents = null;
if (File.Exists(contentFilePath))
{
try
{
string jsonContent = await File.ReadAllTextAsync(contentFilePath);
_contents = JsonConvert.DeserializeObject<List<Content>>(jsonContent);
}
catch (Exception ex)
{
Console.WriteLine($"Fault: {ex.Message}");
}
}
if (_contents == null)
{
_contents = new List<Content>();
}
bool contentExists = _contents.Any(c => c.MessageId == message.Id);
if (!contentExists)
{
string? messageText = null;
string localImagePath = string.Empty;
if (message.Content is MessagePhoto messagePhotoContent)
{
var captionText = messagePhotoContent.Caption?.Text;
if (!string.IsNullOrEmpty(captionText))
{
messageText = captionText;
}
}
if (message.Content is MessagePhoto messagePhoto)
{
try
{
var largestSize = messagePhoto.Photo.Sizes.OrderByDescending(s => s.Width).FirstOrDefault();
if (largestSize != null)
{
var downloadedFile = await _client.DownloadFileAsync(largestSize.Photo.Id, 1, 0, 0, true);
Thread.Sleep(300);
if (downloadedFile.Local.IsDownloadingCompleted)
{
localImagePath = downloadedFile.Local.Path;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Fault: {ex.Message}");
}
}
var newContent = new Content()
{
MessageId = message.Id,
Text = messageText,
ImagePaths = new List<string>(),
ImageViewed = false,
MediaAlbumId = message.MediaAlbumId
};
if (message.MediaAlbumId == 0)
{
newContent.ImagePaths.Add(localImagePath);
}
else
{
var albumContent = _contents.FirstOrDefault(c => c.MediaAlbumId == message.MediaAlbumId);
if (albumContent != null)
{
albumContent?.ImagePaths?.Add(localImagePath);
}
}
_contents.Add(newContent);
string jsonContentString = JsonConvert.SerializeObject(_contents, Formatting.Indented);
await File.WriteAllTextAsync(contentFilePath, jsonContentString);
}
} |
Beta Was this translation helpful? Give feedback.
-
Now I have a curved code for receiving messages, the first time I use
var result = await _client. ExecuteAsync(new TdApi.GetChatHistory
{
ChatId = chatId,
Limit = 100,
Offset = -1,
FromMessageId = lastMessageId
});
Then I use the rest to get the rest
var result = await _client.ExecuteAsync(new TdApi.SearchChatMessages
{
ChatId = chatId,
Limit = 100,
FromMessageId = lastMessageId,
});
But even so, I don't get everything I need, and often the receipt is interrupted and does not continue.
I tried only to get var result = await _client. ExecuteAsync(new TdApi.GetChatHistory
{
ChatId = chatId,
Limit = 100,
Offset = -1,
FromMessageId = lastMessageId
});
But with any settings I get only 1 message, sometimes it is more.
Beta Was this translation helpful? Give feedback.
All reactions