2021-09-05 01:08:16 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using TL;
|
|
|
|
|
|
|
|
|
|
|
|
namespace WTelegramClientTest
|
|
|
|
|
|
{
|
2021-12-03 10:16:01 +01:00
|
|
|
|
static class Program_ListenUpdates
|
2021-09-05 01:08:16 +02:00
|
|
|
|
{
|
2022-02-10 02:28:32 +01:00
|
|
|
|
static WTelegram.Client Client;
|
|
|
|
|
|
static User My;
|
2022-03-28 12:41:28 +02:00
|
|
|
|
static readonly Dictionary<long, User> Users = new();
|
|
|
|
|
|
static readonly Dictionary<long, ChatBase> Chats = new();
|
2022-02-10 02:28:32 +01:00
|
|
|
|
|
2021-10-11 14:44:49 +02:00
|
|
|
|
// go to Project Properties > Debug > Environment variables and add at least these: api_id, api_hash, phone_number
|
2021-09-05 01:08:16 +02:00
|
|
|
|
static async Task Main(string[] _)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("The program will display updates received for the logged-in user. Press any key to terminate");
|
2021-11-09 03:00:03 +01:00
|
|
|
|
WTelegram.Helpers.Log = (l, s) => System.Diagnostics.Debug.WriteLine(s);
|
2022-02-10 02:28:32 +01:00
|
|
|
|
Client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
|
|
|
|
|
using (Client)
|
|
|
|
|
|
{
|
2022-07-29 15:24:18 +02:00
|
|
|
|
Client.OnUpdate += Client_OnUpdate;
|
2022-02-10 02:28:32 +01:00
|
|
|
|
My = await Client.LoginUserIfNeeded();
|
2022-03-28 12:41:28 +02:00
|
|
|
|
Users[My.id] = My;
|
2022-06-14 00:58:51 +02:00
|
|
|
|
// Note: on login, Telegram may sends a bunch of updates/messages that happened in the past and were not acknowledged
|
2022-02-10 02:28:32 +01:00
|
|
|
|
Console.WriteLine($"We are logged-in as {My.username ?? My.first_name + " " + My.last_name} (id {My.id})");
|
|
|
|
|
|
// We collect all infos about the users/chats so that updates can be printed with their names
|
2022-02-27 22:06:13 +01:00
|
|
|
|
var dialogs = await Client.Messages_GetAllDialogs(); // dialogs = groups/channels/users
|
2022-03-28 12:41:28 +02:00
|
|
|
|
dialogs.CollectUsersChats(Users, Chats);
|
2022-02-10 02:28:32 +01:00
|
|
|
|
Console.ReadKey();
|
|
|
|
|
|
}
|
2021-09-05 01:08:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-01 19:06:31 +02:00
|
|
|
|
// if not using async/await, we could just return Task.CompletedTask
|
2023-05-01 18:21:03 +02:00
|
|
|
|
private static async Task Client_OnUpdate(UpdatesBase updates)
|
2021-09-05 01:08:16 +02:00
|
|
|
|
{
|
2022-03-28 12:41:28 +02:00
|
|
|
|
updates.CollectUsersChats(Users, Chats);
|
2021-11-09 03:00:03 +01:00
|
|
|
|
foreach (var update in updates.UpdateList)
|
|
|
|
|
|
switch (update)
|
|
|
|
|
|
{
|
2022-08-01 19:06:31 +02:00
|
|
|
|
case UpdateNewMessage unm: await DisplayMessage(unm.message); break;
|
|
|
|
|
|
case UpdateEditMessage uem: await DisplayMessage(uem.message, true); break;
|
2022-08-12 21:19:10 +02:00
|
|
|
|
// Note: UpdateNewChannelMessage and UpdateEditChannelMessage are also handled by above cases
|
2021-11-09 03:00:03 +01:00
|
|
|
|
case UpdateDeleteChannelMessages udcm: Console.WriteLine($"{udcm.messages.Length} message(s) deleted in {Chat(udcm.channel_id)}"); break;
|
|
|
|
|
|
case UpdateDeleteMessages udm: Console.WriteLine($"{udm.messages.Length} message(s) deleted"); break;
|
|
|
|
|
|
case UpdateUserTyping uut: Console.WriteLine($"{User(uut.user_id)} is {uut.action}"); break;
|
|
|
|
|
|
case UpdateChatUserTyping ucut: Console.WriteLine($"{Peer(ucut.from_id)} is {ucut.action} in {Chat(ucut.chat_id)}"); break;
|
|
|
|
|
|
case UpdateChannelUserTyping ucut2: Console.WriteLine($"{Peer(ucut2.from_id)} is {ucut2.action} in {Chat(ucut2.channel_id)}"); break;
|
|
|
|
|
|
case UpdateChatParticipants { participants: ChatParticipants cp }: Console.WriteLine($"{cp.participants.Length} participants in {Chat(cp.chat_id)}"); break;
|
|
|
|
|
|
case UpdateUserStatus uus: Console.WriteLine($"{User(uus.user_id)} is now {uus.status.GetType().Name[10..]}"); break;
|
2022-11-01 19:26:40 +01:00
|
|
|
|
case UpdateUserName uun: Console.WriteLine($"{User(uun.user_id)} has changed profile name: {uun.first_name} {uun.last_name}"); break;
|
2022-12-29 22:33:28 +01:00
|
|
|
|
case UpdateUser uu: Console.WriteLine($"{User(uu.user_id)} has changed infos/photo"); break;
|
2022-11-08 17:06:16 +01:00
|
|
|
|
default: Console.WriteLine(update.GetType().Name); break; // there are much more update types than the above example cases
|
2021-11-09 03:00:03 +01:00
|
|
|
|
}
|
2021-09-05 01:08:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-01 19:06:31 +02:00
|
|
|
|
// in this example method, we're not using async/await, so we just return Task.CompletedTask
|
|
|
|
|
|
private static Task DisplayMessage(MessageBase messageBase, bool edit = false)
|
2021-09-05 01:08:16 +02:00
|
|
|
|
{
|
2021-10-17 03:16:51 +02:00
|
|
|
|
if (edit) Console.Write("(Edit): ");
|
2021-09-05 01:08:16 +02:00
|
|
|
|
switch (messageBase)
|
|
|
|
|
|
{
|
2021-11-09 03:00:03 +01:00
|
|
|
|
case Message m: Console.WriteLine($"{Peer(m.from_id) ?? m.post_author} in {Peer(m.peer_id)}> {m.message}"); break;
|
|
|
|
|
|
case MessageService ms: Console.WriteLine($"{Peer(ms.from_id)} in {Peer(ms.peer_id)} [{ms.action.GetType().Name[13..]}]"); break;
|
2021-09-05 01:08:16 +02:00
|
|
|
|
}
|
2022-08-01 19:06:31 +02:00
|
|
|
|
return Task.CompletedTask;
|
2021-09-05 01:08:16 +02:00
|
|
|
|
}
|
2022-03-28 12:41:28 +02:00
|
|
|
|
|
|
|
|
|
|
private static string User(long id) => Users.TryGetValue(id, out var user) ? user.ToString() : $"User {id}";
|
|
|
|
|
|
private static string Chat(long id) => Chats.TryGetValue(id, out var chat) ? chat.ToString() : $"Chat {id}";
|
|
|
|
|
|
private static string Peer(Peer peer) => peer is null ? null : peer is PeerUser user ? User(user.user_id)
|
|
|
|
|
|
: peer is PeerChat or PeerChannel ? Chat(peer.ID) : $"Peer {peer.ID}";
|
2021-09-05 01:08:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|