Home > Software engineering >  How do I know that a telegram message has been viewed by the interlocutor
How do I know that a telegram message has been viewed by the interlocutor

Time:11-14

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]);

CodePudding user response:

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

CodePudding user response:

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

  • Related