skip to Main Content

I use WTelgramClient to remind patients about an upcoming doctor’s appointment.

I am sending a message and I need to make sure that it is read.

using var client = new WTelegram.Client(Config);
var user = await client.LoginUserIfNeeded();
var contact = await client.Contacts_ImportContacts(new[] { new InputPhoneContact { phone = phoneNumber } });
object p = await client.SendMessageAsync(contact.users[contact.users.Keys.First()], patientMessage);

It seems that this can be done via flag has_views. I tried the code below, but I can’t call dialogs.messages[0].flag.has_views.

var dialogs = await client.Messages_GetAllDialogs();
Console.WriteLine(dialogs.messages[0]);

2

Answers


  1. First, you should store the user info and the sent message info like this:

    var targetUser = contact.users.Values.First();
    var msg = await client.SendMessageAsync(targetUser, text);
    

    Later, you can check that the matching Dialog.read_outbox_max_id indicate your message was read, like this:

    var dialogs = await client.Messages_GetAllDialogs();
    foreach (Dialog dialog in dialogs.dialogs)
        if (dialog.peer.ID == targetUser.id)
            if (dialog.read_outbox_max_id >= msg.id)
                Console.WriteLine("Sent message was read");
    

    Alternatively, if your program is running in the background, it can monitor updates in realtime, waiting for an UpdateReadHistoryOutbox that matches the targetUser.id and a max_id >= msg.id

    Login or Signup to reply.
  2. Probably you should add a listener for events OnUpdate that is available from the client:

    client.OnUpdate += Client_OnUpdate
    
    private static async Task Client_OnUpdate(IObject arg)
        {
            if (arg is not UpdatesBase updates) return;
            updates.CollectUsersChats(Users, Chats);
            foreach (var update in updates.UpdateList)
                switch (update)
                {
                    case UpdateReadMessagesContents urmc: //get id of read messages: urmc.messages 
                    default: Console.WriteLine(update.GetType().Name); break; // there are much more update types than the above example cases
                }
        }
    

    and check if there is any event of type: UpdateReadMessagesContents from where you can get the ids of read messages.

    I hope it will help

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search