Telegram, tracking message edit/delete and editing my own messages (Client, not Bot API)

2.3k Views Asked by At

So I'm trying to implement the logging of telegram chats into my ELK storage in a proper way, and the existing solution with tgcli is too old (I also have a PoC which logs message edits from Android client via Xposed, but its implemented on top of UI level and is ineffective)

I need to receive edits/deletion of messages, and do it with client Telegram API.

Spent a day on researching it:

  • support for editing messages appeared in May 15, 2016 (telegram blog)
  • telegram-cli's tgl library is 2 years old and most likely has no support for that layer
  • I looked into telegramdesktop source as it was very promising, unfortunately their git history has no scheme changes poiting to edit support.
  • And the official layer version list is truncated. Security via obscurity eh.
  • from some tests done with golang library used in shelomentsevd/telegramgo, edits in supergroup are handled by TL_updateChannelTooLong message

Now I don't want to lose more time picking the libraries/sources. So, I'm asking about the experience with either of the following libraries, I'm looking for exactly one library which will allow to implement the required features fast - for someone who doesn't want to dive deep into MTProto's specifics.

1

There are 1 best solutions below

0
On

It's much easier to do it in telethon.

Here is a sample code I've put together gathering snippets directly from the docs.

from telethon import TelegramClient, events

API_ID = ...
API_HASH = " ... "

client = TelegramClient('session', api_id=API_ID, api_hash=API_HASH)

@client.on(events.MessageDeleted)
async def handler(event):
    # Log all deleted message IDs
    for msg_id in event.deleted_ids:
        print('Message', msg_id, 'was deleted in', event.chat_id)

@client.on(events.MessageEdited)
async def handler(event):
    # Log the date of new edits
    print('Message', event.id, 'changed at', event.date)


with client:
    client.run_until_disconnected()

Docs for: MessageEdited, MessageDeleted)