Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"ext-json":"*",
"ext-curl":"*",
"skrtdev/async":"~0.7",
"monolog/monolog":"^2.1"
"monolog/monolog":"^2.1",
"benmorel/weakmap-polyfill": "^0.2.0"
},
"suggest": {
"ext-mbstring":"Needed to use the built-in Telegram Entites Parser",
Expand Down
31 changes: 30 additions & 1 deletion src/NovaGram/HandlersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace skrtdev\NovaGram;

use skrtdev\Telegram\{Message, CallbackQuery};
use Closure;
use Closure, WeakMap;

trait HandlersTrait{

protected array $commands = [];
protected WeakMap $statuses_cache;

// closure handlers

Expand Down Expand Up @@ -96,6 +97,34 @@ public function onText(string $pattern, Closure $handler): void
}
});
}

public function onUserStatus(string $status, Closure $handler): void
{
$this->statuses_cache ??= new WeakMap();
$this->onTextMessage(function (Message $message) use ($handler, $status) {
if(!isset($message->from)){
return;
}

$user = $message->from;
$real_status = $this->statuses_cache[$user] ??= $user->status();
if($real_status === $status){
$handler($message);
}
});
}

public function onChatStatus(string $status, Closure $handler): void
{
$this->statuses_cache ??= new WeakMap();
$this->onTextMessage(function (Message $message) use ($handler, $status) {
$chat = $message->chat;
$real_status = $this->statuses_cache[$chat] ??= $chat->status();
if($real_status === $status){
$handler($message);
}
});
}

public function onCommand($commands, Closure $handler, string $description = null): void
{
Expand Down