Skip to content

Editing and deleting messages

Kyle2142 edited this page Jun 15, 2018 · 3 revisions

Editing

Lets start with messages to edit:

$text_msg = $bot->sendMessage(123, 'test'); //convenience function exists
$media_msg = $bot->api->sendDocument([ //raw api
    'chat_id'=>123,
    'document'=>new CURLFile('./example.txt')
]); 

You can change the text or inline keyboard of a message, or the caption of a message with media:

$bot->editMessageText(
    $text_msg->chat->id,            //chat_id
    $text_msg->message_id,          //message_id
    "This is an edited message!");  //text

$bot->api->editMessageCaption([
    'chat_id' => $media_msg->chat->id,
    'message_id' => $media_msg->message_id,
    'caption' => "This media caption has been edited!"
]);

Deleting

If we decide that we want to delete a message, there are a few points to note, taken from the botAPI:

  • A message can only be deleted if it was sent less than 48 hours ago.
  • Bots can delete outgoing messages in groups and supergroups.
  • Bots granted can_post_messages permissions can delete outgoing messages in channels.
  • If the bot is an administrator of a group, it can delete any message there.
  • If the bot has the can_delete_messages permission in a supergroup or a channel, it can delete any message there. Returns True on success.
$bot->deleteMessage(12345678, 5); //delete 5th message in the private chat with 12345678

$bot->deleteMessage('@examplechannel', 506); //deletes the 506th message in @examplechannel. 

We can use the result of previous messages to delete them:

$bot->deleteMessage($text_msg->chat->id, $text_msg->message_id); //deletes the message we sent and edited before