2021-10-06 08:21:42 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using TL;
|
|
|
|
|
|
|
|
|
|
|
|
namespace WTelegramClientTest
|
|
|
|
|
|
{
|
2021-12-03 10:16:01 +01:00
|
|
|
|
static class Program_DownloadSavedMedia
|
2021-10-06 08:21:42 +02:00
|
|
|
|
{
|
2021-10-11 14:44:49 +02:00
|
|
|
|
// go to Project Properties > Debug > Environment variables and add at least these: api_id, api_hash, phone_number
|
2023-01-06 13:28:58 +01:00
|
|
|
|
static async Task Main(string[] _)
|
2021-10-06 08:21:42 +02:00
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("The program will download photos/medias from messages you send/forward to yourself (Saved Messages)");
|
2021-10-11 14:44:49 +02:00
|
|
|
|
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
2021-10-06 08:21:42 +02:00
|
|
|
|
var user = await client.LoginUserIfNeeded();
|
2022-07-29 15:24:18 +02:00
|
|
|
|
client.OnUpdate += Client_OnUpdate;
|
2021-10-06 08:21:42 +02:00
|
|
|
|
Console.ReadKey();
|
|
|
|
|
|
|
2023-05-01 18:21:03 +02:00
|
|
|
|
async Task Client_OnUpdate(UpdatesBase updates)
|
2021-10-06 08:21:42 +02:00
|
|
|
|
{
|
2023-05-01 18:21:03 +02:00
|
|
|
|
foreach (var update in updates.UpdateList)
|
2021-10-06 08:21:42 +02:00
|
|
|
|
{
|
2021-11-04 20:10:44 +01:00
|
|
|
|
if (update is not UpdateNewMessage { message: Message message })
|
|
|
|
|
|
continue; // if it's not about a new message, ignore the update
|
|
|
|
|
|
if (message.peer_id.ID != user.ID)
|
|
|
|
|
|
continue; // if it's not in the "Saved messages" chat, ignore it
|
|
|
|
|
|
|
2021-10-06 08:21:42 +02:00
|
|
|
|
if (message.media is MessageMediaDocument { document: Document document })
|
|
|
|
|
|
{
|
2023-01-06 13:28:58 +01:00
|
|
|
|
var filename = document.Filename; // use document original filename, or build a name from document ID & MIME type:
|
|
|
|
|
|
filename ??= $"{document.id}.{document.mime_type[(document.mime_type.IndexOf('/') + 1)..]}";
|
2021-10-06 08:21:42 +02:00
|
|
|
|
Console.WriteLine("Downloading " + filename);
|
|
|
|
|
|
using var fileStream = File.Create(filename);
|
|
|
|
|
|
await client.DownloadFileAsync(document, fileStream);
|
|
|
|
|
|
Console.WriteLine("Download finished");
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (message.media is MessageMediaPhoto { photo: Photo photo })
|
|
|
|
|
|
{
|
|
|
|
|
|
var filename = $"{photo.id}.jpg";
|
|
|
|
|
|
Console.WriteLine("Downloading " + filename);
|
|
|
|
|
|
using var fileStream = File.Create(filename);
|
|
|
|
|
|
var type = await client.DownloadFileAsync(photo, fileStream);
|
|
|
|
|
|
fileStream.Close(); // necessary for the renaming
|
|
|
|
|
|
Console.WriteLine("Download finished");
|
|
|
|
|
|
if (type is not Storage_FileType.unknown and not Storage_FileType.partial)
|
2021-11-04 20:10:44 +01:00
|
|
|
|
File.Move(filename, $"{photo.id}.{type}", true); // rename extension
|
2021-10-06 08:21:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|