Improved examples documentation

This commit is contained in:
Wizou 2021-10-13 00:27:40 +02:00
parent 37ce6524e9
commit 98a95376f3
5 changed files with 22 additions and 9 deletions

View file

@ -43,6 +43,14 @@ namespace WTelegramClientTest
}
else
{
// Zero means the access hash for Durov's Channel was not collected yet.
// So we need to obtain it through Client API calls whose results contains the access_hash field, such as:
// - Messages_GetAllChats (see Program_GetAllChats.cs for an example on how to use it)
// - Messages_GetDialogs (see Program_ListenUpdates.cs for an example on how to use it)
// - Contacts_ResolveUsername (see below for an example on how to use it)
// and many more API methods...
// The access_hash fields can be found inside instance of User, Channel, Photo, Document, etc..
// usually listed through their base class UserBase, ChatBase, PhotoBase, DocumentBase, etc...
Console.WriteLine("Resolving channel @durov to get its ID, access hash and other infos...");
var durovResolved = await client.Contacts_ResolveUsername("durov"); // @durov = Durov's Channel
if (durovResolved.peer.ID != DurovID)

View file

@ -9,6 +9,7 @@ namespace WTelegramClientTest
class Program_GetAllChats
{
// This code is similar to what you should have obtained if you followed the README introduction
// I've just added a few comments to explain further what's going on
// go to Project Properties > Debug > Environment variables and add at least these: api_id, api_hash, phone_number
static string Config(string what)
@ -30,7 +31,7 @@ namespace WTelegramClientTest
var user = await client.LoginUserIfNeeded();
Console.WriteLine($"We are logged-in as {user.username ?? user.first_name + " " + user.last_name} (id {user.id})");
var chats = await client.Messages_GetAllChats(null);
var chats = await client.Messages_GetAllChats(null); // chats = groups/channels (does not include users dialogs)
Console.WriteLine("This user has joined the following:");
foreach (var chat in chats.chats)
switch (chat)
@ -42,7 +43,7 @@ namespace WTelegramClientTest
Console.WriteLine($"{channel.id}: Channel {channel.username}: {channel.title}");
//Console.WriteLine($" → access_hash = {channel.access_hash:X}");
break;
case Channel group:
case Channel group: // no broadcast flag => it's a big group, also called supergroup or megagroup
Console.WriteLine($"{group.id}: Group {group.username}: {group.title}");
//Console.WriteLine($" → access_hash = {group.access_hash:X}");
break;
@ -52,7 +53,7 @@ namespace WTelegramClientTest
long id = long.Parse(Console.ReadLine());
var target = chats.chats.First(chat => chat.ID == id);
Console.WriteLine($"Sending a message in chat {target.ID}: {target.Title}");
// This line implicitely creates an adequate InputPeer (with eventual access_hash) from ChatBase:
// Next line implicitely creates an adequate InputPeer from ChatBase: (with the access_hash if these is one)
InputPeer peer = target;
await client.SendMessageAsync(peer, "Hello, World");
}

View file

@ -20,7 +20,7 @@ namespace WTelegramClientTest
users[my.id] = my;
// note that on login Telegram may sends a bunch of updates/messages that happened in the past and were not acknowledged
Console.WriteLine($"We are logged-in as {my.username ?? my.first_name + " " + my.last_name} (id {my.id})");
var dialogsBase = await client.Messages_GetDialogs(default, 0, null, 0, 0);
var dialogsBase = await client.Messages_GetDialogs(default, 0, null, 0, 0); // dialogs = groups/channels/users
if (dialogsBase is Messages_Dialogs dialogs)
while (dialogs.dialogs.Length != 0)
{
@ -33,7 +33,7 @@ namespace WTelegramClientTest
dialogs = (Messages_Dialogs)await client.Messages_GetDialogs(lastMsg?.Date ?? default, lastDialog.top_message, offsetPeer, 500, 0);
}
Console.ReadKey();
await client.Ping(43); // dummy API call.. this is used to force an acknowledge on this session's updates
await client.Ping(42); // dummy API call
}