I am trying to delete a message using telegram API and Telethon.
Here is my code:
from telethon import InteractiveTelegramClient
from telethon.tl.types.input_peer_channel import InputPeerChannel
from telethon.tl.functions.messages.delete_messages import DeleteMessagesRequest
#...
total_count, messages, senders = client.get_message_history(
chat, limit=1)
msg = messages[0]
result = client.invoke(DeleteMessagesRequest([msg.id]))
But first of all nothing happens, and second, it doesn’t look right, since msg.id is like 5 or 220 and it doesn’t look like a unique number.
Here is the message:
msg: (message (ID: 0xc09bexxx) = (out=None, mentioned=None, media_unread=None, silent=None, post=True, id=5, from_id=None, to_id=(peerChannel (ID: 0xbdddexxx) = (channel_id=1234)), fwd_from=None, via_bot_id=None, reply_to_msg_id=None, date=2017-06-14 14:39:23, message=test33, media=None, reply_markup=None, entities=None, views=1, edit_date=None))
I also tried with the hex number 0xc09bexxx but that gives an exception.
So how can I delete a message in a channel?
So far I looked at this github issue to get started with the delete message. My guess is that maybe the following import is not the right one and I should import the version in the Channels package, which gets a channel id and a message id?
from telethon.tl.functions.messages.delete_messages import DeleteMessagesRequest
3
Answers
Using the other delete from channels package I was able to get the delete message working, but I am still curious to know how to get the delete from messages.delete_messages working.
and it will delete the message from the channel.
For example if u want to obtain all the msgs-ids in the channel and after of it delete all messages… this code works to me: