From 8fe0c086bba765d8a4d8b405cbd71fc4c033f48c Mon Sep 17 00:00:00 2001 From: Wizou Date: Thu, 16 Dec 2021 14:41:43 +0100 Subject: [PATCH] Added contacts list & GUI examples --- EXAMPLES.md | 33 +++++++++++++++++++++++++++++++++ FAQ.md | 2 +- README.md | 2 +- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index c6e2216..0f55d65 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -191,6 +191,39 @@ for (int offset = 0; ;) } ``` + +### Retrieve the current user's contacts list +There are two different methods. Here is the simpler one: +```csharp +using var client = new WTelegram.Client(Environment.GetEnvironmentVariable); +await client.LoginUserIfNeeded(); +var contacts = await client.Contacts_GetContacts(0); +foreach (User contact in contacts.users.Values) + Console.WriteLine($"{contact} {contact.phone}"); +``` + +The second method uses the more complex GDPR export, **takeout session** system. +Here is an example on how to implement it: +```csharp +using TL.Methods; // methods structures, for InvokeWithTakeout + +using var client = new WTelegram.Client(Environment.GetEnvironmentVariable); +await client.LoginUserIfNeeded(); +var takeout = await client.Account_InitTakeoutSession(contacts: true); +var finishTakeout = new Account_FinishTakeoutSession(); +try +{ + var savedContacts = await client.InvokeWithTakeout(takeout.id, new Contacts_GetSaved()); + foreach (SavedPhoneContact contact in savedContacts) + Console.WriteLine($"{contact.first_name} {contact.last_name} {contact.phone}, added on {contact.date}"); + finishTakeout.flags = Account_FinishTakeoutSession.Flags.success; +} +finally +{ + await client.InvokeWithTakeout(takeout.id, finishTakeout); +} +``` + ### Monitor all Telegram events happening for the user diff --git a/FAQ.md b/FAQ.md index 69855e2..48fd26b 100644 --- a/FAQ.md +++ b/FAQ.md @@ -34,7 +34,7 @@ An easy solution is to call `Interaction.InputBox("Enter verification code")` in This might require adding a reference *(and `using`)* to the Microsoft.VisualBasic assembly. A more complex solution requires the use of a `ManualResetEventSlim` that you will wait for in Config callback, -and when the user has provided the verification_code through your GUI, you "set" the event to release your Config callback so it can return the code. +and when the user has provided the verification_code through your GUI, you "set" the event to release your Config callback so it can return the code. ([see example](https://stackoverflow.com/a/70379582/3365403)) #### 4. Where to get the access_hash? Why the error `CHANNEL_INVALID` or `USER_ID_INVALID`? diff --git a/README.md b/README.md index 847f343..2673c5a 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ # How to use >⚠️ This library relies on asynchronous C# programming (`async/await`) so make sure you are familiar with this advanced topic before proceeding. ->If you are a beginner in C#, making a project based on this library might not be a great idea. +>If you are a beginner in C#, starting a project based on this library might not be a great idea. After installing WTelegramClient through Nuget, your first Console program will be as simple as: ```csharp