Ejemplo n.º 1
0
        static async Task DownloadLastMovieFromSavedMessagesExample(TelegramClient tg)
        {
            var fullUserInfo = await tg.Call(new GetFullUser(new InputUser.SelfTag()));

            var userInfo = fullUserInfo.User.AsTag().AssertSome();

            var chatPeer = (InputPeer) new InputPeer.UserTag(
                userId: userInfo.Id,
                accessHash: userInfo.AccessHash.AssertSome()
                );
            const int batchLimit = 100;

            async Task <IEnumerable <Document.Tag> > GetHistory(int offset = 0)
            {
                var resp = await tg.Call(new GetHistory(
                                             peer : chatPeer,
                                             addOffset : offset,
                                             limit : batchLimit,
                                             minId : 0,
                                             maxId : 0,
                                             hash : 0,
                                             offsetDate : 0,
                                             offsetId : 0
                                             ));

                var messages = resp.AsSliceTag().AssertSome().Messages;
                var docs     = messages
                               .Reverse()
                               .Choose(Message.AsTag)
                               .Choose(message => message.Media)
                               .Choose(MessageMedia.AsDocumentTag)
                               .Choose(x => x.Document)
                               .Choose(Document.AsTag);

                return(messages.Count == 0
                    ? docs
                    : (await GetHistory(offset + batchLimit)).Concat(docs));
            }

            var history = await GetHistory();

            var video     = history.Last(x => x.Attributes.Choose(DocumentAttribute.AsVideoTag).Any());
            var videoName = video.Attributes.Choose(DocumentAttribute.AsFilenameTag).Single().FileName;

            var videoLocation = new InputFileLocation.EncryptedTag(
                id: video.Id,
                accessHash: video.AccessHash
                );

            using (var fs = File.OpenWrite(videoName))
            {
                await tg.Upload.DownloadFile(fs, videoLocation);
            }
        }
        public static async Task Run(TelegramClient tg)
        {
            var fullUserInfo = await tg.Call(new GetFullUser(new InputUser.SelfTag()));

            var userInfo = fullUserInfo.Users.Single().Default !;

            var chatPeer = (InputPeer) new InputPeer.UserTag(
                userId: userInfo.Id,
                accessHash: userInfo.AccessHash !.Value
                );

            var history = await ScrapeHistoryDocuments(tg, chatPeer);

            var video     = history.Last(x => x.Attributes.NChoose(x => x.Video).Any());
            var videoName = video.Attributes.NChoose(x => x.Filename).Single().FileName;

            Console.WriteLine($"Downloading the video with name '{videoName}'.");
            var videoLocation = new InputFileLocation.EncryptedTag(
                id: video.Id,
                accessHash: video.AccessHash
                );

            await using var fs = File.OpenWrite(videoName);
            await tg.Upload.DownloadFile(fs, videoLocation);

            Console.WriteLine("The video is downloaded.");
        }
        static async Task <IReadOnlyList <Document.DefaultTag> > ScrapeHistoryDocuments(
            TelegramClient tg,
            InputPeer peer,
            int offset = 0
            )
        {
            const int batchLimit = 100;
            var       resp       = await tg.Call(new GetHistory(
                                                     peer : peer,
                                                     addOffset : offset,
                                                     limit : batchLimit,
                                                     minId : 0,
                                                     maxId : 0,
                                                     hash : 0,
                                                     offsetDate : 0,
                                                     offsetId : 0
                                                     ));

            var messages  = resp.Slice !.Messages;
            var documents = messages
                            .Reverse()
                            .NChoose(x => x.Default)
                            .NChoose(message => message.Media)
                            .NChoose(x => x.Document)
                            .NChoose(x => x.Document)
                            .NChoose(x => x.Default);

            return(messages.Count == 0
                ? documents.ToList()
                : (await ScrapeHistoryDocuments(tg, peer, offset + batchLimit)).Concat(documents).ToList());
        }
Ejemplo n.º 4
0
        public static async Task Run(TelegramClient tg)
        {
            var myInfo = await tg.Call(new GetFullUser(new InputUser.SelfTag()));

            Console.WriteLine("Here is info about your account.");
            Console.WriteLine(myInfo);
        }
Ejemplo n.º 5
0
        static async Task PrintUserInfo(TelegramClient tg)
        {
            var myInfo = await tg.Call(new GetFullUser(new InputUser.SelfTag()));

            Console.WriteLine(myInfo);
        }
Ejemplo n.º 6
0
 // pings each 10 seconds and ignores exceptions
 // tg.Call tries to reconnect if the connection is broken
 // so this construction should keep TelegramClient alive
 static IDisposable KeepAlive(TelegramClient tg) => Observable
 .Timer(dueTime: TimeSpan.Zero, period: TimeSpan.FromSeconds(10))
 .Select(_ => Observable.FromAsync(() => tg.Call(new Ping(pingId: 0))).Materialize())
 .Concat()
 .Subscribe();