Skip to content

Remove obsolete service message system commands #999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
### Notes
- [:ledger: View file changes][Unreleased]
### Added
- Code snippet in `GenericmessageCommand` to keep obsolete service message system commands working.
- Static boolean property `SystemCommand::$execute_deprecated` (must be assigned before handling the request) to try and execute any deprecated system command.
### Changed
### Deprecated
### Removed
- Service message system commands, which are now handled by `GenericmessageCommand`.
### Fixed
- Boolean value for Polls gets saved correctly in MySQL DB.
### Security
Expand Down
87 changes: 86 additions & 1 deletion src/Commands/SystemCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@

namespace Longman\TelegramBot\Commands;

use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;

abstract class SystemCommand extends Command
{
/**
* @var bool Try to execute any deprecated system command.
*/
public static $execute_deprecated = false;

/**
* @{inheritdoc}
*
Expand All @@ -32,7 +39,85 @@ abstract class SystemCommand extends Command
*/
public function execute()
{
//System command, return empty ServerResponse by default
// System command, return empty ServerResponse by default
return Request::emptyResponse();
}

/**
* Method to execute any active conversation.
*
* @return ServerResponse|null
* @throws TelegramException
* @internal
*/
protected function executeActiveConversation()
{
$message = $this->getMessage();
if ($message === null) {
return null;
}

$user = $message->getFrom();
$chat = $message->getChat();
if ($user === null || $chat === null) {
return null;
}

// If a conversation is busy, execute the conversation command after handling the message.
$conversation = new Conversation($user->getId(), $chat->getId());

// Fetch conversation command if it exists and execute it.
if ($conversation->exists() && ($command = $conversation->getCommand())) {
return $this->getTelegram()->executeCommand($command);
}

return null;
}

/**
* BC helper method to execute deprecated system commands.
*
* @return ServerResponse|null
* @throws TelegramException
* @internal
*/
protected function executeDeprecatedSystemCommand()
{
$message = $this->getMessage();
if ($message === null) {
return null;
}

// List of service messages previously handled internally.
$service_message_getters = [
'newchatmembers' => 'getNewChatMembers',
'leftchatmember' => 'getLeftChatMember',
'newchattitle' => 'getNewChatTitle',
'newchatphoto' => 'getNewChatPhoto',
'deletechatphoto' => 'getDeleteChatPhoto',
'groupchatcreated' => 'getGroupChatCreated',
'supergroupchatcreated' => 'getSupergroupChatCreated',
'channelchatcreated' => 'getChannelChatCreated',
'migratefromchatid' => 'getMigrateFromChatId',
'migratetochatid' => 'getMigrateToChatId',
'pinnedmessage' => 'getPinnedMessage',
'successfulpayment' => 'getSuccessfulPayment',
];

foreach ($service_message_getters as $command => $service_message_getter) {
// Let's check if this message is a service message.
if ($message->$service_message_getter() === null) {
continue;
}

// Make sure the command exists otherwise GenericCommand would be executed.
if ($this->getTelegram()->getCommandObject($command) === null) {
break;
}

return $this->getTelegram()->executeCommand($command);
}

return null;
}
}
51 changes: 0 additions & 51 deletions src/Commands/SystemCommands/ChannelchatcreatedCommand.php

This file was deleted.

50 changes: 0 additions & 50 deletions src/Commands/SystemCommands/ChannelpostCommand.php

This file was deleted.

53 changes: 0 additions & 53 deletions src/Commands/SystemCommands/ChoseninlineresultCommand.php

This file was deleted.

51 changes: 0 additions & 51 deletions src/Commands/SystemCommands/DeletechatphotoCommand.php

This file was deleted.

50 changes: 0 additions & 50 deletions src/Commands/SystemCommands/EditedchannelpostCommand.php

This file was deleted.

Loading