Ejemplo n.º 1
0
        private static async Task <byte[]> GetFile(TelegramClient client, TLInputFileLocation file)
        {
            int filePart = 512 * 1024;
            int offset   = 0;

            using (var mem = new MemoryStream())
            {
                while (true)
                {
                    if (!client.IsConnected)
                    {
                        await client.ConnectAsync(true);
                    }
                    var resFile = await client.GetFile(
                        file,
                        filePart, offset);

                    mem.Write(resFile.Bytes, 0, resFile.Bytes.Length);
                    offset += filePart;
                    var readCount = resFile.Bytes.Length;

#if DEBUG
                    Console.Write($" ... read {readCount} of {filePart} .");
#endif
                    if (readCount < filePart)
                    {
                        break;
                    }
                }
                return(mem.ToArray());
            }
        }
Ejemplo n.º 2
0
        public TLFile GetFile(TLFileLocation fileLocation, int iSize)
        {
            TLAbsInputFileLocation inputFileLocation = new TLInputFileLocation()
            {
                volume_id = fileLocation.volume_id,
                local_id  = fileLocation.local_id,
                secret    = fileLocation.secret,
            };
            TLFile file = (TLFile)AsyncHelpers.RunSync <TLFile>(() => m_client.GetFile(inputFileLocation, iSize));

            if (file.bytes.Length != iSize)
            {
                throw new TLCoreException("The file need to be downloaded in parts");
            }
            return(file);
        }
Ejemplo n.º 3
0
        private static async void OnMyTimedEvent(object source, ElapsedEventArgs e)
        {
            try
            {
                Console.WriteLine("On timer event");
                DateTime nowDateTime = DateTime.Now.ToLocalTime();
                // Check that we are well connected
                if (Client != null && Client.IsConnected && UserId != 0)
                {
                    if (ChannelId != null && ChannelId.Count > 0)
                    {
                        TLAbsDialogs = await Client.GetUserDialogsAsync();

                        foreach (TLAbsMessage tLAbsMessage in ((TLDialogs)TLAbsDialogs).Messages.Where(x => x is TLMessage message && TimeUnixTOWindows(message.Date, true) >= nowDateTime.AddMilliseconds(-(TimerIntervalInMs - 1))))
                        {
                            ((TLMessage)tLAbsMessage).Message = CalculOffset(((TLMessage)tLAbsMessage).Message);
                            if (((TLMessage)tLAbsMessage).ToId is TLPeerUser tLPeerUser)
                            {
                                // Personal Chat Do Not Forward!
                            }
                            else if (((TLMessage)tLAbsMessage).ToId is TLPeerChannel channel0 && ((TLMessage)tLAbsMessage).ReplyToMsgId != null)
                            {
                                int crtChannelId = channel0.ChannelId;
                                if (crtChannelId != MyChanId && ChannelId.ContainsKey(crtChannelId))
                                {
                                    Console.WriteLine("ReplyChannelId " + ((TLMessage)tLAbsMessage).ReplyToMsgId);
                                    await ReplyMessage((TLMessage)tLAbsMessage);
                                }
                            }
                            else if (((TLMessage)tLAbsMessage).ToId is TLPeerChat chat && ((TLMessage)tLAbsMessage).ReplyToMsgId != null)
                            {
                                Console.WriteLine("ReplyChatId " + ((TLMessage)tLAbsMessage).ReplyToMsgId);
                                await ReplyMessage((TLMessage)tLAbsMessage);
                            }
                            else if (((TLMessage)tLAbsMessage).ToId is TLPeerChannel channel && ((TLMessage)tLAbsMessage).ReplyToMsgId == null)
                            {
                                int crtChannelId = channel.ChannelId;
                                if (crtChannelId != MyChanId && ChannelId.ContainsKey(crtChannelId))
                                {
                                    Console.WriteLine("New Message Channel " + ChannelId[crtChannelId][0] + " \n" + ((TLMessage)tLAbsMessage).Message);
                                    if (ChannelId.ContainsKey(crtChannelId))
                                    {
                                        if (((TLMessage)tLAbsMessage).Message != "")
                                        {
                                            if (((TLMessage)tLAbsMessage).Message.ToLower().StartsWith("tp") || ((TLMessage)tLAbsMessage).Message.ToLower().StartsWith("sl"))
                                            {
                                                TLChannelMessages historyFromSourceCanal = (TLChannelMessages)await Client.GetHistoryAsync(new TLInputPeerChannel()
                                                {
                                                    ChannelId = channel.ChannelId, AccessHash = (long)ChannelId[channel.ChannelId][1]
                                                });

                                                List <TLAbsMessage> tLMessageList        = historyFromSourceCanal.Messages.ToList().Where(x => x is TLMessage tL).ToList();
                                                List <TLMessage>    orderedtLMessageList = tLMessageList.Cast <TLMessage>().OrderByDescending(x => x.Id).ToList();
                                                string newMessage = CalculOffset(orderedtLMessageList[1].Message + "\n" + ((TLMessage)tLAbsMessage).Message);
                                                if (orderedtLMessageList[1].Message.ToLower().Contains("sell") && !orderedtLMessageList[1].Message.ToLower().Contains("sl"))
                                                {
                                                    await Client.SendMessageAsync(new TLInputPeerChannel()
                                                    {
                                                        ChannelId = MyChanId, AccessHash = AccessHash
                                                    }, newMessage);
                                                }
                                                else if (orderedtLMessageList[1].Message.ToLower().Contains("vente") && !orderedtLMessageList[1].Message.ToLower().Contains("sl"))
                                                {
                                                    await Client.SendMessageAsync(new TLInputPeerChannel()
                                                    {
                                                        ChannelId = MyChanId, AccessHash = AccessHash
                                                    }, newMessage);
                                                }
                                                else if (orderedtLMessageList[1].Message.ToLower().Contains("buy") && !orderedtLMessageList[1].Message.ToLower().Contains("sl"))
                                                {
                                                    await Client.SendMessageAsync(new TLInputPeerChannel()
                                                    {
                                                        ChannelId = MyChanId, AccessHash = AccessHash
                                                    }, newMessage);
                                                }
                                                else if (orderedtLMessageList[1].Message.ToLower().Contains("achat") && !orderedtLMessageList[1].Message.ToLower().Contains("sl"))
                                                {
                                                    await Client.SendMessageAsync(new TLInputPeerChannel()
                                                    {
                                                        ChannelId = MyChanId, AccessHash = AccessHash
                                                    }, newMessage);
                                                }
                                            }
                                            else
                                            {
                                                await Client.SendMessageAsync(new TLInputPeerChannel()
                                                {
                                                    ChannelId = MyChanId, AccessHash = AccessHash
                                                }, ((TLMessage)tLAbsMessage).Message);
                                            }
                                        }
                                        else if (((TLMessage)tLAbsMessage).Media != null)
                                        {
                                            if (((TLMessage)tLAbsMessage).Media.GetType().ToString() == "TeleSharp.TL.TLMessageMediaPhoto")
                                            {
                                                TLMessageMediaPhoto    tLMessageMediaPhoto    = (TLMessageMediaPhoto)((TLMessage)tLAbsMessage).Media;
                                                TLPhoto                tLPhoto                = (TLPhoto)tLMessageMediaPhoto.Photo;
                                                TLPhotoSize            tLPhotoSize            = tLPhoto.Sizes.ToList().OfType <TLPhotoSize>().Last();
                                                TLFileLocation         tLFileLocation         = (TLFileLocation)tLPhotoSize.Location;
                                                TLAbsInputFileLocation tLAbsInputFileLocation = new TLInputFileLocation()
                                                {
                                                    LocalId  = tLFileLocation.LocalId,
                                                    Secret   = tLFileLocation.Secret,
                                                    VolumeId = tLFileLocation.VolumeId
                                                };
                                                TLInputFileLocation TLInputFileLocation = tLAbsInputFileLocation as TLInputFileLocation;
                                                TLFile buffer = await Client.GetFile(TLInputFileLocation, 1024 * 512);

                                                TLInputFile fileResult = (TLInputFile)await UploadHelper.UploadFile(Client, "", new StreamReader(new MemoryStream(buffer.Bytes)));

                                                await Client.SendUploadedPhoto(new TLInputPeerChannel()
                                                {
                                                    ChannelId = MyChanId, AccessHash = AccessHash
                                                }, fileResult, tLMessageMediaPhoto.Caption);
                                            }
                                            else if (((TLMessage)tLAbsMessage).Media.GetType().ToString() == "TeleSharp.TL.TLMessageMediaDocument")
                                            {
                                                TLMessageMediaDocument            tLMessageMediaDocument      = (TLMessageMediaDocument)((TLMessage)tLAbsMessage).Media;
                                                TLDocument                        tLDocument                  = (TLDocument)tLMessageMediaDocument.Document;
                                                TLVector <TLAbsDocumentAttribute> tLAbsDocumentAttributes     = tLDocument.Attributes;
                                                TLInputDocumentFileLocation       tLInputDocumentFileLocation = new TLInputDocumentFileLocation()
                                                {
                                                    AccessHash = tLDocument.AccessHash,
                                                    Id         = tLDocument.Id,
                                                    Version    = tLDocument.Version,
                                                };
                                                TLFile buffer = await Client.GetFile(tLInputDocumentFileLocation, 1024 * 512);

                                                TLInputFile fileResult = (TLInputFile)await UploadHelper.UploadFile(Client, ((TLDocumentAttributeFilename)tLAbsDocumentAttributes[0]).FileName, new StreamReader(new MemoryStream(buffer.Bytes)));

                                                await Client.SendUploadedDocument(new TLInputPeerChannel()
                                                {
                                                    ChannelId = MyChanId, AccessHash = AccessHash
                                                }, fileResult, tLMessageMediaDocument.Caption, tLDocument.MimeType, tLAbsDocumentAttributes);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
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();
                    }
                }
            }
        }