Skip to content

Commit

Permalink
Melhorias no método on
Browse files Browse the repository at this point in the history
  • Loading branch information
httd1 committed May 30, 2024
1 parent ea9c2ea commit 3895f37
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/MarkdownBot/bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());

Expand Down
15 changes: 11 additions & 4 deletions src/TelegramPhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, []);
}

Expand Down
41 changes: 41 additions & 0 deletions test/TelegramPhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}

0 comments on commit 3895f37

Please sign in to comment.