public void SendSticker(TLDocument22 document)
        {
            var chat = Chat as TLEncryptedChat;

            if (chat == null)
            {
                return;
            }

            var decryptedMediaExternalDocument = new TLDecryptedMessageMediaExternalDocument
            {
                Id         = document.Id,
                AccessHash = document.AccessHash,
                Date       = document.Date,
                MimeType   = document.MimeType,
                Size       = document.Size,
                Thumb      = document.Thumb,
                DCId       = document.DCId,
                Attributes = new TLVector <TLDocumentAttributeBase>
                {
                    new TLDocumentAttributeImageSize {
                        H = document.ImageSizeH, W = document.ImageSizeW
                    },
                    new TLDocumentAttributeFileName {
                        FileName = new TLString("sticker.webp")
                    },
                    new TLDocumentAttributeSticker()
                }
            };

            var decryptedTuple = GetDecryptedMessageAndObject(TLString.Empty, decryptedMediaExternalDocument, chat);

            BeginOnUIThread(() =>
            {
                Items.Insert(0, decryptedTuple.Item1);
                RaiseScrollToBottom();
                NotifyOfPropertyChange(() => DescriptionVisibility);

                SendEncrypted(chat, decryptedTuple.Item2, MTProtoService, CacheService);
            });
        }
        public void SendSticker(TLDocument22 document)
        {
            var chat = Chat as TLEncryptedChat;

            if (chat == null)
            {
                return;
            }

            TLDocumentAttributeSticker attributeSticker = null;
            var chat17 = Chat as TLEncryptedChat17;

            if (chat17 != null)
            {
                if (chat17.Layer.Value >= Constants.MinSecretChatWithCaptionsLayer)
                {
                    var attributeSticker29 = new TLDocumentAttributeSticker29 {
                        Alt = TLString.Empty, Stickerset = new TLInputStickerSetEmpty()
                    };
                    var attributes = document.Attributes;
                    if (attributes != null)
                    {
                        var attribute = document.Attributes.FirstOrDefault(x => x is TLDocumentAttributeSticker) as TLDocumentAttributeSticker29;
                        if (attribute != null)
                        {
                            attributeSticker29.Alt = attribute.Alt;

                            var stickerSet = attribute.Stickerset as TLInputStickerSetShortName;
                            if (stickerSet == null)
                            {
                                var stickerSetId = attribute.Stickerset as TLInputStickerSetId;
                                if (stickerSetId != null)
                                {
                                    var allStickers = StateService.GetAllStickers() as TLAllStickers29;
                                    if (allStickers != null)
                                    {
                                        var set = allStickers.Sets.FirstOrDefault(x => x.Id.Value == stickerSetId.Id.Value);
                                        if (set != null && !TLString.IsNullOrEmpty(set.ShortName))
                                        {
                                            attributeSticker29.Stickerset = new TLInputStickerSetShortName {
                                                ShortName = set.ShortName
                                            };
                                        }
                                    }
                                }
                            }
                        }
                    }
                    attributeSticker = attributeSticker29;
                }
                else if (chat17.Layer.Value >= Constants.MinSecretChatWithStickersLayer)
                {
                    attributeSticker = new TLDocumentAttributeSticker();
                }
            }

            var stickerAttributes = new TLVector <TLDocumentAttributeBase>
            {
                new TLDocumentAttributeImageSize {
                    H = document.ImageSizeH, W = document.ImageSizeW
                },
                new TLDocumentAttributeFileName {
                    FileName = new TLString("sticker.webp")
                }
            };

            if (attributeSticker != null)
            {
                stickerAttributes.Add(attributeSticker);
            }
            var decryptedMediaExternalDocument = new TLDecryptedMessageMediaExternalDocument
            {
                Id         = document.Id,
                AccessHash = document.AccessHash,
                Date       = document.Date,
                MimeType   = document.MimeType,
                Size       = document.Size,
                Thumb      = document.Thumb,
                DCId       = document.DCId,
                Attributes = stickerAttributes
            };

            var decryptedTuple = GetDecryptedMessageAndObject(TLString.Empty, decryptedMediaExternalDocument, chat);

            BeginOnUIThread(() =>
            {
                InsertSendingMessage(decryptedTuple.Item1);
                RaiseScrollToBottom();
                NotifyOfPropertyChange(() => DescriptionVisibility);
                Text = string.Empty;

                BeginOnThreadPool(() =>
                {
                    SendEncrypted(chat, decryptedTuple.Item2, MTProtoService, CacheService);
                });
            });
        }
        private static ImageSource ReturnOrEnqueueSticker(TLDecryptedMessageMediaExternalDocument document, TLDecryptedMessage owner)
        {
            if (document == null)
            {
                return(null);
            }

            var documentLocalFileName = document.GetFileName();

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.FileExists(documentLocalFileName))
                {
                    // 1. download full size
                    IoC.Get <IDocumentFileManager>().DownloadFileAsync(document.FileName, document.DCId, document.ToInputFileLocation(), owner, document.Size, progress => { });

                    // 2. download preview
                    var thumbCachedSize = document.Thumb as TLPhotoCachedSize;
                    if (thumbCachedSize != null)
                    {
                        var fileName = "cached" + document.GetFileName();
                        var buffer   = thumbCachedSize.Bytes.Data;
                        if (buffer == null)
                        {
                            return(null);
                        }

                        return(DecodeWebPImage(fileName, buffer, () => { }));
                    }

                    var thumbPhotoSize = document.Thumb as TLPhotoSize;
                    if (thumbPhotoSize != null)
                    {
                        var location = thumbPhotoSize.Location as TLFileLocation;
                        if (location != null)
                        {
                            return(ReturnOrEnqueueStickerPreview(location, owner, thumbPhotoSize.Size));
                        }
                    }
                }
                else
                {
                    if (document.Size.Value > 0 &&
                        document.Size.Value < Telegram.Api.Constants.StickerMaxSize)
                    {
                        byte[] buffer;
                        using (var file = store.OpenFile(documentLocalFileName, FileMode.Open))
                        {
                            buffer = new byte[file.Length];
                            file.Read(buffer, 0, buffer.Length);
                        }

                        return(DecodeWebPImage(documentLocalFileName, buffer,
                                               () =>
                        {
                            using (var localStore = IsolatedStorageFile.GetUserStoreForApplication())
                            {
                                localStore.DeleteFile(documentLocalFileName);
                            }
                        }));
                    }
                }
            }

            return(null);
        }