2021-09-05 01:08:16 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2021-09-25 02:13:06 +02:00
|
|
|
|
using System.Linq;
|
2021-09-05 01:08:16 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using TL;
|
|
|
|
|
|
|
|
|
|
|
|
namespace WTelegramClientTest
|
|
|
|
|
|
{
|
|
|
|
|
|
class Program_ListenUpdates
|
|
|
|
|
|
{
|
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);
|
2021-10-11 14:44:49 +02:00
|
|
|
|
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
2021-09-05 01:08:16 +02:00
|
|
|
|
client.Update += Client_Update;
|
2021-09-25 02:13:06 +02:00
|
|
|
|
var my = await client.LoginUserIfNeeded();
|
2021-11-09 03:00:03 +01:00
|
|
|
|
_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
|
2021-09-25 02:13:06 +02:00
|
|
|
|
Console.WriteLine($"We are logged-in as {my.username ?? my.first_name + " " + my.last_name} (id {my.id})");
|
2021-11-09 03:00:03 +01:00
|
|
|
|
// We collect all infos about the users/chats so that updates can be printed with their names
|
2021-10-13 00:27:40 +02:00
|
|
|
|
var dialogsBase = await client.Messages_GetDialogs(default, 0, null, 0, 0); // dialogs = groups/channels/users
|
2021-09-25 02:13:06 +02:00
|
|
|
|
if (dialogsBase is Messages_Dialogs dialogs)
|
2021-09-26 05:07:17 +02:00
|
|
|
|
while (dialogs.dialogs.Length != 0)
|
2021-09-25 02:13:06 +02:00
|
|
|
|
{
|
2021-11-09 03:00:03 +01:00
|
|
|
|
foreach (var (id, user) in dialogs.users) _users[id] = user;
|
|
|
|
|
|
foreach (var (id, chat) in dialogs.chats) _chats[id] = chat;
|
2021-10-22 15:26:46 +02:00
|
|
|
|
var lastDialog = dialogs.dialogs[^1];
|
|
|
|
|
|
var lastMsg = dialogs.messages.LastOrDefault(m => m.Peer.ID == lastDialog.Peer.ID && m.ID == lastDialog.TopMessage);
|
2021-10-23 01:37:50 +02:00
|
|
|
|
var offsetPeer = dialogs.UserOrChat(lastDialog).ToInputPeer();
|
2021-10-22 15:26:46 +02:00
|
|
|
|
dialogs = (Messages_Dialogs)await client.Messages_GetDialogs(lastMsg?.Date ?? default, lastDialog.TopMessage, offsetPeer, 500, 0);
|
2021-09-25 02:13:06 +02:00
|
|
|
|
}
|
2021-09-05 01:08:16 +02:00
|
|
|
|
Console.ReadKey();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-09 03:00:03 +01:00
|
|
|
|
private static readonly Dictionary<long, UserBase> _users = new();
|
|
|
|
|
|
private static readonly Dictionary<long, ChatBase> _chats = new();
|
|
|
|
|
|
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
|
|
|
|
|
2021-11-07 16:52:58 +01:00
|
|
|
|
private static void Client_Update(IObject arg)
|
2021-09-05 01:08:16 +02:00
|
|
|
|
{
|
2021-11-09 03:00:03 +01:00
|
|
|
|
if (arg is not UpdatesBase updates) return;
|
|
|
|
|
|
foreach(var (id, user) in updates.Users) _users[id] = user;
|
|
|
|
|
|
foreach(var (id, chat) in updates.Chats) _chats[id] = chat;
|
|
|
|
|
|
foreach (var update in updates.UpdateList)
|
|
|
|
|
|
switch (update)
|
|
|
|
|
|
{
|
|
|
|
|
|
case UpdateNewMessage unm: DisplayMessage(unm.message); break;
|
|
|
|
|
|
case UpdateEditMessage uem: DisplayMessage(uem.message, true); break;
|
|
|
|
|
|
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;
|
|
|
|
|
|
case UpdateUserName uun: Console.WriteLine($"{User(uun.user_id)} has changed profile name: @{uun.username} {uun.first_name} {uun.last_name}"); break;
|
|
|
|
|
|
case UpdateUserPhoto uup: Console.WriteLine($"{User(uup.user_id)} has changed profile photo"); break;
|
|
|
|
|
|
default: Console.WriteLine(update.GetType().Name); break;
|
|
|
|
|
|
}
|
2021-09-05 01:08:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-17 03:16:51 +02:00
|
|
|
|
private static void 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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|