Search code examples
telegram-apiwtelegramclient

How to work with messages from WTelegramClient updates? (get chat/user infos)


I'm new to the WTelegramClient C# Library and was used to TLSharp (not working anymore)

I'm trying to understand how I get User info after update is received, I have the example code that listen to updates and write them in console but I can't understand how I can respond to the user that sent the message (new update)

I think I need the user id/access_hash to send message to the sender but I can't understand how

Here is how I get the new messages but it can get only username or name/id

   private static void DisplayMessage(MessageBase messageBase, bool edit = false)
            {
                if (edit) Console.Write("(Edit): ");
                switch (messageBase)
                {
                    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;
                }
            }

Here i can get the name or username of sender(if have) and the message itself MessageService ('user' not channel or group) for example get me only firstname and lastname

How to get all info of sender or chat itself (i want to try mark as read the message)

I'm used to TLSharp and the new library WTelegramClient is different.

Thanks!!!


Solution

  • Below is a quick example on how to modify HandleMessage to react to a message sent in private from a user, get the details about this user, verify who it is and which text was sent to us, and then send him a message back.

    Notes:

    • For this example to work, you will need the latest version of Program_ListenUpdates.cs with static variables
    • HandleMessage is now async Task, in order to use await
    • You can pass user to send a message because class User is implicitly converted to InputPeerUser (with the user id/access_hash).
    • You can do similarly for messages coming from chats, using PeerChat/PeerChannel classes and the _chats dictionary to get chat details
    private static async Task HandleMessage(MessageBase messageBase, bool edit = false)
    {
        if (edit) Console.Write("(Edit): ");
        switch (messageBase)
        {
            case Message m:
                Console.WriteLine($"{Peer(m.from_id) ?? m.post_author} in {Peer(m.peer_id)}> {m.message}");
                if (m.flags.HasFlag(Message.Flags.out_))
                    break; // ignore our own outgoing messages
                if (m.Peer is PeerUser pu) // got a message in a direct chat with a user
                {
                    if (Manager.Users.TryGetValue(pu.user_id, out var user)) // get user details
                    {
                        if (user.username == "Wiz0u" && m.message == "hello")
                        {
                            await Client.SendMessageAsync(user, $"hi {user.first_name}, I'm {My.first_name}");
                        }
                    }
                }
                break;
            case MessageService ms:
                Console.WriteLine($"{Peer(ms.from_id)} in {Peer(ms.peer_id)} [{ms.action.GetType().Name[13..]}]");
                break;
        }
    }