How to detect edited or deleted messages on telegram TLSharp
Asked Answered
A

0

6

I am trying to detect which message is edited or deleted on a subscribed channel on telegram with TLSharp library in c#.

1- in a while(true) loop I am getting latest updates.

2- when I delete or edit a message for test, I receive TLUpdateChannelTooLong only.

3- then I use client.GetHistoryAsync function to get channel messages, and check their EditDate.

But I don't know how much should I go deep in history and I can not find deleted message with this code easily.

Is there any solution to find deleted/edited messages easy and safe?

Part of my code:

state = await client.SendRequestAsync<TLState>(new TLRequestGetState());
while (true)
{
    await Task.Delay(1000);
    var req = new TLRequestGetDifference() { Date = state.Date, Pts = state.Pts, Qts = state.Qts };
    TLDifference diff = null;
    try
    {
        diff = await client.SendRequestAsync<TLAbsDifference>(req) as TLDifference;
    }
    catch (Exception ex)
    {
        HandleThisException(ex);
    }
    //--
    if (diff != null)
    {
        state = await client.SendRequestAsync<TLState>(new TLRequestGetState());
        foreach (var upd in diff.OtherUpdates.OfType<TLUpdateNewChannelMessage>())
        {
            var tm = (upd.Message as TLMessage);
            if (tm == null) { continue; } // ?
            var textMessage = tm.Message;
            if (tm.Media != null)
            {
                if (tm.Media.GetType().ToString() == "TeleSharp.TL.TLMessageMediaPhoto")
                {
                    var tLMessageMediaPhoto = (tm.Media as TLMessageMediaPhoto);
                    textMessage = tLMessageMediaPhoto.Caption;
                }
            }
            try
            {
                var from = (tm.ToId as TLPeerChannel).ChannelId;
                long replyTo = tm.ReplyToMsgId == null ? 0 : (long)tm.ReplyToMsgId;
                await AnalyzeNewMessage( ... );
            }
            catch (Exception exParsing)
            {
                HandleThisException(exParsing);
            }
        }
         // Checking Edited/Deleted Messages
        foreach(var upLong in diff.OtherUpdates.OfType<TLUpdateChannelTooLong>())
        {
            TLChannel theChat = null;
            foreach(var chat in diff.Chats.OfType<TLChannel>())
            {
                if(chat.Id == upLong.ChannelId) { theChat = chat;  break; }
            }
            if (theChat != null)
            {
                var x = await client.GetHistoryAsync(
                    new TLInputPeerChannel { ChannelId = theChat.Id, AccessHash = (long)theChat.AccessHash },
                    0,-1,2
                    ); // checking only 2 last messages!
                var ChMsgs = x as TLChannelMessages;
                foreach (var msg in ChMsgs.Messages.OfType<TLMessage>())
                {
                    if(msg.EditDate != null)
                    {
                        var txt = msg.Message;
                        if (msg.Media != null)
                        {
                            if (msg.Media.GetType().ToString() == "TeleSharp.TL.TLMessageMediaPhoto")
                            {
                                txt = (msg.Media as TLMessageMediaPhoto).Caption;
                            }
                        }
                        await AnalyzeEditedMessage( ... );
                    }
                }
            }
        }
    }
}
Alcestis answered 4/12, 2018 at 0:32 Comment(3)
It would be awesome if you could provide a minimal reproducible exampleBauble
I added most important part of codes for easier checking, @JohnBAlcestis
It seems there is no special way to detect deleted/edited messages and we should keep message id and edited date and compare all of them with existing messages on channel...Alcestis

© 2022 - 2024 — McMap. All rights reserved.