Skip to content

Use class constants instead of strings for 'generic' and 'genericmessage' #1074

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
merged 2 commits into from
Apr 13, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
### Notes
- [:ledger: View file changes][Unreleased]
### Added
- Replaced 'generic' and 'genericmessage' strings with Telegram::GENERIC_COMMAND and Telegram::GENERIC_MESSAGE_COMMAND constants (@1int)
### Changed
### Deprecated
### Removed
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/SystemCommands/GenericmessageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;

/**
* Generic message command
Expand All @@ -24,7 +25,7 @@ class GenericmessageCommand extends SystemCommand
/**
* @var string
*/
protected $name = 'genericmessage';
protected $name = Telegram::GENERIC_MESSAGE_COMMAND;

/**
* @var string
Expand Down
16 changes: 13 additions & 3 deletions src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ class Telegram
*/
protected $last_update_id = null;

/**
* The command to be executed when there's a new message update and nothing more suitable is found
*/
const GENERIC_MESSAGE_COMMAND = 'genericmessage';

/**
* The command to be executed by default (when no other relevant commands are applicable)
*/
const GENERIC_COMMAND = 'generic';

/**
* Telegram constructor.
*
Expand Down Expand Up @@ -455,7 +465,7 @@ public function processUpdate(Update $update)
$this->getCommandsList();

//If all else fails, it's a generic message.
$command = 'genericmessage';
$command = self::GENERIC_MESSAGE_COMMAND;

$update_type = $this->update->getUpdateType();
if ($update_type === 'message') {
Expand Down Expand Up @@ -506,12 +516,12 @@ public function executeCommand($command)

if (!$command_obj || !$command_obj->isEnabled()) {
//Failsafe in case the Generic command can't be found
if ($command === 'generic') {
if ($command === self::GENERIC_COMMAND) {
throw new TelegramException('Generic command missing!');
}

//Handle a generic command or non existing one
$this->last_command_response = $this->executeCommand('generic');
$this->last_command_response = $this->executeCommand(self::GENERIC_COMMAND);
} else {
//execute() method is executed after preExecute()
//This is to prevent executing a DB query without a valid connection
Expand Down