Telethon: delete a message


I am trying to delete a message using telegram API and Telethon.

Here is my code:

from telethon import InteractiveTelegramClient
from import InputPeerChannel
from import DeleteMessagesRequest

total_count, messages, senders = client.get_message_history(
                        chat, limit=1)
msg = messages[0]

result = client.invoke(DeleteMessagesRequest([]))

But first of all nothing happens, and second, it doesn’t look right, since 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 import DeleteMessagesRequest
Asked By: apadana



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.

from import DeleteMessagesRequest

channel = InputPeerChannel(channel_id, access_hash)
result = client.invoke(DeleteMessagesRequest(channel, []))

and it will delete the message from the channel.

Answered By: apadana
from telethon import TelegramClient

client = TelegramClient('cookie', '123', 'XXX')

id_group = -1001231231231

id_message = 3

await client.delete_messages(entity=id_group, message_ids=[id_message])
Answered By: David B.

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:

from telethon import TelegramClient

client = TelegramClient('cookiename', Apiid, 'api_hash')
id_group = -XXXX

async def getmsgs(client,id_group):
     messagesid = []
     async for message in client.iter_messages(id_group):
     return messagesid

async def delmsgs(client,id_group,list_msgs_id):
     await client.delete_messages(entity=id_group, message_ids=list_msgs_id)

with client:
     messages = client.loop.run_until_complete(getmsgs(client,id_group)) 
Answered By: Gman
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.