WTelegramClient/Examples/Program_GetAllChats.cs

59 lines
2.6 KiB
C#
Raw Normal View History

2021-10-11 18:29:24 +02:00
using System;
using System.Threading.Tasks;
using TL;
namespace WTelegramClientTest
{
2021-12-03 10:16:01 +01:00
static class Program_GetAllChats
2021-10-11 18:29:24 +02:00
{
// This code is similar to what you should have obtained if you followed the README introduction
2021-10-13 00:27:40 +02:00
// I've just added a few comments to explain further what's going on
2021-10-11 18:29:24 +02:00
// go to Project Properties > Debug > Launch Profiles > Environment variables and add at least these: api_id, api_hash, phone_number
2021-10-11 18:29:24 +02:00
static string Config(string what)
{
if (what == "api_id") return Environment.GetEnvironmentVariable("api_id");
if (what == "api_hash") return Environment.GetEnvironmentVariable("api_hash");
if (what == "phone_number") return Environment.GetEnvironmentVariable("phone_number");
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;
}
static async Task Main(string[] _)
{
2024-09-07 01:59:27 +02:00
await using var client = new WTelegram.Client(Config);
2021-10-11 18:29:24 +02:00
var user = await client.LoginUserIfNeeded();
Console.WriteLine($"We are logged-in as {user.username ?? user.first_name + " " + user.last_name} (id {user.id})");
2022-02-13 03:15:23 +01:00
var chats = await client.Messages_GetAllChats(); // chats = groups/channels (does not include users dialogs)
2021-10-11 18:29:24 +02:00
Console.WriteLine("This user has joined the following:");
foreach (var (id, chat) in chats.chats)
2021-10-11 18:29:24 +02:00
switch (chat)
{
case Chat smallgroup when smallgroup.IsActive:
Console.WriteLine($"{id}: Small group: {smallgroup.title} with {smallgroup.participants_count} members");
2021-10-11 18:29:24 +02:00
break;
case Channel channel when channel.IsChannel:
Console.WriteLine($"{id}: Channel {channel.username}: {channel.title}");
2021-10-11 18:29:24 +02:00
//Console.WriteLine($" → access_hash = {channel.access_hash:X}");
break;
2021-10-13 00:27:40 +02:00
case Channel group: // no broadcast flag => it's a big group, also called supergroup or megagroup
Console.WriteLine($"{id}: Group {group.username}: {group.title}");
2021-10-11 18:29:24 +02:00
//Console.WriteLine($" → access_hash = {group.access_hash:X}");
break;
}
Console.Write("Type a chat ID to send a message: ");
long chatId = long.Parse(Console.ReadLine());
var target = chats.chats[chatId];
Console.WriteLine($"Sending a message in chat {chatId}: {target.Title}");
2021-10-13 00:27:40 +02:00
// Next line implicitely creates an adequate InputPeer from ChatBase: (with the access_hash if these is one)
2021-10-11 18:29:24 +02:00
InputPeer peer = target;
await client.SendMessageAsync(peer, "Hello, World");
}
}
}