From 3d6cc15894447ad34dcb074ab964274f097e8a80 Mon Sep 17 00:00:00 2001 From: Netkas Date: Thu, 27 Apr 2023 14:32:32 -0400 Subject: [PATCH] Added method `\TgBotLib\Bot::answerWebAppQuery` to answer a callback query sent from a web app, which returns the newly added `\TgBotLib\Objects\Telegram\SentWebAppMessage` object on success. https://git.n64.cc/nosial/libs/tgbot/-/issues/3 --- CHANGELOG.md | 1 + src/TgBotLib/Bot.php | 38 ++++++++++++++ .../Objects/Telegram/SentWebAppMessage.php | 51 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 src/TgBotLib/Objects/Telegram/SentWebAppMessage.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b3efc74..6a901ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ input objects for methods that require input objects. * Added object `\TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultContact`, see [InlineQueryResultContact](https://core.telegram.org/bots/api#inlinequeryresultcontact) for more information. * Added object `\TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultGame`, see [InlineQueryResultGame](https://core.telegram.org/bots/api#inlinequeryresultgame) for more information. * Added class `\TgBotLib\Objects\Telegram\InlineQueryResult` which is the base class for all inline query results, additionally added `\TgBotLib\Abstracts\InlineQueryResultType` to represent the type of inline query result object. + * Added method `\TgBotLib\Bot::answerWebAppQuery` to answer a callback query sent from a web app, which returns the newly added `\TgBotLib\Objects\Telegram\SentWebAppMessage` object on success. ### Changed * Refactored InputMessageContent types to its own namespace so InputMessageContent can always return the correct InputMessageContent object type when calling `fromArray()` diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 4b30aa1..d9b0fca 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -24,9 +24,22 @@ use TgBotLib\Objects\Telegram\ChatMember; use TgBotLib\Objects\Telegram\File; use TgBotLib\Objects\Telegram\ForumTopic; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultArticle; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultAudio; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultContact; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultDocument; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultGame; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultGif; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultLocation; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultMpeg4Gif; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultPhoto; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultVenue; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultVideo; + use TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultVoice; use TgBotLib\Objects\Telegram\MenuButton; use TgBotLib\Objects\Telegram\Message; use TgBotLib\Objects\Telegram\Poll; + use TgBotLib\Objects\Telegram\SentWebAppMessage; use TgBotLib\Objects\Telegram\Sticker; use TgBotLib\Objects\Telegram\Update; use TgBotLib\Objects\Telegram\User; @@ -2735,4 +2748,29 @@ return true; } + + /** + * Use this method to set the result of an interaction with a Web App and send a corresponding message on behalf + * of the user to the chat from which the query originated. On success, a SentWebAppMessage object is returned. + * + * @param string $web_app_query_id Unique identifier for the query to be answered + * @param InlineQueryResultArticle|InlineQueryResultAudio|InlineQueryResultContact|InlineQueryResultDocument|InlineQueryResultGame|InlineQueryResultGif|InlineQueryResultLocation|InlineQueryResultMpeg4Gif|InlineQueryResultPhoto|InlineQueryResultVenue|InlineQueryResultVideo|InlineQueryResultVoice $result This object represents one result of an inline query. + * @return SentWebAppMessage + * @throws TelegramException + * @link https://core.telegram.org/bots/api#answerwebappquery + * @noinspection PhpUnused + * @see https://core.telegram.org/bots/webapps + */ + public function answerWebAppQuery( + string $web_app_query_id, + InlineQueryResultArticle|InlineQueryResultAudio|InlineQueryResultContact|InlineQueryResultDocument| + InlineQueryResultGame|InlineQueryResultGif|InlineQueryResultLocation|InlineQueryResultMpeg4Gif| + InlineQueryResultPhoto|InlineQueryResultVenue|InlineQueryResultVideo|InlineQueryResultVoice $result + ): SentWebAppMessage + { + return SentWebAppMessage::fromArray($this->sendRequest('answerWebAppQuery', [ + 'web_app_query_id' => $web_app_query_id, + 'result' => $result->toArray() + ])); + } } \ No newline at end of file diff --git a/src/TgBotLib/Objects/Telegram/SentWebAppMessage.php b/src/TgBotLib/Objects/Telegram/SentWebAppMessage.php new file mode 100644 index 0000000..2523c65 --- /dev/null +++ b/src/TgBotLib/Objects/Telegram/SentWebAppMessage.php @@ -0,0 +1,51 @@ +inline_message_id; + } + + /** + * Returns an array representation of the object + * + * @return null[]|string[] + */ + public function toArray(): array + { + return [ + 'inline_message_id' => $this->inline_message_id, + ]; + } + + /** + * Constructs object from an array representation + * + * @param array $data + * @return SentWebAppMessage + */ + public static function fromArray(array $data): SentWebAppMessage + { + $object = new self(); + + $object->inline_message_id = $data['inline_message_id'] ?? null; + + return $object; + } + } \ No newline at end of file