1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 10:19:00 +01:00

Add to many features!

- Add CallbackQuery class for handle callbackQueries updates with 2 bound method(edit and answer)
- Add support for CallbackQueries in UpdateHandler class
- Add FilterCallback & FilterCallbackRegex class for handle callbackQueries texts
- Change structure for FilterFromAdmin to handle also callbackQueries requests
- add 2 method edit and translate to Message class
This commit is contained in:
Mahdi 2023-07-30 16:12:04 +03:30
parent b5055c1c38
commit b578b4ceb0
6 changed files with 232 additions and 1 deletions

View File

@ -0,0 +1,104 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\ParseMode;
final class CallbackQuery extends Update
{
/** @var int Query ID */
public readonly int $queryId;
/** @var int ID of the user that pressed the button */
public readonly int $userId;
/** @var int Chat where the inline keyboard was sent */
public readonly int $chatId;
/** @var int Message ID */
public readonly int $messageId;
/**
* @var int Global identifier, uniquely corresponding to the chat to which the message with the callback button was sent. Useful for high scores in games.
*/
public readonly int $chatInstance;
/** @var string Callback data */
public readonly string $data;
/**
* @var string Short name of a Game to be returned, serves as the unique identifier for the game
*/
public readonly string $gameShortName;
/**
* @readonly
*
* @var list<string> Regex matches, if a filter regex is present
*/
public ?array $matches = null;
/** @internal */
public function __construct(MTProto $API, array $rawCallback)
{
parent::__construct($API);
$this->queryId = $rawCallback['query_id'];
$this->userId = $rawCallback['user_id'];
$this->chatId = $rawCallback['peer']['user_id'] ?? $rawCallback['peer']['chat_id'];
$this->messageId = $rawCallback['msg_id'];
$this->chatInstance = $rawCallback['chat_instance'];
$this->data = (string) $rawCallback['data'];
$this->gameShortName = $rawCallback['game_short_name'] ?? '';
}
public function answer(
string $message,
bool $alert = false,
?string $url = null,
int $cacheTime = 5 * 60
): bool {
return $this->getClient()->methodCallAsyncRead(
'messages.setBotCallbackAnswer',
[
'query_id' => $this->queryId,
'message' => $message,
'alert' => $alert,
'url' => $url,
'cache_time' => $cacheTime
]
);
}
public function edit(
?string $message,
?array $replyMarkup = null,
?array $entities = null,
ParseMode $parseMode = ParseMode::TEXT,
?int $scheduleDate = null,
bool $noWebpage = false
): Message {
$result = $this->getClient()->methodCallAsyncRead(
'messages.editMessage',
[
'peer' => $this->chatId,
'id' => $this->messageId,
'message' => $message,
'reply_markup' => $replyMarkup,
'entities' => $entities,
'parse_mode' => $parseMode,
'schedule_date' => $scheduleDate,
'no_webpage' => $noWebpage
]
);
if (isset($result['_'])) {
return $this->getClient()->wrapMessage($this->getClient()->extractMessage($result));
}
$last = null;
foreach ($result as $updates) {
$new = $this->getClient()->wrapMessage($this->getClient()->extractMessage($updates));
if ($last) {
$last->nextSent = $new;
} else {
$first = $new;
}
$last = $new;
}
return $first;
}
}

View File

@ -0,0 +1,22 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\Filter;
use Attribute;
use danog\MadelineProto\EventHandler\CallbackQuery;
use danog\MadelineProto\EventHandler\Update;
use Webmozart\Assert\Assert;
#[Attribute(Attribute::TARGET_METHOD)]
class FilterCallback extends Filter
{
public function __construct(
private readonly string $content
) {
Assert::notEmpty($content);
}
public function apply(Update $update): bool
{
return $update instanceof CallbackQuery && $update->data === $this->content;
}
}

View File

@ -0,0 +1,27 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\Filter;
use Attribute;
use danog\MadelineProto\EventHandler\CallbackQuery;
use danog\MadelineProto\EventHandler\Update;
use Webmozart\Assert\Assert;
#[Attribute(Attribute::TARGET_METHOD)]
class FilterCallbackRegex extends Filter
{
public function __construct(private readonly string $regex)
{
\preg_match($regex, '');
Assert::eq(\preg_last_error_msg(), 'No error');
}
public function apply(Update $update): bool
{
if ($update instanceof CallbackQuery && \preg_match($this->regex, $update->data, $matches)) {
$update->matches = $matches;
return true;
}
return false;
}
}

View File

@ -5,6 +5,7 @@ namespace danog\MadelineProto\EventHandler\Filter;
use Attribute;
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\EventHandler\AbstractMessage;
use danog\MadelineProto\EventHandler\CallbackQuery;
use danog\MadelineProto\EventHandler\Update;
/**
@ -21,6 +22,7 @@ final class FilterFromAdmin extends Filter
}
public function apply(Update $update): bool
{
return $update instanceof AbstractMessage && \in_array($update->senderId, $this->adminIds, true);
return ($update instanceof AbstractMessage && \in_array($update->senderId, $this->adminIds, true)) ||
($update instanceof CallbackQuery && \in_array($update->userId, $this->adminIds, true));
}
}

View File

@ -15,7 +15,9 @@ use danog\MadelineProto\EventHandler\Media\Sticker;
use danog\MadelineProto\EventHandler\Media\Video;
use danog\MadelineProto\EventHandler\Media\Voice;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\ParseMode;
use danog\MadelineProto\StrTools;
use Webmozart\Assert\Assert;
/**
* Represents an incoming or outgoing message.
@ -259,6 +261,78 @@ abstract class Message extends AbstractMessage
return $this->reactions;
}
/**
* Translate text message(for media translate it caption).
*
* @param string $toLang Two-letter ISO 639-1 language code of the language to which the message is translated
*
*/
public function translate(
string $toLang = 'en'
): string {
Assert::notEmpty($this->message);
$result = $this->getClient()->methodCallAsyncRead(
'messages.translateText',
[
'peer' => $this->chatId,
'id' => [$this->id],
'text' => $this->message,
'to_lang' => $toLang
]
);
return $result[0]['text'];
}
/**
* Edit message text.
*
* @param string $message New message
* @param array|null $replyMarkup Reply markup for inline keyboards
* @param array|null $entities Message entities for styled text
* @param ParseMode $parseMode Whether to parse HTML or Markdown markup in the message
* @param int|null $scheduleDate Scheduled message date for scheduled messages
* @param bool $noWebpage Disable webpage preview
*
*/
public function edit(
string $message,
?array $replyMarkup = null,
?array $entities = null,
ParseMode $parseMode = ParseMode::TEXT,
?int $scheduleDate = null,
bool $noWebpage = false
): Message {
Assert::notEmpty($this->message);
$result = $this->getClient()->methodCallAsyncRead(
'messages.editMessage',
[
'peer' => $this->chatId,
'id' => $this->id,
'message' => $message,
'reply_markup' => $replyMarkup,
'entities' => $entities,
'parse_mode' => $parseMode,
'schedule_date' => $scheduleDate,
'no_webpage' => $noWebpage
]
);
if (isset($result['_'])) {
return $this->getClient()->wrapMessage($this->getClient()->extractMessage($result));
}
$last = null;
foreach ($result as $updates) {
$new = $this->getClient()->wrapMessage($this->getClient()->extractMessage($updates));
if ($last) {
$last->nextSent = $new;
} else {
$first = $new;
}
$last = $new;
}
return $first;
}
protected readonly string $html;
protected readonly string $htmlTelegram;
protected readonly ?array $entities;

View File

@ -27,6 +27,7 @@ use Amp\Http\Client\Response;
use Amp\TimeoutException;
use danog\MadelineProto\API;
use danog\MadelineProto\EventHandler\AbstractMessage;
use danog\MadelineProto\EventHandler\CallbackQuery;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\Message\ChannelMessage;
use danog\MadelineProto\EventHandler\Message\GroupMessage;
@ -334,6 +335,7 @@ trait UpdateHandler
{
return match ($update['_']) {
'updateNewChannelMessage', 'updateNewMessage', 'updateNewScheduledMessage', 'updateEditMessage', 'updateEditChannelMessage' => $this->wrapMessage($update['message']),
'updateBotCallbackQuery' => new CallbackQuery($this, $update),
default => null
};
}