Repository: defstudio/telegraph (php)

Issue:
Implement `getMyCommands` method
ref https://core.telegram.org/bots/api#getmycommands


Pull request description:
implement `getMyCommands` method
In this PR I have implemented `getMyCommands` method. Resolves #193

```php
$response = Telegraph::getMyCommands()->send();

dd($response->json('result'));
```
## Result
```
array:2 [▼
  0 => array:2 [▼
    "command" => "command1"
    "description" => "command 1 description"
  ]
  1 => array:2 [▼
    "command" => "command2"
    "description" => "command 2 description"
  ]
]
```

Patch:
diff --git a/docs/content/en/features/telegram-api-calls.md b/docs/content/en/features/telegram-api-calls.md
index afea082e..fc6715a2 100644
--- a/docs/content/en/features/telegram-api-calls.md
+++ b/docs/content/en/features/telegram-api-calls.md
@@ -224,6 +224,15 @@ Telegraph::registerBotCommands([
 ])->send();
 ```
 
+## getRegisteredCommands
+
+retrieve bot's registered commands.
+
+```php
+$response = Telegraph::getRegisteredCommands()->send();
+$response->json('result')
+```
+
 ## registerWebhook
 
 register a webhook for the active bot
diff --git a/src/Concerns/HasBotsAndChats.php b/src/Concerns/HasBotsAndChats.php
index 6f918515..7a5b57aa 100644
--- a/src/Concerns/HasBotsAndChats.php
+++ b/src/Concerns/HasBotsAndChats.php
@@ -11,7 +11,6 @@ namespace DefStudio\Telegraph\Concerns;
 use DefStudio\Telegraph\DTO\Attachment;
 use DefStudio\Telegraph\Enums\ChatActions;
 use DefStudio\Telegraph\Enums\ChatAdminPermissions;
-use DefStudio\Telegraph\Exceptions\BotCommandException;
 use DefStudio\Telegraph\Exceptions\ChatSettingsException;
 use DefStudio\Telegraph\Exceptions\FileException;
 use DefStudio\Telegraph\Exceptions\TelegraphException;
@@ -155,46 +154,6 @@ trait HasBotsAndChats
         return $telegraph;
     }
 
-    /**
-     * @param array<string, string> $commands
-     */
-    public function registerBotCommands(array $commands): Telegraph
-    {
-        $telegraph = clone $this;
-
-        $telegraph->endpoint = self::ENDPOINT_REGISTER_BOT_COMMANDS;
-
-        if (count($commands) > 100) {
-            throw BotCommandException::tooManyCommands();
-        }
-
-        $telegraph->data['commands'] = collect($commands)->map(function (string $description, string $command) {
-            if (strlen($command) > 32) {
-                throw BotCommandException::longCommand($command);
-            }
-
-            if (!preg_match('/[a-z0-9_]+/', $command)) {
-                throw BotCommandException::invalidCommand($command);
-            }
-
-            return [
-                'command' => $command,
-                'description' => $description,
-            ];
-        })->values()->toArray();
-
-        return $telegraph;
-    }
-
-    public function unregisterBotCommands(): Telegraph
-    {
-        $telegraph = clone $this;
-
-        $telegraph->endpoint = self::ENDPOINT_UNREGISTER_BOT_COMMANDS;
-
-        return $telegraph;
-    }
-
     public function chatAction(string $action): Telegraph
     {
         $telegraph = clone $this;
diff --git a/src/Concerns/InteractsWithCommands.php b/src/Concerns/InteractsWithCommands.php
new file mode 100644
index 00000000..6bdbcad3
--- /dev/null
+++ b/src/Concerns/InteractsWithCommands.php
@@ -0,0 +1,65 @@
+<?php
+
+
+/** @noinspection PhpUnhandledExceptionInspection */
+
+namespace DefStudio\Telegraph\Concerns;
+
+use DefStudio\Telegraph\Telegraph;
+use DefStudio\Telegraph\Exceptions\BotCommandException;
+
+/**
+ * @mixin Telegraph
+ */
+
+trait InteractsWithCommands
+{
+    public function getRegisteredCommands(): Telegraph
+    {
+        $telegraph = clone $this;
+
+        $telegraph->endpoint = self::ENDPOINT_GET_REGISTERED_BOT_COMMANDS;
+
+        return $telegraph;
+    }
+
+    /**
+     * @param array<string, string> $commands
+     */
+    public function registerBotCommands(array $commands): Telegraph
+    {
+        $telegraph = clone $this;
+
+        $telegraph->endpoint = self::ENDPOINT_REGISTER_BOT_COMMANDS;
+
+        if (count($commands) > 100) {
+            throw BotCommandException::tooManyCommands();
+        }
+
+        $telegraph->data['commands'] = collect($commands)->map(function (string $description, string $command) {
+            if (strlen($command) > 32) {
+                throw BotCommandException::longCommand($command);
+            }
+
+            if (!preg_match('/[a-z0-9_]+/', $command)) {
+                throw BotCommandException::invalidCommand($command);
+            }
+
+            return [
+                'command' => $command,
+                'description' => $description,
+            ];
+        })->values()->toArray();
+
+        return $telegraph;
+    }
+
+    public function unregisterBotCommands(): Telegraph
+    {
+        $telegraph = clone $this;
+
+        $telegraph->endpoint = self::ENDPOINT_UNREGISTER_BOT_COMMANDS;
+
+        return $telegraph;
+    }
+}
diff --git a/src/Facades/Telegraph.php b/src/Facades/Telegraph.php
index 00ebc225..a85a7d4e 100644
--- a/src/Facades/Telegraph.php
+++ b/src/Facades/Telegraph.php
@@ -24,6 +24,7 @@ use Illuminate\Support\Facades\Facade;
  * @method static \DefStudio\Telegraph\Telegraph  registerWebhook()
  * @method static \DefStudio\Telegraph\Telegraph  unregisterWebhook(bool $dropPendingUpdates = false)
  * @method static \DefStudio\Telegraph\Telegraph  registerBotCommands(array $commands)
+ * @method static \DefStudio\Telegraph\Telegraph  getRegisteredCommands()
  * @method static \DefStudio\Telegraph\Telegraph  unregisterBotCommands()
  * @method static \DefStudio\Telegraph\Telegraph  getWebhookDebugInfo()
  * @method static \DefStudio\Telegraph\Telegraph  replyWebhook(string $callbackQueryId, string $message)
diff --git a/src/Models/TelegraphBot.php b/src/Models/TelegraphBot.php
index d8c5f64a..c256b7fc 100644
--- a/src/Models/TelegraphBot.php
+++ b/src/Models/TelegraphBot.php
@@ -130,6 +130,11 @@ class TelegraphBot extends Model implements Storable
         return TelegraphFacade::bot($this)->registerBotCommands($commands);
     }
 
+    public function getRegisteredCommands(): Telegraph
+    {
+        return TelegraphFacade::bot($this)->getRegisteredCommands();
+    }
+
     public function unregisterCommands(): Telegraph
     {
         return TelegraphFacade::bot($this)->unregisterBotCommands();
diff --git a/src/Telegraph.php b/src/Telegraph.php
index 42a12443..0a4f4cbc 100755
--- a/src/Telegraph.php
+++ b/src/Telegraph.php
@@ -18,6 +18,7 @@ use DefStudio\Telegraph\Concerns\InteractsWithWebhooks;
 use DefStudio\Telegraph\Concerns\InteractWithUsers;
 use DefStudio\Telegraph\Concerns\ManagesKeyboards;
 use DefStudio\Telegraph\Concerns\SendsAttachments;
+use DefStudio\Telegraph\Concerns\InteractsWithCommands;
 use DefStudio\Telegraph\Concerns\StoresFiles;
 use DefStudio\Telegraph\DTO\Attachment;
 use Illuminate\Foundation\Bus\PendingDispatch;
@@ -36,6 +37,7 @@ class Telegraph
     use AnswersInlineQueries;
     use CreatesScopedPayloads;
     use InteractWithUsers;
+    use InteractsWithCommands;
 
     public const MAX_DOCUMENT_SIZE_IN_MB = 50;
     public const MAX_ANIMATION_SIZE_IN_MB = 50;
@@ -59,6 +61,7 @@ class Telegraph
     public const ENDPOINT_GET_BOT_UPDATES = 'getUpdates';
     public const ENDPOINT_GET_BOT_INFO = 'getMe';
     public const ENDPOINT_REGISTER_BOT_COMMANDS = 'setMyCommands';
+    public const ENDPOINT_GET_REGISTERED_BOT_COMMANDS = 'getMyCommands';
     public const ENDPOINT_UNREGISTER_BOT_COMMANDS = 'deleteMyCommands';
     public const ENDPOINT_SET_WEBHOOK = 'setWebhook';
     public const ENDPOINT_UNSET_WEBHOOK = 'deleteWebhook';


Test patch:
diff --git a/tests/Unit/Concerns/HasBotsAndChatsTest.php b/tests/Unit/Concerns/HasBotsAndChatsTest.php
index 023332bd..67544952 100644
--- a/tests/Unit/Concerns/HasBotsAndChatsTest.php
+++ b/tests/Unit/Concerns/HasBotsAndChatsTest.php
@@ -62,30 +62,6 @@ it('can retrieve bot info from its token', function () {
     assertMatchesSnapshot($response->json('result'));
 });
 
-it('can register commands', function () {
-    expect(function (\DefStudio\Telegraph\Telegraph $telegraph) {
-        return $telegraph->bot(make_bot())->registerBotCommands([
-            'foo' => 'first command',
-            'bar' => 'second command',
-        ]);
-    })->toMatchTelegramSnapshot();
-});
-
-it('can register commands with token', function () {
-    expect(function (\DefStudio\Telegraph\Telegraph $telegraph) {
-        return $telegraph->bot("3f3814e1-5836-3d77-904e-60f64b15df36")->registerBotCommands([
-            'foo' => 'first command',
-            'bar' => 'second command',
-        ]);
-    })->toMatchTelegramSnapshot();
-});
-
-it('can unregister commands', function () {
-    expect(function (\DefStudio\Telegraph\Telegraph $telegraph) {
-        return $telegraph->bot(make_bot())->unregisterBotCommands();
-    })->toMatchTelegramSnapshot();
-});
-
 it('can leave a chat', function () {
     expect(function (\DefStudio\Telegraph\Telegraph $telegraph) {
         return $telegraph->chat(make_chat())->leaveChat();
diff --git a/tests/Unit/Concerns/InteractsWithCommandsTest.php b/tests/Unit/Concerns/InteractsWithCommandsTest.php
new file mode 100644
index 00000000..722e029f
--- /dev/null
+++ b/tests/Unit/Concerns/InteractsWithCommandsTest.php
@@ -0,0 +1,37 @@
+<?php
+
+/** @noinspection PhpUnhandledExceptionInspection */
+
+/** @noinspection LaravelFunctionsInspection */
+
+use DefStudio\Telegraph\Telegraph;
+
+it('can retrieve bot commands', function () {
+    expect(function (Telegraph $telegraph) {
+        return $telegraph->bot(make_bot())->getRegisteredCommands();
+    })->toMatchTelegramSnapshot();
+});
+
+it('can register commands', function () {
+    expect(function (\DefStudio\Telegraph\Telegraph $telegraph) {
+        return $telegraph->bot(make_bot())->registerBotCommands([
+            'foo' => 'first command',
+            'bar' => 'second command',
+        ]);
+    })->toMatchTelegramSnapshot();
+});
+
+it('can register commands with token', function () {
+    expect(function (\DefStudio\Telegraph\Telegraph $telegraph) {
+        return $telegraph->bot("3f3814e1-5836-3d77-904e-60f64b15df36")->registerBotCommands([
+            'foo' => 'first command',
+            'bar' => 'second command',
+        ]);
+    })->toMatchTelegramSnapshot();
+});
+
+it('can unregister commands', function () {
+    expect(function (\DefStudio\Telegraph\Telegraph $telegraph) {
+        return $telegraph->bot(make_bot())->unregisterBotCommands();
+    })->toMatchTelegramSnapshot();
+});
diff --git a/tests/Unit/Models/TelegraphBotTest.php b/tests/Unit/Models/TelegraphBotTest.php
index 24e25f5b..9aaad465 100644
--- a/tests/Unit/Models/TelegraphBotTest.php
+++ b/tests/Unit/Models/TelegraphBotTest.php
@@ -75,6 +75,19 @@ it('can register commands', function () {
     );
 });
 
+it('can get registered commands', function () {
+    Telegraph::fake();
+
+    $bot = make_bot();
+
+    $bot->getRegisteredCommands()->send();
+
+    Telegraph::assertSentData(
+        \DefStudio\Telegraph\Telegraph::ENDPOINT_GET_REGISTERED_BOT_COMMANDS,
+        []
+    );
+});
+
 it('can unregister commands', function () {
     Telegraph::fake();
 
diff --git a/tests/__snapshots__/HasBotsAndChatsTest__it_can_register_commands__1.yml b/tests/__snapshots__/InteractsWithCommandsTest__it_can_register_commands__1.yml
similarity index 100%
rename from tests/__snapshots__/HasBotsAndChatsTest__it_can_register_commands__1.yml
rename to tests/__snapshots__/InteractsWithCommandsTest__it_can_register_commands__1.yml
diff --git a/tests/__snapshots__/HasBotsAndChatsTest__it_can_register_commands_with_token__1.yml b/tests/__snapshots__/InteractsWithCommandsTest__it_can_register_commands_with_token__1.yml
similarity index 100%
rename from tests/__snapshots__/HasBotsAndChatsTest__it_can_register_commands_with_token__1.yml
rename to tests/__snapshots__/InteractsWithCommandsTest__it_can_register_commands_with_token__1.yml
diff --git a/tests/__snapshots__/InteractsWithCommandsTest__it_can_retrieve_bot_commands__1.yml b/tests/__snapshots__/InteractsWithCommandsTest__it_can_retrieve_bot_commands__1.yml
new file mode 100644
index 00000000..a4d656cf
--- /dev/null
+++ b/tests/__snapshots__/InteractsWithCommandsTest__it_can_retrieve_bot_commands__1.yml
@@ -0,0 +1,3 @@
+url: 'https://api.telegram.org/bot3f3814e1-5836-3d77-904e-60f64b15df36/getMyCommands'
+payload: {  }
+files: {  }