Skip to content

Commit dafad7c

Browse files
authored
Merge pull request #314 from TelegramBot/feature/botCommands
Add BotCommand type, setMyCommands and getMyCommands methods
2 parents d709fb0 + ae0c41b commit dafad7c

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

src/BotApi.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TelegramBot\Api;
44

5+
use TelegramBot\Api\Types\ArrayOfBotCommand;
56
use TelegramBot\Api\Types\ArrayOfChatMemberEntity;
67
use TelegramBot\Api\Types\ArrayOfMessageEntity;
78
use TelegramBot\Api\Types\ArrayOfMessages;
@@ -1138,6 +1139,39 @@ public function answerCallbackQuery($callbackQueryId, $text = null, $showAlert =
11381139
]);
11391140
}
11401141

1142+
/**
1143+
* Use this method to change the list of the bot's commands. Returns True on success.
1144+
*
1145+
* @param $commands
1146+
*
1147+
* @return mixed
1148+
* @throws Exception
1149+
* @throws HttpException
1150+
* @throws InvalidJsonException
1151+
*/
1152+
public function setMyCommands($commands)
1153+
{
1154+
return $this->call(
1155+
'setMyCommands',
1156+
[
1157+
'commands' => json_encode($commands)
1158+
]
1159+
);
1160+
}
1161+
1162+
/**
1163+
* Use this method to get the current list of the bot's commands. Requires no parameters.
1164+
* Returns Array of BotCommand on success.
1165+
*
1166+
* @return mixed
1167+
* @throws Exception
1168+
* @throws HttpException
1169+
* @throws InvalidJsonException
1170+
*/
1171+
public function getMyCommands()
1172+
{
1173+
return ArrayOfBotCommand::fromResponse($this->call('getMyCommands'));
1174+
}
11411175

11421176
/**
11431177
* Use this method to edit text messages sent by the bot or via the bot

src/Types/ArrayOfBotCommand.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace TelegramBot\Api\Types;
4+
5+
abstract class ArrayOfBotCommand
6+
{
7+
public static function fromResponse($data)
8+
{
9+
$arrayOfBotCommand = [];
10+
foreach ($data as $botCommand) {
11+
$arrayOfBotCommand[] = BotCommand::fromResponse($botCommand);
12+
}
13+
14+
return $arrayOfBotCommand;
15+
}
16+
}

src/Types/BotCommand.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace TelegramBot\Api\Types;
4+
5+
use TelegramBot\Api\BaseType;
6+
use TelegramBot\Api\TypeInterface;
7+
8+
class BotCommand extends BaseType implements TypeInterface
9+
{
10+
/**
11+
* {@inheritdoc}
12+
*
13+
* @var array
14+
*/
15+
static protected $requiredParams = ['command', 'description'];
16+
17+
/**
18+
* {@inheritdoc}
19+
*
20+
* @var array
21+
*/
22+
static protected $map = [
23+
'command' => true,
24+
'description' => true,
25+
];
26+
27+
/**
28+
* Text of the command, 1-32 characters. Can contain only lowercase English letters, digits and underscores.
29+
*
30+
* @var string
31+
*/
32+
protected $command;
33+
34+
/**
35+
* Description of the command, 3-256 characters.
36+
*
37+
* @var string
38+
*/
39+
protected $description;
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getCommand()
45+
{
46+
return $this->command;
47+
}
48+
49+
/**
50+
* @param string $command
51+
*/
52+
public function setCommand($command)
53+
{
54+
$this->command = $command;
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
public function getDescription()
61+
{
62+
return $this->description;
63+
}
64+
65+
/**
66+
* @param string $description
67+
*/
68+
public function setDescription($description)
69+
{
70+
$this->description = $description;
71+
}
72+
}

tests/BotCommandTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace TelegramBot\Api\Test;
4+
5+
use TelegramBot\Api\Types\BotCommand;
6+
7+
class BotCommandTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testSetCommand()
10+
{
11+
$item = new BotCommand();
12+
$item->setCommand('start');
13+
$this->assertAttributeEquals('start', 'command', $item);
14+
}
15+
16+
public function testGetCommand()
17+
{
18+
$item = new BotCommand();
19+
$item->setCommand('start');
20+
$this->assertEquals('start', $item->getCommand());
21+
}
22+
23+
public function testSetDescription()
24+
{
25+
$item = new BotCommand();
26+
$item->setDescription('This is a start command!');
27+
$this->assertAttributeEquals('This is a start command!', 'description', $item);
28+
}
29+
30+
public function testGetDescription()
31+
{
32+
$item = new BotCommand();
33+
$item->setDescription('This is a start command!');
34+
$this->assertEquals('This is a start command!', $item->getDescription());
35+
}
36+
37+
public function testFromResponse()
38+
{
39+
$botCommand = BotCommand::fromResponse(
40+
[
41+
'command' => 'start',
42+
'description' => 'This is a start command!',
43+
]
44+
);
45+
46+
$this->assertInstanceOf('\TelegramBot\Api\Types\BotCommand', $botCommand);
47+
$this->assertEquals('start', $botCommand->getCommand());
48+
$this->assertEquals('This is a start command!', $botCommand->getDescription());
49+
}
50+
}

0 commit comments

Comments
 (0)