How to get the Topic icon/emoji from the icon_emoji_id?

104 Views Asked by At

I am writing an ASP.NET Core web app where I want to display a list of the channels and groups with topics, showing the configured icons/profile photos for each.

The channels are fine and I can use:

await WT.Client.DownloadProfilePhotoAsync(t.Channel, photoOutputStream, false, false);

to download the image for serving.

What I'm struggling with are the - what looks like - custom emojis for the forum topics.

I have tried the following, where t is an instance of my model class for passing to the controller:

var forums = await WT.Client.Channels_GetForumTopics((Channel)subChat.Value);
if (forums != null)
{
    
    t.Topics = forums;
    foreach (ForumTopic forum in forums.topics)
    {
        if (forum.icon_emoji_id != 0)
        {
            var result = await WT.Client.Messages_GetCustomEmojiDocuments(subChat.Value.ID);
            System.Console.WriteLine(result[0].ID);//debug breakpoint
        }
    }
}
else
{
    t.Topics = null;
}

The problem seems to be that Messages_GetCustomEmojiDocuments returns DocumentBase[] and not Document[] thus I can't get any further details about the topic emoji as described here.

Is this approach correct? Can you advise a way I can achieve what I need to do please?

1

There are 1 best solutions below

4
AliSalehi On

well WTelegramClient is heavily tight to pattern-matching, almost every API in telegram has some inheritance associated with its objects. in your case, when you get an DocumentBase it can either be TL.DocumentEmpty or TL.Document ( also included in the documents) so you can write:

var result = await client.Messages_GetCustomEmojiDocuments(1);

foreach (var documentBase in result)
{
    if (documentBase is Document document)
    {
        // document variable now contains every information you need
    }
   
}

you can put an else statement after the if and hard cast your documentBase to (DocumentEmpty) since there is no other possibilities or you can play it safe and do this:

switch (documentBase)
{
    case Document doc:
        break;
    case DocumentEmpty empty:
        break;
}

either way you have to access the inner data using pattern-matching