Make use of the new Keyboard entities.

Some typos, code cleanup.
parent 0001e687
...@@ -33,8 +33,6 @@ class KeyboardCommand extends UserCommand ...@@ -33,8 +33,6 @@ class KeyboardCommand extends UserCommand
*/ */
public function execute() public function execute()
{ {
$chat_id = $this->getMessage()->getChat()->getId();
//Keyboard examples //Keyboard examples
/** @var Keyboard[] $keyboards */ /** @var Keyboard[] $keyboards */
$keyboards = []; $keyboards = [];
...@@ -77,6 +75,7 @@ class KeyboardCommand extends UserCommand ...@@ -77,6 +75,7 @@ class KeyboardCommand extends UserCommand
->setOneTimeKeyboard(true) ->setOneTimeKeyboard(true)
->setSelective(false); ->setSelective(false);
$chat_id = $this->getMessage()->getChat()->getId();
$data = [ $data = [
'chat_id' => $chat_id, 'chat_id' => $chat_id,
'text' => 'Press a Button:', 'text' => 'Press a Button:',
......
...@@ -23,7 +23,7 @@ class MarkdownCommand extends UserCommand ...@@ -23,7 +23,7 @@ class MarkdownCommand extends UserCommand
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $name = 'markdown'; protected $name = 'markdown';
protected $description = 'Print Markdown tesxt'; protected $description = 'Print Markdown text';
protected $usage = '/markdown'; protected $usage = '/markdown';
protected $version = '1.0.1'; protected $version = '1.0.1';
/**#@-*/ /**#@-*/
......
...@@ -12,7 +12,7 @@ namespace Longman\TelegramBot\Commands\UserCommands; ...@@ -12,7 +12,7 @@ namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand; use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Conversation; use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Entities\ReplyKeyboardHide; use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
/** /**
...@@ -85,7 +85,7 @@ class CancelCommand extends UserCommand ...@@ -85,7 +85,7 @@ class CancelCommand extends UserCommand
{ {
return Request::sendMessage( return Request::sendMessage(
[ [
'reply_markup' => new ReplyKeyboardHide(['selective' => true]), 'reply_markup' => Keyboard::hide(['selective' => true]),
'chat_id' => $this->getMessage()->getChat()->getId(), 'chat_id' => $this->getMessage()->getChat()->getId(),
'text' => $text, 'text' => $text,
] ]
......
...@@ -15,7 +15,7 @@ namespace Longman\TelegramBot\Entities; ...@@ -15,7 +15,7 @@ namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
/** /**
* Class ReplyKeyboardMarkup * Class Keyboard
* *
* @link https://core.telegram.org/bots/api#replykeyboardmarkup * @link https://core.telegram.org/bots/api#replykeyboardmarkup
* *
......
...@@ -149,19 +149,16 @@ class Message extends Entity ...@@ -149,19 +149,16 @@ class Message extends Entity
*/ */
public function getFullCommand() public function getFullCommand()
{ {
if (strpos($this->text, '/') === 0) { $text = $this->getProperty('text');
$no_EOL = strtok($this->text, PHP_EOL); if (strpos($text, '/') === 0) {
$no_space = strtok($this->text, ' '); $no_EOL = strtok($text, PHP_EOL);
$no_space = strtok($text, ' ');
//try to understand which separator \n or space divide /command from text //try to understand which separator \n or space divide /command from text
if (strlen($no_space) < strlen($no_EOL)) { return strlen($no_space) < strlen($no_EOL) ? $no_space : $no_EOL;
return $no_space;
} else {
return $no_EOL;
}
} else {
return null;
} }
return null;
} }
/** /**
...@@ -171,8 +168,9 @@ class Message extends Entity ...@@ -171,8 +168,9 @@ class Message extends Entity
*/ */
public function getCommand() public function getCommand()
{ {
if (!empty($this->command)) { $command = $this->getProperty('command');
return $this->command; if (!empty($command)) {
return $command;
} }
$cmd = $this->getFullCommand(); $cmd = $this->getFullCommand();
...@@ -185,11 +183,11 @@ class Message extends Entity ...@@ -185,11 +183,11 @@ class Message extends Entity
if (isset($split_cmd[1])) { if (isset($split_cmd[1])) {
//command is followed by name check if is addressed to me //command is followed by name check if is addressed to me
if (strtolower($split_cmd[1]) === strtolower($this->bot_name)) { if (strtolower($split_cmd[1]) === strtolower($this->bot_name)) {
return $this->command = $split_cmd[0]; return $split_cmd[0];
} }
} else { } else {
//command is not followed by name //command is not followed by name
return $this->command = $cmd; return $cmd;
} }
} }
...@@ -205,7 +203,7 @@ class Message extends Entity ...@@ -205,7 +203,7 @@ class Message extends Entity
*/ */
public function getText($without_cmd = false) public function getText($without_cmd = false)
{ {
$text = $this->text; $text = $this->getProperty('text');
if ($without_cmd && $command = $this->getFullCommand()) { if ($without_cmd && $command = $this->getFullCommand()) {
if (strlen($command) + 1 < strlen($text)) { if (strlen($command) + 1 < strlen($text)) {
...@@ -225,13 +223,9 @@ class Message extends Entity ...@@ -225,13 +223,9 @@ class Message extends Entity
*/ */
public function botAddedInChat() public function botAddedInChat()
{ {
if (!empty($this->new_chat_member)) { $member = $this->getNewChatMember();
if ($this->new_chat_member->getUsername() === $this->getBotName()) {
return true;
}
}
return false; return $member !== null && $member->getUsername() === $this->getBotName();
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment