Telegram Bot How to delete or remove a message or media from a channel or group
Asked Answered
O

8

34

I want to know an example of removing message or file like a photo

I did not find any functional tutorial in this regard,

One answered 8/2, 2016 at 12:32 Comment(1)
Now it's available to do!Modena
M
40

There is no such functionality in Telegram Bot API right now.

UPD 2017-05-19: There is an official method deleteMessage, more info: https://core.telegram.org/bots/api#deletemessage

https://mcmap.net/q/435239/-telegram-bot-how-to-delete-or-remove-a-message-or-media-from-a-channel-or-group

Modena answered 8/2, 2016 at 14:24 Comment(10)
There is method for deleting messages not with bot API: core.telegram.org/method/messages.deleteMessagesModena
Thanks @ihoru, does that mean the bot can't delete a for example incorrect post? Are you sure about that?One
is there any solution in this regard?One
it is not even possible other administrators to delete the message from the channel is there any solution?One
Channel's administrator can delete message posted by bot to the channel, but not from private chat with user.Modena
With telegram bot api v2.1 can we do this or it is still un available?Ama
@MohammadRezaEsmaeilzadeh It's still unavailable.Resemblance
@Modena I couldn't delete the replyed and forwarded messages with this method! but deleting usual messages are totally ok, can you help me bro!?Cullis
@Cullis give us more information. What error do you get?Modena
@Modena thanks bro, the error was gone! but I didn't understand what was the problem! :))Cullis
L
34

There is an official support of deleteMessage method in Bot API 3.0. More details here: https://core.telegram.org/bots/api#deletemessage

https://api.telegram.org/botTOKEN/deleteMessage?chat_id=CID&message_id=MID

As you can see there are two arguments: chat_id and message_id.

You can remove bot's messages or other messages (if bot is admin) except service messages (such as join/leave messages).

  1. On success, it will return following JSON object: {"ok":true,"result":true}.

  2. If you are trying to remove service message or other user's message, but bot is not an admin: {"ok":false,"error_code":400,"description":"Bad Request: message can't be deleted"}.

  3. If you are trying to remove non-existent message or its already deleted: {"ok":false,"error_code":400,"description":"Bad Request: message to delete not found"}

Lamellicorn answered 14/5, 2017 at 15:19 Comment(1)
Good answer. Could you give an example how to manage those three cases in javaScriptGordan
C
2
Kindly check with the below code snippet!, the below code have worked for me!
String chatId = String.valueOf(callbackQuery.getMessage().getChatId());
Integer messageId = callbackQuery.getMessage().getMessageId();
DeleteMessage deleteMessage = new DeleteMessage(chatId, messageId);
try {
  execute(deleteMessage);
}catch(TelegramApiException tae) {
  throw new RuntimeException(tae);
}
Commons answered 29/6, 2021 at 2:42 Comment(0)
S
1

you can forward message and save message id, and then remove that message. if you can do it, your message exist.

do it:

try:
   mes=bot.forward_message(chat_id=?,from_chat_id=?,message_id=?)
   bot.delete_message(chat_id=?,message_id=mes.id)
except:
   print("your message deleted")
Scriven answered 20/8, 2021 at 18:46 Comment(0)
R
0

There are two methods in bot api that let you to edit a message: editMessageText and editMessageCaption. It is not ideal, but you can use it as an alternative.

For example by editing the message to:

"This message is unavailable."

Resemblance answered 29/4, 2017 at 8:22 Comment(0)
C
0

Using python, if you have a CommandHandler() you can read the chat_id and message_id like so:

dispatcher.add_handler(CommandHandler("start", handler_start))

def handler_start(update: Update, context: CallbackContext):
    chat_id = update.message.chat_id
    message_id = update.message._id_attrs[0]
    context.bot.delete_message(chat_id, message_id)
Cardoon answered 30/5, 2022 at 11:57 Comment(0)
U
0

If on php. I send message. Get response from it (message id of bot) And use deleteMessage

<?php
$botToken = "yourBotToken";
$botAPI = "https://api.telegram.org/bot" . $botToken;

$update = json_decode(file_get_contents('php://input'), TRUE);
$msg = $update['message']['text'];
if ($msg == '/start') {
$data = http_build_query([
        'text' => "test message (delete this)",
        'chat_id' => $update['message']['chat']['id'],
]);
$send = file_get_contents($botAPI . "/sendMessage?{$data}");

$response = json_decode($send), true); // decode response
$message_id = $response['result']['message_id']; // get bots message

// Deleting message
$data_del = http_build_query([
    'chat_id' => $update['message']['chat']['id'],
    'message_id' => $message_id,
]);
    file_get_contents($botAPI . "/deleteMessage?{$data_del}");
}
Urbanism answered 1/8, 2022 at 12:54 Comment(0)
D
-2

https://api.telegram.org/botTOKEN/deleteMessage?chat_id=CID&message_id=MID

Example https://api.telegram.org/bot123456789:zzzzzzzzxxxxxxxxxxyyyyyyyyyy/deleteMessage?chat_id=123456789&message_id=123456,

It is important that the id of the message temine with a (comma) (,) and you can see it in the json when you send the message

Demoniac answered 10/4, 2019 at 2:40 Comment(1)
Is this any different than the existing answer by fox.cpp?Kutenai

© 2022 - 2024 — McMap. All rights reserved.