How to detect edited or deleted messages on telegram TLSharp

2.5k views Asked by At

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( ... );
                    }
                }
            }
        }
    }
}
0

There are 0 answers