Core: How to remove service messages in channels?

Created on 24 Jun 2020  路  4Comments  路  Source: php-telegram-bot/core

I want to remove this service messages from my channel:

bot pinned 芦new message...禄

Most helpful comment

This is quite easy actually!

When the bot (or any other user) pins a message, a new message object is sent to the bot, which is then handled by the GenericmessageCommand.

So in the execute method you can put the following:

$message = $this->getMessage();
if ($message && $message->getPinnedMessage()) {
    return Request::deleteMessage([
        'chat_id'    => $message->getChat()->getId(),
        'message_id' => $message->getMessageId(),
    ]);
}

All 4 comments

This is quite easy actually!

When the bot (or any other user) pins a message, a new message object is sent to the bot, which is then handled by the GenericmessageCommand.

So in the execute method you can put the following:

$message = $this->getMessage();
if ($message && $message->getPinnedMessage()) {
    return Request::deleteMessage([
        'chat_id'    => $message->getChat()->getId(),
        'message_id' => $message->getMessageId(),
    ]);
}

This is quite easy actually!

When the bot (or any other user) pins a message, a new message object is sent to the bot, which is then handled by the GenericmessageCommand.

So in the execute method you can put the following:

$message = $this->getMessage();
if ($message && $message->getPinnedMessage()) {
    return Request::deleteMessage([
        'chat_id'    => $message->getChat()->getId(),
        'message_id' => $message->getMessageId(),
    ]);
}

Thanks @noplanman ...

It seems that the deleteMessage never executed...

Request::pinChatMessage([
    'chat_id' => $channel_id,
    'message_id' => $send_message_id,
    'disable_notification' => false,
]);

I put following to GenericmessageCommand's execute method and run above in SurveyCommand, but nothing happened after message pin!

$message = $this->getMessage();
$chat_id = $message->getChat()->getId();

if ($message && $message->getPinnedMessage()) {
    Request::sendMessage([
        'chat_id' => 'My Telegram user ID',
        'text' => print_r($message, 1),
    ]);

    return Request::deleteMessage([
        'chat_id'    => $chat_id,
        'message_id' => $message->getMessageId(),
    ]);
}

You might have to create ChannelpostCommand and put that code there - replace getMessage() with getChannelPost().

@WISTFUL12 I assume it works now and we can close this off?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sineverba picture sineverba  路  3Comments

Bl0ck154 picture Bl0ck154  路  3Comments

NabiKAZ picture NabiKAZ  路  4Comments

ttvd94 picture ttvd94  路  4Comments

TheSleepySlee picture TheSleepySlee  路  3Comments