sochix / TLSharp

Telegram client library implemented in C#
1k stars 379 forks source link

How to get Photo message in tlsharp? #1014

Open theSadra opened 3 years ago

theSadra commented 3 years ago

Hi everyone, I'm using Tl sharp to create a channel message receiver. and I have a problem about receiving and saving Photo Files. I checked out official tlsharp code sample to get document files bud I want to save photo files using client.get(); method.

this is the code sample for ducument files:

How can I use this code for getting and saving Photo files?


          var client = NewClient();

            await client.ConnectAsync();

            var result = await client.GetContactsAsync();

            var user = result.Users
                .OfType<TLUser>()
                .FirstOrDefault(x => x.Phone == NumberToSendMessage);

            var inputPeer = new TLInputPeerUser() { UserId = user.Id };
            var res = await client.SendRequestAsync<TLMessagesSlice>(new TLRequestGetHistory() { Peer = inputPeer });
            var document = res.Messages
                .OfType<TLMessage>()
                .Where(m => m.Media != null)
                .Select(m => m.Media)
                .OfType<TLMessageMediaDocument>()
                .Select(md => md.Document)
                .OfType<TLDocument>()
                .First();

            var resFile = await client.GetFile(
                new TLInputDocumentFileLocation()
                {
                    AccessHash = document.AccessHash,
                    Id = document.Id,
                    Version = document.Version
                },
                 document.Size);
rocket-builder commented 3 years ago

i find such code example, but it throw Flood Prevention exception var mediaPhoto = message.Media as TLMessageMediaPhoto;

                    var photo = mediaPhoto.Photo as TLPhoto;

                    TLPhotoSize photoSize = photo.Sizes.ToList().OfType<TLPhotoSize>().Last();
                    TLFileLocation location = (TLFileLocation)photoSize.Location;

                    TLAbsInputFileLocation inputLocation = new TLInputFileLocation()
                    {
                        LocalId = location.LocalId,
                        Secret = location.Secret,
                        VolumeId = location.VolumeId
                    };
                    TLInputFileLocation inputFileLocation = inputLocation as TLInputFileLocation;

                    var photoFile = await _client.GetFile(inputFileLocation, 1024 * 512);

                    does anyone know how to avoid this error?
SmaGMan commented 2 years ago

Check this thread https://github.com/sochix/TLSharp/issues/1019