Skip to content

Commit

Permalink
Merge pull request #314 from TelegramBot/feature/botCommands
Browse files Browse the repository at this point in the history
Add BotCommand type, setMyCommands and getMyCommands methods
  • Loading branch information
MyZik authored Jan 23, 2021
2 parents d709fb0 + ae0c41b commit dafad7c
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/BotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TelegramBot\Api;

use TelegramBot\Api\Types\ArrayOfBotCommand;
use TelegramBot\Api\Types\ArrayOfChatMemberEntity;
use TelegramBot\Api\Types\ArrayOfMessageEntity;
use TelegramBot\Api\Types\ArrayOfMessages;
Expand Down Expand Up @@ -1138,6 +1139,39 @@ public function answerCallbackQuery($callbackQueryId, $text = null, $showAlert =
]);
}

/**
* Use this method to change the list of the bot's commands. Returns True on success.
*
* @param $commands
*
* @return mixed
* @throws Exception
* @throws HttpException
* @throws InvalidJsonException
*/
public function setMyCommands($commands)
{
return $this->call(
'setMyCommands',
[
'commands' => json_encode($commands)
]
);
}

/**
* Use this method to get the current list of the bot's commands. Requires no parameters.
* Returns Array of BotCommand on success.
*
* @return mixed
* @throws Exception
* @throws HttpException
* @throws InvalidJsonException
*/
public function getMyCommands()
{
return ArrayOfBotCommand::fromResponse($this->call('getMyCommands'));
}

/**
* Use this method to edit text messages sent by the bot or via the bot
Expand Down
16 changes: 16 additions & 0 deletions src/Types/ArrayOfBotCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace TelegramBot\Api\Types;

abstract class ArrayOfBotCommand
{
public static function fromResponse($data)
{
$arrayOfBotCommand = [];
foreach ($data as $botCommand) {
$arrayOfBotCommand[] = BotCommand::fromResponse($botCommand);
}

return $arrayOfBotCommand;
}
}
72 changes: 72 additions & 0 deletions src/Types/BotCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace TelegramBot\Api\Types;

use TelegramBot\Api\BaseType;
use TelegramBot\Api\TypeInterface;

class BotCommand extends BaseType implements TypeInterface
{
/**
* {@inheritdoc}
*
* @var array
*/
static protected $requiredParams = ['command', 'description'];

/**
* {@inheritdoc}
*
* @var array
*/
static protected $map = [
'command' => true,
'description' => true,
];

/**
* Text of the command, 1-32 characters. Can contain only lowercase English letters, digits and underscores.
*
* @var string
*/
protected $command;

/**
* Description of the command, 3-256 characters.
*
* @var string
*/
protected $description;

/**
* @return string
*/
public function getCommand()
{
return $this->command;
}

/**
* @param string $command
*/
public function setCommand($command)
{
$this->command = $command;
}

/**
* @return string
*/
public function getDescription()
{
return $this->description;
}

/**
* @param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
}
50 changes: 50 additions & 0 deletions tests/BotCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace TelegramBot\Api\Test;

use TelegramBot\Api\Types\BotCommand;

class BotCommandTest extends \PHPUnit_Framework_TestCase
{
public function testSetCommand()
{
$item = new BotCommand();
$item->setCommand('start');
$this->assertAttributeEquals('start', 'command', $item);
}

public function testGetCommand()
{
$item = new BotCommand();
$item->setCommand('start');
$this->assertEquals('start', $item->getCommand());
}

public function testSetDescription()
{
$item = new BotCommand();
$item->setDescription('This is a start command!');
$this->assertAttributeEquals('This is a start command!', 'description', $item);
}

public function testGetDescription()
{
$item = new BotCommand();
$item->setDescription('This is a start command!');
$this->assertEquals('This is a start command!', $item->getDescription());
}

public function testFromResponse()
{
$botCommand = BotCommand::fromResponse(
[
'command' => 'start',
'description' => 'This is a start command!',
]
);

$this->assertInstanceOf('\TelegramBot\Api\Types\BotCommand', $botCommand);
$this->assertEquals('start', $botCommand->getCommand());
$this->assertEquals('This is a start command!', $botCommand->getDescription());
}
}

0 comments on commit dafad7c

Please sign in to comment.