From 3895f37dca52fda3183c70f3a28192bd3438b300 Mon Sep 17 00:00:00 2001 From: "J.M" <23122559+httd1@users.noreply.github.com> Date: Thu, 30 May 2024 10:03:48 -0300 Subject: [PATCH] =?UTF-8?q?Melhorias=20no=20m=C3=A9todo=20on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +++ examples/MarkdownBot/bot.php | 2 +- src/TelegramPhp.php | 15 +++++++++---- test/TelegramPhpTest.php | 41 ++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2ffeca1..1ab46c2 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,9 @@ class LogCommands { $tlg->on ('my_chat_member', function ($bot){ // code here }); +// $tlg->on (['message_reaction', 'message'], function ($bot){ + // code here +// }); ``` - Processando updates '_chat_member_' ```php diff --git a/examples/MarkdownBot/bot.php b/examples/MarkdownBot/bot.php index 20ecf11..270ff3d 100644 --- a/examples/MarkdownBot/bot.php +++ b/examples/MarkdownBot/bot.php @@ -45,7 +45,7 @@ }'); // get and process reactions -$tlg->on ('message_reaction', function ($bot){ +$tlg->on (['message_reaction', 'message'], function ($bot){ print_r ($bot->getContent ()); diff --git a/src/TelegramPhp.php b/src/TelegramPhp.php index 8ce4b7d..fed722d 100644 --- a/src/TelegramPhp.php +++ b/src/TelegramPhp.php @@ -153,17 +153,24 @@ public function commandMatch (string $regex, callable|string $action) :void } /** - * Runs $action when an update sent by Telegram is the same of $update. + * Runs $action when an update sent by Telegram is the same of $updates. * - * @param string $update + * @param string|array $updates * @param callable|string $action * * @return void */ - public function on (string $update, callable|string $action) : void + public function on (string|array $updates, callable|string $action) : void { - if ($this->getUpdateType () == $update){ + // convert to array + if (!is_array ($updates)){ + $updates_list [] = $updates; + }else { + $updates_list = $updates; + } + + if (in_array ($this->getUpdateType (), $updates_list)){ $this->runAction ($action, []); } diff --git a/test/TelegramPhpTest.php b/test/TelegramPhpTest.php index 506a4b8..7683883 100644 --- a/test/TelegramPhpTest.php +++ b/test/TelegramPhpTest.php @@ -207,4 +207,45 @@ public function testCommandDefault (){ $this->assertEquals ('commandDefault', $test1); } + + public function testOn (){ + + $tlg = new TelegramPhp(); + $tlg->setContent('{ + "update_id": 905605451, + "message_reaction": { + "chat": { + "id": 275123569, + "first_name": "J.M", + "username": "httd1", + "type": "private" + }, + "message_id": 116, + "user": { + "id": 275123569, + "is_bot": false, + "first_name": "J.M", + "username": "httd1", + "language_code": "pt-br" + }, + "date": 1704974812, + "old_reaction": [], + "new_reaction": [ + { + "type": "emoji", + "emoji": "👍" + } + ] + } + }'); + + $valueTest = false; + + $tlg->on (['message_reaction', 'channel_post'], function ($bot) use (&$valueTest){ + $valueTest = true; + }); + + $this->assertTrue ($valueTest); + + } } \ No newline at end of file