示例#1
0
 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;
     }
 }
示例#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;
                    }
                }
            }
示例#3
0
 public OptionalClientDisposable(Telegram telegram, TelegramClient optionalClient = null)
 {
     _optionalClient = optionalClient;
     if (_optionalClient == null)
     {
         _fullClient = new FullClientDisposable(telegram);
     }
 }
示例#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
                    };
                }
            }
示例#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()
                });
            }
示例#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
             }
         });
     }
 }
示例#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;
     }
 }
示例#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;
     }
 }