1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 05:34:42 +01:00

Merge pull request #1393 from AhJXD/v8

Add InlineQuery
This commit is contained in:
Daniil Gentili 2023-08-29 22:01:56 +02:00 committed by GitHub
commit 5a94df0b3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 143 additions and 3 deletions

View File

@ -19,6 +19,8 @@ namespace danog\MadelineProto\EventHandler\Filter;
use Attribute;
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\EventHandler\Message\GroupMessage;
use danog\MadelineProto\EventHandler\Query\ButtonQuery;
use danog\MadelineProto\EventHandler\InlineQuery;
use danog\MadelineProto\EventHandler\Update;
/**
@ -40,6 +42,8 @@ abstract class AbstractFilterFromSender extends Filter
}
public function apply(Update $update): bool
{
return $update instanceof GroupMessage && $update->senderId === $this->peerResolved;
return ($update instanceof GroupMessage && $update->senderId === $this->peerResolved) ||
($update instanceof ButtonQuery && $update->userId === $this->peerResolved) ||
($update instanceof InlineQuery && $update->userId === $this->peerResolved);
}
}

View File

@ -19,6 +19,8 @@ namespace danog\MadelineProto\EventHandler\Filter;
use Attribute;
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\EventHandler\Message\GroupMessage;
use danog\MadelineProto\EventHandler\Query\ButtonQuery;
use danog\MadelineProto\EventHandler\InlineQuery;
use danog\MadelineProto\EventHandler\Update;
/**
@ -52,5 +54,7 @@ abstract class AbstractFilterFromSenders extends Filter
public function apply(Update $update): bool
{
return $update instanceof GroupMessage && \in_array($update->senderId, $this->peersResolved, true);
($update instanceof ButtonQuery && \in_array($update->userId, $this->peerResolved, true)) ||
($update instanceof InlineQuery && \in_array($update->userId, $this->peerResolved, true));
}
}

View File

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

View File

@ -19,6 +19,7 @@ namespace danog\MadelineProto\EventHandler\Filter;
use Attribute;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\Query\ButtonQuery;
use danog\MadelineProto\EventHandler\InlineQuery;
use danog\MadelineProto\EventHandler\Update;
use Webmozart\Assert\Assert;
@ -46,6 +47,11 @@ final class FilterRegex extends Filter
$update->matches = $matches;
return true;
}
if ($update instanceof InlineQuery && \preg_match($this->regex, $update->query, $matches)) {
/** @psalm-suppress InaccessibleProperty */
$update->matches = $matches;
return true;
}
return false;
}
}

View File

@ -27,4 +27,4 @@ use danog\MadelineProto\EventHandler\Update;
#[Attribute(Attribute::TARGET_METHOD)]
final class FilterSender extends AbstractFilterFromSender
{
}
}

View File

@ -0,0 +1,44 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler;
use danog\MadelineProto\EventHandler\Media\GeoPoint;
use danog\MadelineProto\MTProto;
/**
* An incoming inline query
*/
final class InlineQuery extends Update
{
/** @var int Query ID */
public readonly int $queryId;
/** @var string Text of query */
public readonly string $query;
/** @var int User that sent the query */
public readonly int $userId;
/** @var string Offset to navigate through results */
public readonly string $offset;
/** @var GeoPoint Attached geolocation */
public readonly ?GeoPoint $geo;
/** @var InlineQueryPeerType Type of the chat from which the inline query was sent. */
public readonly InlineQueryPeerType $peerType;
/**
* @readonly
*
* @var list<string> Regex matches, if a filter regex is present
*/
public ?array $matches = null;
/** @internal */
public function __construct(MTProto $API, array $rawInlineQuery)
{
parent::__construct($API);
$this->queryId = $rawInlineQuery['query_id'];
$this->query = $rawInlineQuery['query'];
$this->userId = $rawInlineQuery['user_id'];
$this->offset = $rawInlineQuery['offset'];
$this->geo = isset($rawInlineQuery['geo']) ? new GeoPoint($rawInlineQuery['geo']) : null;
$this->peerType = InlineQueryPeerType::fromString($rawInlineQuery['peer_type']['_']);
}
}

View File

@ -0,0 +1,45 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler;
use AssertionError;
use JsonSerializable;
/** @internal */
enum InlineQueryPeerType implements JsonSerializable
{
/** private chat */
case PM;
/** [chat](https://core.telegram.org/api/channel) */
case Chat;
/** private chat with a bot. */
case BotPM;
/** [channel](https://core.telegram.org/api/channel) */
case Broadcast;
/** [supergroup](https://core.telegram.org/api/channel) */
case Megagroup;
/** private chat with the bot itself */
case SameBotPM;
/**
* Get InlineQueryPeerType from update
*
* @param string Type of the chat from which the inline query was sent.
* @throws AssertionError
*/
public static function fromString(string $name): InlineQueryPeerType
{
$newName = substr($name, 19);
foreach (InlineQueryPeerType::cases() as $case) {
if ($case->name === $newName)
return $case;
}
throw new AssertionError("Undefined case InlineQueryPeerType::".$name);
}
/** @internal */
public function jsonSerialize(): string
{
return $this->name;
}
}

View File

@ -0,0 +1,33 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\Media;
use JsonSerializable;
final class GeoPoint implements JsonSerializable
{
/** @var int Longitude */
public readonly int $long;
/** @var int Latitude */
public readonly int $lat;
/** @var int Access hash */
public readonly int $accessHash;
/** @var int The estimated horizontal accuracy of the location, in meters; as defined by the sender. */
public readonly ?int $accuracyRadius;
public function __construct(array $rawGeoPoint)
{
$this->long = $rawGeoPoint['long'];
$this->lat = $rawGeoPoint['lat'];
$this->accessHash = $rawGeoPoint['access_hash'];
$this->accuracyRadius = $rawGeoPoint['accuracy_radius'] ?? null;
}
/** @internal */
public function jsonSerialize(): mixed
{
$v = \get_object_vars($this);
$v['_'] = static::class;
return $v;
}
}

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\InlineQuery;
use danog\MadelineProto\EventHandler\Message;
use danog\MadelineProto\EventHandler\Message\ChannelMessage;
use danog\MadelineProto\EventHandler\Message\GroupMessage;
@ -344,6 +345,7 @@ trait UpdateHandler
'updateInlineBotCallbackQuery' => isset($update['game_short_name'])
? new InlineGameQuery($this, $update)
: new InlineButtonQuery($this, $update),
'updateBotInlineQuery' => new InlineQuery($this, $update),
'updatePhoneCall' => $update['phone_call'],
'updateBroadcastProgress' => $update['progress'],
default => null