public FullClientDisposable(Telegram telegram)
 {
     if (telegram.IsFullClientConnected)
     {
         _client = telegram._fullClient;
         _isFullClient = true;
     }
     else
     {
         var transportConfig =
             new TcpClientTransportConfig(telegram._settings.NearestDcIp, telegram._settings.NearestDcPort);
         var client = new TelegramClient(transportConfig,
             new ConnectionConfig(telegram._settings.AuthKey, telegram._settings.Salt), AppInfo);
         var result = TelegramUtils.RunSynchronously(client.Connect());
         if (result != MTProtoConnectResult.Success)
         {
             throw new Exception("Failed to connect: " + result);
         }
         _client = client;
         _isFullClient = false;
     }
 }
Exemple #2
0
            private async Task<IUploadFile> FetchUserThumbnail(IUser user, Telegram telegramService, bool small)
            {
                var thumbnailLocation = TelegramUtils.GetUserPhotoLocation(user, small);

                if (thumbnailLocation == null)
                {
                    return null;
                }

                if (thumbnailLocation.DcId == telegramService.Settings.NearestDcId)
                {
                    using (var clientDisposable = new Telegram.FullClientDisposable(telegramService))
                    {
                        return await FetchFileBytes(clientDisposable.Client, thumbnailLocation);
                    }
                }
                else
                {
                    try
                    {
                        var telegramClient = telegramService.GetClient((int) thumbnailLocation.DcId);
                        return await FetchFileBytes(telegramClient, thumbnailLocation);
                    }
                    catch (Exception ex)
                    {
                        Utils.DebugPrint("Failed to obtain client from DC manager: " + ex);
                        return null;
                    }
                }
            }
Exemple #3
0
 public OptionalClientDisposable(Telegram telegram, TelegramClient optionalClient = null)
 {
     _optionalClient = optionalClient;
     if (_optionalClient == null)
     {
         _fullClient = new FullClientDisposable(telegram);
     }
 }
Exemple #4
0
            private async Task<IInputFile> UploadProfilePhoto(Telegram service, TelegramClient client,
                byte[] resizedPhoto)
            {
                var fileId = service.GenerateRandomId();
                const int chunkSize = 65536;
                var chunk = new byte[chunkSize];
                uint chunkNumber = 0;
                var offset = 0;
                using (var memoryStream = new MemoryStream(resizedPhoto))
                {
                    int bytesRead;
                    while ((bytesRead = memoryStream.Read(chunk, 0, chunk.Length)) > 0)
                    {
                        //RPC call
                        var uploaded =
                            await client.Methods.UploadSaveFilePartAsync(new UploadSaveFilePartArgs
                            {
                                Bytes = chunk,
                                FileId = fileId,
                                FilePart = chunkNumber
                            });

                        if (!uploaded)
                        {
                            return null;
                        }
                        chunkNumber++;
                        offset += bytesRead;
                    }

                    return new InputFile
                    {
                        Id = fileId,
                        Md5Checksum = "",
                        Name = service.GenerateRandomId() + ".jpeg",
                        Parts = chunkNumber
                    };
                }
            }
Exemple #5
0
            private async Task SetProfilePhoto(Telegram service, TelegramClient client, IInputFile inputFile)
            {
                var iPhoto = await client.Methods.PhotosUploadProfilePhotoAsync(new PhotosUploadProfilePhotoArgs
                {
                    Caption = "",
                    Crop = new InputPhotoCropAuto(),
                    File = inputFile,
                    GeoPoint = new InputGeoPointEmpty()
                });

                var photo = iPhoto as PhotosPhoto;
                if (photo != null)
                {
                    service.Dialogs.AddUsers(photo.Users);
                }

                var photoObj = photo.Photo as Photo;

                if (photoObj == null)
                {
                    return;
                }

                await client.Methods.PhotosUpdateProfilePhotoAsync(new PhotosUpdateProfilePhotoArgs
                {
                    Id = new InputPhoto
                    {
                        Id = photoObj.Id,
                    },
                    Crop = new InputPhotoCropAuto()
                });
            }
Exemple #6
0
 private async Task SetPrivacyOptions(Telegram telegramService, IInputPrivacyKey key, IInputPrivacyRule rule)
 {
     using (var client = new Telegram.FullClientDisposable(telegramService))
     {
         await client.Client.Methods.AccountSetPrivacyAsync(new AccountSetPrivacyArgs
         {
             Key = key,
             Rules = new List<IInputPrivacyRule>
             {
                 rule
             }
         });
     }
 }
Exemple #7
0
 private async Task SendGroupPrivacyChangeUpdate(Telegram telegramService, int selectedIndex)
 {
     IInputPrivacyKey key = new InputPrivacyKeyChatInvite();
     switch (selectedIndex)
     {
         case 0:
             await SetPrivacyOptions(telegramService, key, new InputPrivacyValueAllowAll());
             break;
         case 1:
             await SetPrivacyOptions(telegramService, key, new InputPrivacyValueAllowContacts());
             break;
     }
 }
Exemple #8
0
 private async Task SendLastSeenPrivacyChangeUpdate(Telegram telegramService, int selectedIndex)
 {
     IInputPrivacyKey key = new InputPrivacyKeyStatusTimestamp();
     switch (selectedIndex)
     {
         case 0:
             await SetPrivacyOptions(telegramService, key, new InputPrivacyValueAllowAll());
             break;
         case 1:
             await SetPrivacyOptions(telegramService, key, new InputPrivacyValueAllowContacts());
             break;
         case 2:
             await SetPrivacyOptions(telegramService, key, new InputPrivacyValueDisallowAll());
             break;
     }
 }