A null config value for "verification_code" will show a console prompt. This allows Environment.GetEnvironmentVariable to be used directly as config callback.

This commit is contained in:
Wizou 2021-10-11 14:44:49 +02:00
parent 87a85bec4b
commit 4f9fbfc12c
4 changed files with 18 additions and 27 deletions

View file

@ -9,17 +9,11 @@ namespace WTelegramClientTest
{
class Program_DownloadSavedMedia
{
static string Config(string what)
{
// go to Project Properties > Debug > Environment variables and add at least these: api_id, api_hash, phone_number
if (what == "verification_code") { Console.Write("Code: "); return Console.ReadLine(); }
return Environment.GetEnvironmentVariable(what);
}
// go to Project Properties > Debug > Environment variables and add at least these: api_id, api_hash, phone_number
static async Task Main(string[] args)
{
Console.WriteLine("The program will download photos/medias from messages you send/forward to yourself (Saved Messages)");
using var client = new WTelegram.Client(Config);
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
await client.ConnectAsync();
var user = await client.LoginUserIfNeeded();
client.Update += Client_Update;
@ -34,7 +28,7 @@ namespace WTelegramClientTest
continue; // if it's not a new saved message, ignore it
if (message.media is MessageMediaDocument { document: Document document })
{
int slash = document.mime_type.IndexOf('/'); // quick & dirty conversion from Mime to extension
int slash = document.mime_type.IndexOf('/'); // quick & dirty conversion from MIME type to file extension
var filename = slash > 0 ? $"{document.id}.{document.mime_type[(slash + 1)..]}" : $"{document.id}.bin";
Console.WriteLine("Downloading " + filename);
using var fileStream = File.Create(filename);
@ -50,7 +44,7 @@ namespace WTelegramClientTest
fileStream.Close(); // necessary for the renaming
Console.WriteLine("Download finished");
if (type is not Storage_FileType.unknown and not Storage_FileType.partial)
File.Move(filename, Path.ChangeExtension(filename, type.ToString())); // rename extension
File.Move(filename, $"{photo.id}.{type}"); // rename extension
}
}
}

View file

@ -8,18 +8,12 @@ namespace WTelegramClientTest
{
class Program_ListenUpdates
{
static string Config(string what)
{
// go to Project Properties > Debug > Environment variables and add at least these: api_id, api_hash, phone_number
if (what == "verification_code") { Console.Write("Code: "); return Console.ReadLine(); }
return Environment.GetEnvironmentVariable(what);
}
// go to Project Properties > Debug > Environment variables and add at least these: api_id, api_hash, phone_number
static async Task Main(string[] _)
{
Console.WriteLine("The program will display updates received for the logged-in user. Press any key to terminate");
WTelegram.Helpers.Log += (l, s) => System.Diagnostics.Debug.WriteLine(s);
using var client = new WTelegram.Client(Config) { CollectAccessHash = true };
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
client.Update += Client_Update;
await client.ConnectAsync();
var my = await client.LoginUserIfNeeded();