mirror of
https://github.com/wiz0u/WTelegramClient.git
synced 2025-12-06 06:52:01 +01:00
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:
parent
87a85bec4b
commit
4f9fbfc12c
|
|
@ -8,18 +8,12 @@ using TL;
|
|||
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);
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,19 +7,13 @@ using TL;
|
|||
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);
|
||||
}
|
||||
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ 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 == "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
|
||||
|
|
@ -55,7 +55,9 @@ static string Config(string what)
|
|||
...
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue