diff --git a/Examples/Program_DownloadSavedMedia.cs b/Examples/Program_DownloadSavedMedia.cs index 519cdd5..62b6fad 100644 --- a/Examples/Program_DownloadSavedMedia.cs +++ b/Examples/Program_DownloadSavedMedia.cs @@ -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 } } } diff --git a/Examples/Program_ListenUpdates.cs b/Examples/Program_ListenUpdates.cs index 2d4e779..275094b 100644 --- a/Examples/Program_ListenUpdates.cs +++ b/Examples/Program_ListenUpdates.cs @@ -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(); diff --git a/README.md b/README.md index a84fc23..9dbf251 100644 --- a/README.md +++ b/README.md @@ -46,16 +46,18 @@ static string Config(string what) if (what == "api_id") return "YOUR_API_ID"; if (what == "api_hash") return "YOUR_API_HASH"; if (what == "phone_number") return "+12025550156"; - if (what == "verification_code") { Console.Write("Code: "); return Console.ReadLine(); } - if (what == "first_name") return "John"; // if sign-up is required - if (what == "last_name") return "Doe"; // if sign-up is required - if (what == "password") return "secret!"; // if user has enabled 2FA + if (what == "verification_code") return null; // let WTelegramClient ask the user with a console prompt + if (what == "first_name") return "John"; // if sign-up is required + if (what == "last_name") return "Doe"; // if sign-up is required + if (what == "password") return "secret!"; // if user has enabled 2FA return null; } ... using var client = new WTelegram.Client(Config); ``` -There are other configuration items that are queried to your method but returning `null` let WTelegramClient choose a default adequate value. Those shown above are the only ones that have no default values and are required to be provided by your method. +There are other configuration items that are queried to your method but returning `null` let WTelegramClient choose a default adequate value. Those shown above are the only ones that have no default values and should be provided by your method. + +Another simpler approach is to pass `Environment.GetEnvironmentVariable` as the config callback and define the above items as environment variables. Finally, if you want to redirect the library logs to your logger instead of the Console, you can install a delegate in the `WTelegram.Helpers.Log` static property. Its `int` argument is the log severity, compatible with the classic [LogLevel enum](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loglevel) diff --git a/src/Client.cs b/src/Client.cs index 0cd19a6..6cfc81f 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -101,13 +101,14 @@ namespace WTelegram "lang_pack" => "", "lang_code" => CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, "user_id" => "-1", - _ => null // api_id api_hash phone_number verification_code... it's up to you to reply to these correctly + "verification_code" => AskConfig(config), + _ => null // api_id api_hash phone_number... it's up to you to reply to these correctly }; - public static string DefaultConfigOrAsk(string config) + public static string DefaultConfigOrAsk(string config) => DefaultConfig(config) ?? AskConfig(config); + + private static string AskConfig(string config) { - var value = DefaultConfig(config); - if (value != null) return value; Console.Write($"Enter {config.Replace('_', ' ')}: "); return Console.ReadLine(); }