0

I'm using the WTelegramClient library in a .NET application to load all my Telegram dialogs and their recent messages. I want to get the avatar (profile picture) of each dialog as a remote Uri, not download files to disk. This is to display user/chat icons in a UI via HTTP without local storage.

Here’s the relevant part of my current code:

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    var user = await _client.LoginUserIfNeeded();
    Console.WriteLine($"Logged in as {user.username ?? user.first_name}");

    var dialogs = await _client.Messages_GetAllDialogs();

    var scope = _services.CreateScope();
    var store = scope.ServiceProvider.GetRequiredService<IMessageStore>();

    foreach (var chatBase in dialogs.chats.Values.OfType<Chat>())
        await store.AddOrUpdateChatAsync(chatBase);

    foreach (var usr in dialogs.users.Values.OfType<User>())
        await store.AddOrUpdateUserAsync(usr);

    var dialogsFiltered = dialogs.dialogs.OfType<Dialog>().ToList();

    foreach (var dlg in dialogsFiltered)
    {
        InputPeer peer = null!;

        if (dialogs.chats.TryGetValue(dlg.peer.ID, out var chatPeer))
            peer = chatPeer;
        else if (dialogs.users.TryGetValue(dlg.peer.ID, out var usrObj))
            peer = new InputPeerUser(dlg.peer.ID, usrObj.access_hash);

        if (peer is null) continue;

        var history = await _client.Messages_GetHistory(peer, limit: 50);
        foreach (var msg in history.Messages.OfType<Message>())
            await store.SaveOrUpdateMessageAsync(msg);

        // Here's where I want to get the dialog icon URI
    }

    await Task.Delay(Timeout.Infinite, stoppingToken);
}

Question: Is there a way to get a remote URL or public URI for a user's or chat's avatar (like https://t.me/i/userpic/...) using WTelegramClient, instead of downloading and serving them myself?

If this is not possible directly, is there a workaround to show user/chat avatars in a web UI without saving them locally?

I tried using this method to download profile pictures, but it saves them as local files instead of giving me any usable Uri:

private async Task<string?> GetAvatarUriAsync(IPeerInfo peer)
{
    try
    {
        var peerName = peer switch
        {
            User u => $"{u.id}",
            ChatBase c => $"{c.ID}",
            _ => null
        };

        if (peerName == null) return null;

        var avatarDir = Path.Combine(Directory.GetCurrentDirectory(), "avatars");
        Directory.CreateDirectory(avatarDir);

        var fileName = $"{peerName}.jpg";
        var fullPath = Path.Combine(avatarDir, fileName);

        var stream = new FileStream(fullPath, FileMode.Create);
        var avatarStream = await _client.DownloadProfilePhotoAsync(peer, stream, true);
        stream.Close();
        stream.Dispose();
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Avatar download failed: {ex.Message}");
    }
    return null;
}

1
  • Can you not use var stream = new MemoryStream(); and get the image using Image.FromStream(stream) Commented Jun 21 at 1:34

1 Answer 1

1

It's not possible.
As Telegram is a secure & encrypted messenger, all media & data stored in Telegram is inaccessible from normal web access.

The only exception is a public preview for some public channels via URLs like https://t.me/s/ProxyMTProto

With WTelegramClient, you can download media to memory (via MemoryStream) if you don't want to save them to disk

Sign up to request clarification or add additional context in comments.

4 Comments

Telegram client is open source, and op is not using browser environment, so as long as he has the token, it is possible, while I don't know if WTelegramClient provides the function.
did you read OP's question? he wants an URL to some Telegram media file (probably because he uses an UI control that accept picture URLs)
upvoted, didn't see your mention of MemoryStream before making my comment.
Thank you! I actually figured it out myself already. I just wanted to double-check and make sure I wasn’t missing anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.