Arjun Starz
Arjun Starz

Reputation: 23

How to delete a destination telegram message if the source message is deleted

I am creating a python program using Telethon to find a way to delete a message in the destination channel once the source message is deleted.

For eg:
A -- source channel
B -- destination channel

A posts a message X which gets forwarded to destination channel B. After sometime, A deletes message X, B should also delete that message.

My code looks like this:

@client.on(events.MessageDeleted)
async def edit_message_bot(event):
    ...

But the problem is when I use this method, msg_id from event is different for source and destination channel and hence couldn't find a way to delete it.

Please help

Upvotes: 0

Views: 874

Answers (1)

andrewyazura
andrewyazura

Reputation: 50

I found source code for the MessageDeleted event and the documentation says:

Telegram does not send information about where a message was deleted if it occurs in private conversations with other users or in small group chats, because message IDs are unique and you can identify the chat with the message ID alone if you saved it previously.

Telethon does not save information of where messages occur, so it cannot know in which chat a message was deleted (this will only work in channels, where the channel ID is present).

This means that the chats= parameter will not work reliably, unless you intend on working with channels and super-groups only.

In your case, you should get chats parameter, which can help. Also, you should use deleted_ids, not msg_id. Link

I think you should store the message-id of the original message and id of the message in the destination channel so you can easily find it. So you'll have something like a dictionary where the original id and a new one are stored together.

Upvotes: 1

Related Questions