Implement some helper methods for replying to commands and answering queries.

parent c31598f6
......@@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
### Added
- Donation section and links in readme.
- Missing payment methods in `Request` class.
- Some helper methods for replying to commands and answering queries.
### Changed
- Updated and optimised all DB classes, removing a lot of bulky code.
### Deprecated
......
......@@ -386,4 +386,44 @@ abstract class Command
return false;
}
/**
* Helper to reply to a chat directly.
*
* @param string $text
* @param array $data
*
* @return \Longman\TelegramBot\Entities\ServerResponse
*/
public function replyToChat($text, array $data = [])
{
if ($message = $this->getMessage() ?: $this->getEditedMessage() ?: $this->getChannelPost() ?: $this->getEditedChannelPost()) {
return Request::sendMessage(array_merge([
'chat_id' => $message->getChat()->getId(),
'text' => $text,
], $data));
}
return Request::emptyResponse();
}
/**
* Helper to reply to a user directly.
*
* @param string $text
* @param array $data
*
* @return \Longman\TelegramBot\Entities\ServerResponse
*/
public function replyToUser($text, array $data = [])
{
if ($message = $this->getMessage() ?: $this->getEditedMessage()) {
return Request::sendMessage(array_merge([
'chat_id' => $message->getFrom()->getId(),
'text' => $text,
], $data));
}
return Request::emptyResponse();
}
}
......@@ -10,6 +10,8 @@
namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Request;
/**
* Class CallbackQuery.
*
......@@ -33,4 +35,18 @@ class CallbackQuery extends Entity
'message' => Message::class,
];
}
/**
* Answer this callback query.
*
* @param array $data
*
* @return ServerResponse
*/
public function answer(array $data = [])
{
return Request::answerCallbackQuery(array_merge([
'callback_query_id' => $this->getId(),
], $data));
}
}
......@@ -10,6 +10,8 @@
namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResult;
/**
* Class InlineQuery
*
......@@ -33,4 +35,20 @@ class InlineQuery extends Entity
'location' => Location::class,
];
}
/**
* Answer this inline query with the passed results.
*
* @param InlineQueryResult[] $results
* @param array $data
*
* @return ServerResponse
*/
public function answer(array $results, array $data = [])
{
return Request::answerCallbackQuery(array_merge([
'callback_query_id' => $this->getId(),
'results' => $results,
], $data));
}
}
<?php
namespace Longman\TelegramBot\Entities\InlineQuery;
interface InlineQueryResult
{
}
......@@ -56,7 +56,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width
* @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height
*/
class InlineQueryResultArticle extends InlineEntity
class InlineQueryResultArticle extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultArticle constructor
......
......@@ -50,7 +50,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the audio
*/
class InlineQueryResultAudio extends InlineEntity
class InlineQueryResultAudio extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultAudio constructor
......
......@@ -41,7 +41,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the audio
*/
class InlineQueryResultCachedAudio extends InlineEntity
class InlineQueryResultCachedAudio extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultCachedAudio constructor
......
......@@ -47,7 +47,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the file
*/
class InlineQueryResultCachedDocument extends InlineEntity
class InlineQueryResultCachedDocument extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultCachedDocument constructor
......
......@@ -44,7 +44,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the GIF animation
*/
class InlineQueryResultCachedGif extends InlineEntity
class InlineQueryResultCachedGif extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultCachedGif constructor
......
......@@ -44,7 +44,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video animation
*/
class InlineQueryResultCachedMpeg4Gif extends InlineEntity
class InlineQueryResultCachedMpeg4Gif extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultCachedMpeg4Gif constructor
......
......@@ -47,7 +47,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo
*/
class InlineQueryResultCachedPhoto extends InlineEntity
class InlineQueryResultCachedPhoto extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultCachedPhoto constructor
......
......@@ -38,7 +38,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the sticker
*/
class InlineQueryResultCachedSticker extends InlineEntity
class InlineQueryResultCachedSticker extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultCachedSticker constructor
......
......@@ -47,7 +47,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video
*/
class InlineQueryResultCachedVideo extends InlineEntity
class InlineQueryResultCachedVideo extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultCachedVideo constructor
......
......@@ -44,7 +44,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. An Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the voice message
*/
class InlineQueryResultCachedVoice extends InlineEntity
class InlineQueryResultCachedVoice extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultCachedVoice constructor
......
......@@ -53,7 +53,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width
* @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height
*/
class InlineQueryResultContact extends InlineEntity
class InlineQueryResultContact extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultContact constructor
......
......@@ -59,7 +59,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width
* @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height
*/
class InlineQueryResultDocument extends InlineEntity
class InlineQueryResultDocument extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultDocument constructor
......
......@@ -55,7 +55,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the GIF animation
*/
class InlineQueryResultGif extends InlineEntity
class InlineQueryResultGif extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultGif constructor
......
......@@ -53,7 +53,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width
* @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height
*/
class InlineQueryResultLocation extends InlineEntity
class InlineQueryResultLocation extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultLocation constructor
......
......@@ -55,7 +55,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video animation
*/
class InlineQueryResultMpeg4Gif extends InlineEntity
class InlineQueryResultMpeg4Gif extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultMpeg4Gif constructor
......
......@@ -56,7 +56,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the photo
*/
class InlineQueryResultPhoto extends InlineEntity
class InlineQueryResultPhoto extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultPhoto constructor
......
......@@ -59,7 +59,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setThumbWidth(int $thumb_width) Optional. Thumbnail width
* @method $this setThumbHeight(int $thumb_height) Optional. Thumbnail height
*/
class InlineQueryResultVenue extends InlineEntity
class InlineQueryResultVenue extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultVenue constructor
......
......@@ -62,7 +62,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the video
*/
class InlineQueryResultVideo extends InlineEntity
class InlineQueryResultVideo extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultVideo constructor
......
......@@ -47,7 +47,7 @@ use Longman\TelegramBot\Entities\InputMessageContent\InputMessageContent;
* @method $this setReplyMarkup(InlineKeyboard $reply_markup) Optional. Inline keyboard attached to the message
* @method $this setInputMessageContent(InputMessageContent $input_message_content) Optional. Content of the message to be sent instead of the voice recording
*/
class InlineQueryResultVoice extends InlineEntity
class InlineQueryResultVoice extends InlineEntity implements InlineQueryResult
{
/**
* InlineQueryResultVoice constructor
......
......@@ -12,6 +12,7 @@ namespace Longman\TelegramBot\Entities\Payments;
use Longman\TelegramBot\Entities\Entity;
use Longman\TelegramBot\Entities\User;
use Longman\TelegramBot\Request;
/**
* Class PreCheckoutQuery
......@@ -40,4 +41,20 @@ class PreCheckoutQuery extends Entity
'order_info' => OrderInfo::class,
];
}
/**
* Answer this pre-checkout query.
*
* @param bool $ok
* @param array $data
*
* @return \Longman\TelegramBot\Entities\ServerResponse
*/
public function answer($ok, array $data = [])
{
return Request::answerPreCheckoutQuery(array_merge([
'pre_checkout_query_id' => $this->getId(),
'ok' => $ok,
], $data));
}
}
......@@ -12,6 +12,7 @@ namespace Longman\TelegramBot\Entities\Payments;
use Longman\TelegramBot\Entities\Entity;
use Longman\TelegramBot\Entities\User;
use Longman\TelegramBot\Request;
/**
* Class ShippingQuery
......@@ -37,4 +38,20 @@ class ShippingQuery extends Entity
'shipping_address' => ShippingAddress::class,
];
}
/**
* Answer this shipping query.
*
* @param bool $ok
* @param array $data
*
* @return \Longman\TelegramBot\Entities\ServerResponse
*/
public function answer($ok, array $data = [])
{
return Request::answerShippingQuery(array_merge([
'shipping_query_id' => $this->getId(),
'ok' => $ok,
], $data));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment