Ejemplo n.º 1
0
        public virtual async Task DownloadFileFromWrongLocationTest()
        {
            TelegramClient client = this.NewClient();

            TelegramAuthModel authModel = new TelegramAuthModel()
            {
                ApiId   = this.ApiId,
                ApiHash = this.ApiHash
            };
            await client.AuthenticateAsync(authModel);

            TeleSharp.TL.Contacts.TLContacts result = await client.GetContactsAsync();

            TLUser user = result.Users
                          .OfType <TLUser>()
                          .FirstOrDefault(x => x.Id == 5880094);

            TLUserProfilePhoto photo         = ((TLUserProfilePhoto)user.Photo);
            TLFileLocation     photoLocation = (TLFileLocation)photo.PhotoBig;

            TeleSharp.TL.Upload.TLFile resFile = await client.GetFile(new TLInputFileLocation()
            {
                LocalId  = photoLocation.LocalId,
                Secret   = photoLocation.Secret,
                VolumeId = photoLocation.VolumeId
            }, 1024);

            TLAbsDialogs res = await client.GetUserDialogsAsync();

            Assert.IsTrue(resFile.Bytes.Length > 0);
        }
Ejemplo n.º 2
0
        public virtual async Task DownloadFileFromContactTest()
        {
            TelegramClient client = this.NewClient();

            TelegramAuthModel authModel = new TelegramAuthModel()
            {
                ApiId   = this.ApiId,
                ApiHash = this.ApiHash
            };
            await client.AuthenticateAsync(authModel);

            TeleSharp.TL.Contacts.TLContacts result = await client.GetContactsAsync();

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

            TLInputPeerUser inputPeer = new TLInputPeerUser()
            {
                UserId = user.Id
            };
            TLMessagesSlice res = await client.SendRequestAsync <TLMessagesSlice>(new TLRequestGetHistory()
            {
                Peer = inputPeer
            });

            TLDocument document = res.Messages
                                  .OfType <TLMessage>()
                                  .Where(m => m.Media != null)
                                  .Select(m => m.Media)
                                  .OfType <TLMessageMediaDocument>()
                                  .Select(md => md.Document)
                                  .OfType <TLDocument>()
                                  .First();

            TeleSharp.TL.Upload.TLFile resFile = await client.GetFile(
                new TLInputDocumentFileLocation()
            {
                AccessHash = document.AccessHash,
                Id         = document.Id,
                Version    = document.Version
            },
                document.Size);

            Assert.IsTrue(resFile.Bytes.Length > 0);
        }
Ejemplo n.º 3
0
 public async void sendMessage(TLMessage msg, string channelName, TeleSharp.TL.Upload.TLFile tfile)
 {
     using (MemoryStream memStream = new MemoryStream())
     {
         Random random = new Random();
         var    ra     = random.Next(1000);
         memStream.Write(tfile.Bytes, 0, tfile.Bytes.Length);
         using (var s = System.Drawing.Image.FromStream(memStream))
         {
             s.Save(ra + "1.jpg");
             foreach (var item in userId)
             {
                 using (FileStream stream = new FileStream(ra + "1.jpg", FileMode.Open))
                 {
                     Message x = await Bot.SendPhotoAsync(item, new FileToSend("123.jpg", stream));
                 }
             }
         }
     }
 }
Ejemplo n.º 4
0
        private async void obtenerFoto()
        {
            var result = await MainWindow.client.GetContactsAsync();

            var user = result.Users
                       .Where(x => x.GetType() == typeof(TLUser))
                       .Cast <TLUser>()
                       .FirstOrDefault(x => x.Phone == "34" + u.Telefono);
            bool mirarfoto = true;

            if (user == null)
            {
                //await MainWindow.client.ConnectAsync();
                var phoneContact = new TLInputPhoneContact()
                {
                    Phone = "34" + u.Telefono, FirstName = u.Nombre, LastName = u.Apellidos
                };
                var contacts = new List <TLInputPhoneContact>()
                {
                    phoneContact
                };
                var req = new TLRequestImportContacts()
                {
                    Contacts = new TLVector <TLInputPhoneContact>(contacts)
                };
                var rrr = await MainWindow.client.SendRequestAsync <TLImportedContacts>(req);

                if (rrr.Imported.Count == 1)
                {
                    user = result.Users
                           .Where(x => x.GetType() == typeof(TLUser))
                           .Cast <TLUser>()
                           .FirstOrDefault(x => x.Phone == "34" + u.Telefono);
                }
                else
                {
                    mirarfoto = false;
                }
            }
            if (mirarfoto && user != null)
            {
                TLAbsUserProfilePhoto photo = user.Photo;
                TLUserProfilePhoto    up    = (TLUserProfilePhoto)photo;
                if (up != null && up.PhotoBig != null)
                {
                    TLFileLocation         tf   = (TLFileLocation)up.PhotoBig;
                    TLAbsInputFileLocation aifl = new TLInputFileLocation()
                    {
                        LocalId  = tf.LocalId,
                        Secret   = tf.Secret,
                        VolumeId = tf.VolumeId
                    };
                    TeleSharp.TL.Upload.TLFile buffer = await MainWindow.client.GetFile(aifl, 1024 * 1024);

                    if (buffer.Bytes != null)
                    {
                        u.Photo = buffer.Bytes;
                        u.cambiarFoto();
                    }
                }
            }
        }