From b8e5b5924160f22aea405beb1aa03e24fd5ba93b Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 28 Nov 2024 21:57:58 -0500 Subject: [PATCH] Add InlineQueryResultsButton class and integration --- src/TgBotLib/Bot.php | 3 + .../Objects/InlineQueryResultsButton.php | 136 ++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 src/TgBotLib/Objects/InlineQueryResultsButton.php diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 36e60aa..be644ed 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -37,7 +37,9 @@ use TgBotLib\Objects\ForceReply; use TgBotLib\Objects\ForumTopic; use TgBotLib\Objects\Gifts; + use TgBotLib\Objects\Inline\InlineQueryResult; use TgBotLib\Objects\InlineKeyboardMarkup; + use TgBotLib\Objects\InlineQueryResultsButton; use TgBotLib\Objects\InputMedia; use TgBotLib\Objects\InputPollOption; use TgBotLib\Objects\LinkPreviewOptions; @@ -172,6 +174,7 @@ * @method true deleteStickerSet(string $name) Use this method to delete a sticker set that was created by the bot. Returns True on success. * @method Gifts getAvailableGifts() Returns the list of gifts that can be sent by the bot to users. Requires no parameters. Returns a Gifts object. * @method true sendGift(int $user_id, string $gift_id, ?string $text=null, ?ParseMode $text_parse_mode=null, MessageEntity[]|null $text_entities=null) Sends a gift to the given user. The gift can't be converted to Telegram Stars by the user. Returns True on success. + * @method true answerInlineQuery(string $inline_query_id, InlineQueryResult[] $results, ?int $cache_time=null, ?bool $is_personal=null, ?string $next_offset=null, ?InlineQueryResultsButton $button=null) Use this method to send answers to an inline query. On success, True is returned. * @throws TelegramException if the method execution fails. */ class Bot diff --git a/src/TgBotLib/Objects/InlineQueryResultsButton.php b/src/TgBotLib/Objects/InlineQueryResultsButton.php new file mode 100644 index 0000000..0cd2612 --- /dev/null +++ b/src/TgBotLib/Objects/InlineQueryResultsButton.php @@ -0,0 +1,136 @@ +text = null; + $this->webApp = null; + $this->startParameter = null; + return; + } + + $this->text = $data['text']; + $this->webApp = WebAppInfo::fromArray($data['web_app']); + $this->startParameter = $data['start_parameter']; + } + + /** + * Label text on the button + * + * @return string|null + */ + public function getText(): ?string + { + return $this->text; + } + + /** + * Label text on the button + * + * @param string|null $text + * @return $this + */ + public function setText(?string $text): InlineQueryResultsButton + { + $this->text = $text; + return $this; + } + + /** + * Optional. Description of the Web App that will be launched when the user presses the button. + * The Web App will be able to switch back to the inline mode using the method switchInlineQuery inside the Web App. + * + * @return WebAppInfo|null + */ + public function getWebApp(): ?WebAppInfo + { + return $this->webApp; + } + + /** + * Optional. Description of the Web App that will be launched when the user presses the button. + * The Web App will be able to switch back to the inline mode using the method switchInlineQuery inside the Web App. + * + * @param WebAppInfo|null $webApp + * @return $this + */ + public function setWebApp(?WebAppInfo $webApp): InlineQueryResultsButton + { + $this->webApp = $webApp; + return $this; + } + + /** + * Optional. Deep-linking parameter for the /start message sent to the bot when a user presses the button. + * 1-64 characters, only A-Z, a-z, 0-9, _ and - are allowed. + * + * Example: An inline bot that sends YouTube videos can ask the user to connect the bot to their YouTube account + * to adapt search results accordingly. To do this, it displays a 'Connect your YouTube account' button above + * the results, or even before showing any. The user presses the button, switches to a private chat with the bot + * and, in doing so, passes a start parameter that instructs the bot to return an OAuth link. Once done, the bot + * can offer a switch_inline button so that the user can easily return to the chat where they wanted to use the + * bot's inline capabilities. + * + * @return string|null + */ + public function getStartParameter(): ?string + { + return $this->startParameter; + } + + /** + * Optional. Deep-linking parameter for the /start message sent to the bot when a user presses the button. + * 1-64 characters, only A-Z, a-z, 0-9, _ and - are allowed. + * + * Example: An inline bot that sends YouTube videos can ask the user to connect the bot to their YouTube account + * to adapt search results accordingly. To do this, it displays a 'Connect your YouTube account' button above + * the results, or even before showing any. The user presses the button, switches to a private chat with the bot + * and, in doing so, passes a start parameter that instructs the bot to return an OAuth link. Once done, the bot + * can offer a switch_inline button so that the user can easily return to the chat where they wanted to use the + * bot's inline capabilities. + * + * @param string|null $startParameter + * @return $this + */ + public function setStartParameter(?string $startParameter): InlineQueryResultsButton + { + $this->startParameter = $startParameter; + return $this; + } + + /** + * @inheritDoc + */ + public function toArray(): ?array + { + return [ + 'text' => $this->text, + 'web_app' => $this->webApp->toArray(), + 'start_parameter' => $this->startParameter + ]; + } + + /** + * @inheritDoc + */ + public static function fromArray(?array $data): ?InlineQueryResultsButton + { + return new self($data); + } + } \ No newline at end of file