Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit 75cb55d

Browse files
authored
Merge pull request #135 from DJTommek/pr/bot-command-scope
added support for BotCommandScope
2 parents c9f25d1 + 1657198 commit 75cb55d

File tree

7 files changed

+215
-0
lines changed

7 files changed

+215
-0
lines changed

examples/get-my-commands-scope.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
include __DIR__ . '/basics.php';
6+
7+
use React\EventLoop\Factory;
8+
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
9+
use unreal4u\TelegramAPI\HttpClientRequestHandler;
10+
use unreal4u\TelegramAPI\Telegram\Methods\GetMyCommands;
11+
use unreal4u\TelegramAPI\Telegram\Types\BotCommand;
12+
use unreal4u\TelegramAPI\Telegram\Types\BotCommandScope;
13+
use unreal4u\TelegramAPI\TgLog;
14+
15+
$loop = Factory::create();
16+
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
17+
18+
$scopeType = 'all_private_chats';
19+
20+
$method = new GetMyCommands();
21+
$method->scope = new BotCommandScope();
22+
$method->scope->type = $scopeType;
23+
24+
$promise = $tgLog->performApiRequest($method);
25+
$promise->then(
26+
function (TelegramTypes $response) use ($scopeType) {
27+
var_dump(sprintf('There are %d command(s) for scope "%s"', count($response->data), $scopeType));
28+
foreach($response->data as $command) {
29+
/** @var $command BotCommand */
30+
printf('/%s - %s' . PHP_EOL, $command->command, $command->description);
31+
}
32+
},
33+
function (\Exception $e) {
34+
var_dump($e->getTraceAsString());
35+
}
36+
);
37+
38+
$loop->run();

examples/get-my-commands.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
include __DIR__ . '/basics.php';
6+
7+
use React\EventLoop\Factory;
8+
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
9+
use unreal4u\TelegramAPI\HttpClientRequestHandler;
10+
use unreal4u\TelegramAPI\Telegram\Types\BotCommand;
11+
use unreal4u\TelegramAPI\TgLog;
12+
13+
$loop = Factory::create();
14+
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
15+
16+
$promise = $tgLog->performApiRequest(new \unreal4u\TelegramAPI\Telegram\Methods\GetMyCommands());
17+
$promise->then(
18+
function (TelegramTypes $response) {
19+
var_dump(sprintf('There are %d command(s) for default scope.', count($response->data)));
20+
foreach($response->data as $command) {
21+
/** @var $command BotCommand */
22+
printf('/%s - %s' . PHP_EOL, $command->command, $command->description);
23+
}
24+
},
25+
function (\Exception $e) {
26+
var_dump($e->getTraceAsString());
27+
}
28+
);
29+
30+
$loop->run();

examples/set-my-commands-scope.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
include __DIR__ . '/basics.php';
6+
7+
use React\EventLoop\Factory;
8+
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
9+
use unreal4u\TelegramAPI\HttpClientRequestHandler;
10+
use unreal4u\TelegramAPI\Telegram\Methods\SetMyCommands;
11+
use unreal4u\TelegramAPI\Telegram\Types\BotCommand;
12+
use unreal4u\TelegramAPI\Telegram\Types\BotCommandScope;
13+
use unreal4u\TelegramAPI\TgLog;
14+
15+
$loop = Factory::create();
16+
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
17+
18+
$scopeType = 'all_private_chats';
19+
20+
$method = new SetMyCommands();
21+
$method->scope = new BotCommandScope();
22+
$method->scope->type = $scopeType;
23+
24+
$command = new BotCommand();
25+
$command->command = 'help';
26+
$command->description = sprintf('description for command help in scope %s', $scopeType);
27+
$method->commands[] = $command;
28+
29+
$command = new BotCommand();
30+
$command->command = 'settings';
31+
$command->description = sprintf('%s icon and settings description in scope %s', "\u{2764}", $scopeType);
32+
$method->commands[] = $command;
33+
34+
$command = new BotCommand();
35+
$command->command = 'foo_bar';
36+
$command->description = sprintf('just another command visible only in scope %s', $scopeType);
37+
$method->commands[] = $command;
38+
39+
$promise = $tgLog->performApiRequest($method);
40+
$promise->then(
41+
function (TelegramTypes $response) use ($scopeType) {
42+
var_dump(sprintf('Commands for scope "%s" were set. Use GetMyCommands() to get list of them.', $scopeType));
43+
},
44+
function (\Exception $e) {
45+
var_dump($e->getTraceAsString());
46+
}
47+
);
48+
49+
$loop->run();

examples/set-my-commands.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
include __DIR__ . '/basics.php';
6+
7+
use React\EventLoop\Factory;
8+
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
9+
use unreal4u\TelegramAPI\HttpClientRequestHandler;
10+
use unreal4u\TelegramAPI\Telegram\Methods\SetMyCommands;
11+
use unreal4u\TelegramAPI\Telegram\Types\BotCommand;
12+
use unreal4u\TelegramAPI\Telegram\Types\BotCommandScope;
13+
use unreal4u\TelegramAPI\TgLog;
14+
15+
$loop = Factory::create();
16+
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
17+
18+
$method = new SetMyCommands();
19+
20+
$command = new BotCommand();
21+
$command->command = 'help';
22+
$command->description = 'description for command help for default scope';
23+
$method->commands[] = $command;
24+
25+
$command = new BotCommand();
26+
$command->command = 'settings';
27+
$command->description = sprintf('%s icon and settings description for default scope', "\u{2764}");
28+
$method->commands[] = $command;
29+
30+
$promise = $tgLog->performApiRequest($method);
31+
$promise->then(
32+
function (TelegramTypes $response) {
33+
var_dump('Commands for default scope were set. Use GetMyCommands() to get list of them.');
34+
},
35+
function (\Exception $e) {
36+
var_dump($e->getTraceAsString());
37+
}
38+
);
39+
40+
$loop->run();

src/Telegram/Methods/GetMyCommands.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
99
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
1010
use unreal4u\TelegramAPI\InternalFunctionality\TelegramResponse;
11+
use unreal4u\TelegramAPI\Telegram\Types\BotCommandScope;
1112
use unreal4u\TelegramAPI\Telegram\Types\Custom\BotCommandArray;
1213

1314
/**
@@ -20,6 +21,12 @@
2021
*/
2122
class GetMyCommands extends TelegramMethods
2223
{
24+
/**
25+
* A JSON-serialized object, describing scope of users. Defaults to BotCommandScopeDefault.
26+
* @var BotCommandScope
27+
*/
28+
public $scope;
29+
2330
public static function bindToObject(TelegramResponse $data, LoggerInterface $logger): TelegramTypes
2431
{
2532
return new BotCommandArray($data->getResult(), $logger);
@@ -29,4 +36,12 @@ public function getMandatoryFields(): array
2936
{
3037
return [];
3138
}
39+
40+
public function performSpecialConditions(): TelegramMethods
41+
{
42+
if ($this->scope) {
43+
$this->scope = json_encode($this->scope);
44+
}
45+
return parent::performSpecialConditions();
46+
}
3247
}

src/Telegram/Methods/SetMyCommands.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
1010
use unreal4u\TelegramAPI\InternalFunctionality\TelegramResponse;
1111
use unreal4u\TelegramAPI\Telegram\Types\BotCommand;
12+
use unreal4u\TelegramAPI\Telegram\Types\BotCommandScope;
1213
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultBoolean;
1314

1415
/**
@@ -28,6 +29,14 @@ class SetMyCommands extends TelegramMethods
2829
*/
2930
public $commands;
3031

32+
/**
33+
* A JSON-serialized object, describing scope of users for which the commands are relevant. Defaults to
34+
* BotCommandScopeDefault.
35+
*
36+
* @var BotCommandScope
37+
*/
38+
public $scope;
39+
3140
public static function bindToObject(TelegramResponse $data, LoggerInterface $logger): TelegramTypes
3241
{
3342
return new ResultBoolean($data->getResultBoolean(), $logger);
@@ -43,6 +52,9 @@ public function getMandatoryFields(): array
4352
public function performSpecialConditions(): TelegramMethods
4453
{
4554
$this->commands = json_encode($this->commands);
55+
if ($this->scope) {
56+
$this->scope = json_encode($this->scope);
57+
}
4658
return parent::performSpecialConditions();
4759
}
4860
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace unreal4u\TelegramAPI\Telegram\Types;
6+
7+
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
8+
9+
/**
10+
* This object represents the scope to which bot commands are applied. Currently, the following 7 scopes are supported:
11+
* BotCommandScopeDefault
12+
* BotCommandScopeAllPrivateChats
13+
* BotCommandScopeAllGroupChats
14+
* BotCommandScopeAllChatAdministrators
15+
* BotCommandScopeChat
16+
* BotCommandScopeChatAdministrators
17+
* BotCommandScopeChatMember
18+
*
19+
* Objects defined as-is June 2021, Bot API v5.3
20+
*
21+
* @see https://core.telegram.org/bots/api#botcommandscope
22+
*/
23+
class BotCommandScope extends TelegramTypes
24+
{
25+
/**
26+
* Scope type
27+
*
28+
* @var string
29+
*/
30+
public $type = 'default';
31+
}

0 commit comments

Comments
 (0)