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

Commit 1657198

Browse files
committed
Bot command scope: added examples for *MyCommands
1 parent 8cfe679 commit 1657198

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-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();

0 commit comments

Comments
 (0)