Ebrahim
Ebrahim

Reputation: 1818

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

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

I did not find any functional tutorial in this regard,

Upvotes: 34

Views: 185570

Answers (8)

Mark Shot
Mark Shot

Reputation: 329

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}");
}

Upvotes: 0

Francois
Francois

Reputation: 10631

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)

Upvotes: 0

ladan.sahbaee
ladan.sahbaee

Reputation: 11

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")

Upvotes: 1

Sanjay Saravanan
Sanjay Saravanan

Reputation: 21

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);
}

Upvotes: 2

Darry Castro
Darry Castro

Reputation: 1

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

Upvotes: -2

fox.cpp
fox.cpp

Reputation: 396

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"}

Upvotes: 35

ihoru
ihoru

Reputation: 1980

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://stackoverflow.com/a/43965602/1140438

Upvotes: 42

Moradnejad
Moradnejad

Reputation: 3653

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."

Upvotes: 0

Related Questions