public static double GetGameDimension(TLGame game, bool isWidth)
        {
            const double width = 311.0 - 12.0;

            if (isWidth)
            {
                return(width);
            }

            var photo = game.Photo as TLPhoto;

            if (photo == null)
            {
                return(double.NaN);
            }

            TLPhotoSize size  = null;
            var         sizes = photo.Sizes.OfType <TLPhotoSize>();

            foreach (var photoSize in sizes)
            {
                if (size == null ||
                    Math.Abs(width - size.W.Value) > Math.Abs(width - photoSize.W.Value))
                {
                    size = photoSize;
                }
            }

            if (size != null)
            {
                return(width / size.W.Value * size.H.Value); //* 0.75;
            }

            return(double.NaN);
        }
Ejemplo n.º 2
0
        public WebViewModel(IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService)
        {
            _stateService      = stateService;
            _navigationService = navigationService;
            _mtProtoService    = mtProtoService;

            _sharedContact             = stateService.SharedContact as TLUser;
            stateService.SharedContact = null;

            var userName = _sharedContact as IUserName;

            if (userName != null && !TLString.IsNullOrEmpty(userName.UserName))
            {
                Subtitle = "@" + userName.UserName;
            }

            _botCallbackAnswer             = stateService.BotCallbackAnswer;
            stateService.BotCallbackAnswer = null;

            _forwardMessages             = stateService.ForwardMessages;
            stateService.ForwardMessages = null;

            var botCallbackAnswer54 = _botCallbackAnswer as TLBotCallbackAnswer54;

            if (botCallbackAnswer54 != null)
            {
                Url = new Uri(botCallbackAnswer54.Url.ToString(), UriKind.RelativeOrAbsolute);
#if DEBUG
                Clipboard.SetText(botCallbackAnswer54.Url.ToString());
                MessageBox.Show(botCallbackAnswer54.Url.ToString());
#endif
            }

            _game             = stateService.Game;
            stateService.Game = null;

            if (_game != null)
            {
                GameTitle = _game.Title.ToString();
            }

            _inputPeer             = stateService.InputPeer;
            stateService.InputPeer = null;

            _timer = new Timer(OnTimerTick);
        }
Ejemplo n.º 3
0
        private void ProcessBotInlineResult(TLMessage45 message, TLBotInlineResultBase resultBase, TLInt botId)
        {
            message.InlineBotResultId      = resultBase.Id;
            message.InlineBotResultQueryId = resultBase.QueryId;
            message.ViaBotId = botId;

            var botInlineMessageMediaVenue = resultBase.SendMessage as TLBotInlineMessageMediaVenue;
            var botInlineMessageMediaGeo   = resultBase.SendMessage as TLBotInlineMessageMediaGeo;

            if (botInlineMessageMediaVenue != null)
            {
                message._media = new TLMessageMediaVenue72
                {
                    Title     = botInlineMessageMediaVenue.Title,
                    Address   = botInlineMessageMediaVenue.Address,
                    Provider  = botInlineMessageMediaVenue.Provider,
                    VenueId   = botInlineMessageMediaVenue.VenueId,
                    VenueType = TLString.Empty,
                    Geo       = botInlineMessageMediaVenue.Geo
                };
            }
            else if (botInlineMessageMediaGeo != null)
            {
                message._media = new TLMessageMediaGeo {
                    Geo = botInlineMessageMediaGeo.Geo
                };
            }

            var botInlineMessageMediaContact = resultBase.SendMessage as TLBotInlineMessageMediaContact;

            if (botInlineMessageMediaContact != null)
            {
                message._media = new TLMessageMediaContact82
                {
                    PhoneNumber = botInlineMessageMediaContact.PhoneNumber,
                    FirstName   = botInlineMessageMediaContact.FirstName,
                    LastName    = botInlineMessageMediaContact.LastName,
                    UserId      = new TLInt(0),
                    VCard       = TLString.Empty
                };
            }

            var mediaResult = resultBase as TLBotInlineMediaResult;

            if (mediaResult != null)
            {
                if (TLString.Equals(mediaResult.Type, new TLString("voice"), StringComparison.OrdinalIgnoreCase) &&
                    mediaResult.Document != null)
                {
                    message._media = new TLMessageMediaDocument75 {
                        Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty, NotListened = !(With is TLChannel)
                    };

                    message.NotListened = !(With is TLChannel);
                }
                else if (TLString.Equals(mediaResult.Type, new TLString("audio"), StringComparison.OrdinalIgnoreCase) &&
                         mediaResult.Document != null)
                {
                    message._media = new TLMessageMediaDocument75 {
                        Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty
                    };
                }
                else if (TLString.Equals(mediaResult.Type, new TLString("sticker"), StringComparison.OrdinalIgnoreCase) &&
                         mediaResult.Document != null)
                {
                    message._media = new TLMessageMediaDocument75 {
                        Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty
                    };
                }
                else if (TLString.Equals(mediaResult.Type, new TLString("file"), StringComparison.OrdinalIgnoreCase) &&
                         mediaResult.Document != null)
                {
                    message._media = new TLMessageMediaDocument75 {
                        Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty
                    };
                }
                else if (TLString.Equals(mediaResult.Type, new TLString("gif"), StringComparison.OrdinalIgnoreCase) &&
                         mediaResult.Document != null)
                {
                    message._media = new TLMessageMediaDocument75 {
                        Flags = new TLInt(0), Document = mediaResult.Document, Caption = TLString.Empty
                    };
                }
                else if (TLString.Equals(mediaResult.Type, new TLString("photo"), StringComparison.OrdinalIgnoreCase) &&
                         mediaResult.Photo != null)
                {
                    message._media = new TLMessageMediaPhoto75 {
                        Flags = new TLInt(0), Photo = mediaResult.Photo, Caption = TLString.Empty
                    };
                }
                else if (TLString.Equals(mediaResult.Type, new TLString("game"), StringComparison.OrdinalIgnoreCase))
                {
                    var game = new TLGame
                    {
                        Flags       = new TLInt(0),
                        Id          = new TLLong(0),
                        AccessHash  = new TLLong(0),
                        ShortName   = mediaResult.Id,
                        Title       = mediaResult.Title ?? TLString.Empty,
                        Description = mediaResult.Description ?? TLString.Empty,
                        Photo       = mediaResult.Photo ?? new TLPhotoEmpty {
                            Id = new TLLong(0)
                        },
                        Document = mediaResult.Document
                    };

                    message._media = new TLMessageMediaGame {
                        Game = game, SourceMessage = message
                    };
                }
            }

            var result = resultBase as TLBotInlineResult;

            if (result != null)
            {
                var isVoice = TLString.Equals(result.Type, new TLString("voice"), StringComparison.OrdinalIgnoreCase);
                var isAudio = TLString.Equals(result.Type, new TLString("audio"), StringComparison.OrdinalIgnoreCase);
                var isFile  = TLString.Equals(result.Type, new TLString("file"), StringComparison.OrdinalIgnoreCase);

                if (isFile ||
                    isAudio ||
                    isVoice)
                {
                    var document = result.Document as TLDocument22;
                    if (document == null)
                    {
                        string fileName = null;
                        if (result.ContentUrl != null)
                        {
                            var fileUri = new Uri(result.ContentUrlString);
                            try
                            {
                                fileName = Path.GetFileName(fileUri.LocalPath);
                            }
                            catch (Exception ex)
                            {
                            }

                            if (fileName == null)
                            {
                                fileName = "file.ext";
                            }
                        }

                        document = new TLDocument54
                        {
                            Id         = new TLLong(0),
                            AccessHash = new TLLong(0),
                            Date       = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                            FileName   = new TLString(fileName),
                            MimeType   = result.ContentType ?? TLString.Empty,
                            Size       = new TLInt(0),
                            Thumb      = new TLPhotoSizeEmpty {
                                Type = TLString.Empty
                            },
                            DCId    = new TLInt(0),
                            Version = new TLInt(0)
                        };

                        if (isVoice || isAudio)
                        {
                            var documentAttributeAudio = new TLDocumentAttributeAudio46
                            {
                                Duration  = result.Duration ?? new TLInt(0),
                                Title     = result.Title ?? TLString.Empty,
                                Performer = null,
                                Voice     = isVoice
                            };
                            document.Attributes.Add(documentAttributeAudio);
                        }

                        //message._status = MessageStatus.Failed;
                    }
                    var mediaDocument = new TLMessageMediaDocument75 {
                        Flags = new TLInt(0), Document = document, Caption = TLString.Empty
                    };

                    message._media = mediaDocument;

                    mediaDocument.NotListened = isVoice && !(With is TLChannel);
                    message.NotListened       = isVoice && !(With is TLChannel);
                }
                else if (TLString.Equals(result.Type, new TLString("gif"), StringComparison.OrdinalIgnoreCase))
                {
                    var document = result.Document;
                    if (document != null)
                    {
                        var mediaDocument = new TLMessageMediaDocument75 {
                            Flags = new TLInt(0), Document = document, Caption = TLString.Empty
                        };

                        message._media = mediaDocument;
                    }
                }
                else if (TLString.Equals(result.Type, new TLString("photo"), StringComparison.OrdinalIgnoreCase))
                {
                    Telegram.Api.Helpers.Execute.ShowDebugMessage(string.Format("w={0} h={1}\nthumb_url={2}\ncontent_url={3}", result.W, result.H, result.ThumbUrl, result.ContentUrl));

                    var location = new TLFileLocation {
                        DCId = new TLInt(1), VolumeId = TLLong.Random(), LocalId = TLInt.Random(), Secret = TLLong.Random()
                    };

                    var cachedSize = new TLPhotoCachedSize
                    {
                        Type     = new TLString("s"),
                        W        = result.W ?? new TLInt(90),
                        H        = result.H ?? new TLInt(90),
                        Location = location,
                        Bytes    = TLString.Empty,
                        TempUrl  = result.ThumbUrlString ?? result.ContentUrlString
                                   //Size = new TLInt(0)
                    };

                    var size = new TLPhotoSize
                    {
                        Type     = new TLString("m"),
                        W        = result.W ?? new TLInt(90),
                        H        = result.H ?? new TLInt(90),
                        Location = location,
                        TempUrl  = result.ContentUrlString,
                        Size     = new TLInt(0)
                    };

                    if (!string.IsNullOrEmpty(result.ThumbUrlString))
                    {
                        //ServicePointManager.ServerCertificateValidationCallback += new

                        var webClient = new WebClient();
                        webClient.OpenReadAsync(new Uri(result.ThumbUrlString, UriKind.Absolute));
                        webClient.OpenReadCompleted += (sender, args) =>
                        {
                            if (args.Cancelled)
                            {
                                return;
                            }
                            if (args.Error != null)
                            {
                                return;
                            }

                            var fileName = String.Format("{0}_{1}_{2}.jpg",
                                                         location.VolumeId,
                                                         location.LocalId,
                                                         location.Secret);

                            using (var stream = args.Result)
                            {
                                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                                {
                                    if (store.FileExists(fileName))
                                    {
                                        return;
                                    }
                                    using (var file = store.OpenFile(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
                                    {
                                        const int BUFFER_SIZE = 128 * 1024;
                                        var       buf         = new byte[BUFFER_SIZE];

                                        var bytesread = 0;
                                        while ((bytesread = stream.Read(buf, 0, BUFFER_SIZE)) > 0)
                                        {
                                            var position = stream.Position;
                                            stream.Position = position - 10;
                                            var tempBuffer = new byte[10];
                                            var resultOk   = stream.Read(tempBuffer, 0, tempBuffer.Length);
                                            file.Write(buf, 0, bytesread);
                                        }
                                    }
                                }
                            }

                            if (!string.IsNullOrEmpty(result.ContentUrlString))
                            {
                                webClient.OpenReadAsync(new Uri(result.ContentUrlString, UriKind.Absolute));
                                webClient.OpenReadCompleted += (sender2, args2) =>
                                {
                                    if (args2.Cancelled)
                                    {
                                        return;
                                    }
                                    if (args2.Error != null)
                                    {
                                        return;
                                    }

                                    using (var stream = args2.Result)
                                    {
                                        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                                        {
                                            if (store.FileExists(fileName))
                                            {
                                                return;
                                            }
                                            using (var file = store.OpenFile(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
                                            {
                                                const int BUFFER_SIZE = 128 * 1024;
                                                var       buf         = new byte[BUFFER_SIZE];

                                                int bytesread = 0;
                                                while ((bytesread = stream.Read(buf, 0, BUFFER_SIZE)) > 0)
                                                {
                                                    file.Write(buf, 0, bytesread);
                                                }
                                            }
                                        }
                                    }
                                };
                            }
                        };
                    }

                    var photo = new TLPhoto56
                    {
                        Flags      = new TLInt(0),
                        Id         = TLLong.Random(),
                        AccessHash = TLLong.Random(),
                        Date       = TLUtils.DateToUniversalTimeTLInt(MTProtoService.ClientTicksDelta, DateTime.Now),
                        Sizes      = new TLVector <TLPhotoSizeBase> {
                            cachedSize, size
                        }
                    };

                    var mediaPhoto = new TLMessageMediaPhoto75 {
                        Flags = new TLInt(0), Photo = photo, Caption = TLString.Empty
                    };

                    message._media = mediaPhoto;
                }
            }

            var messageText = resultBase.SendMessage as TLBotInlineMessageText;

            if (messageText != null)
            {
                message.Message  = messageText.Message;
                message.Entities = messageText.Entities;
                if (messageText.NoWebpage)
                {
                }
            }

            var mediaAuto = resultBase.SendMessage as TLBotInlineMessageMediaAuto75;

            if (mediaAuto != null)
            {
                message.Message  = mediaAuto.Caption;
                message.Entities = mediaAuto.Entities;
            }

            if (resultBase.SendMessage != null &&
                resultBase.SendMessage.ReplyMarkup != null)
            {
                message.ReplyMarkup = resultBase.SendMessage.ReplyMarkup;
            }
        }