From a89c59cf766ffb6d0059ca34d0441e9af04435b6 Mon Sep 17 00:00:00 2001 From: TangMo Date: Fri, 5 May 2023 20:37:10 +0000 Subject: [PATCH] Support refreshing preview --- src/Commands/CallbackqueryCommand.php | 75 +++++++++++++++++++++----- src/Commands/GenericmessageCommand.php | 15 +++++- 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/src/Commands/CallbackqueryCommand.php b/src/Commands/CallbackqueryCommand.php index a779385..4dedda0 100644 --- a/src/Commands/CallbackqueryCommand.php +++ b/src/Commands/CallbackqueryCommand.php @@ -2,6 +2,8 @@ namespace Bot\Command\System; use Longman\TelegramBot\Commands\SystemCommand; +use Longman\TelegramBot\Entities\CallbackQuery; +use Longman\TelegramBot\Entities\InlineKeyboard; use Longman\TelegramBot\Entities\ServerResponse; use Longman\TelegramBot\Request; @@ -11,23 +13,68 @@ class CallbackqueryCommand extends SystemCommand { try { $callbackQuery = $this->getUpdate()->getCallbackQuery(); - $originalSenderId = json_decode($callbackQuery->getData(), true)['sender_id']; - $callbackSenderId = $callbackQuery->getFrom()?->getId(); - if ($originalSenderId === $callbackSenderId) { - return Request::deleteMessage([ - 'chat_id' => $callbackQuery->getMessage()->getChat()->getId(), - 'message_id' => $callbackQuery->getMessage()->getMessageId(), - ]); - } else { - // Only possible if the user already has an DM channel open with the bot - // but better than nothing - return Request::sendMessage([ - 'chat_id' => $callbackSenderId, - 'text' => 'Only the person sending the original message can delete the reply.', - ]); + $callbackData = json_decode($callbackQuery->getData(), true); + $response = match ($callbackData['action'] ?? null) { + 'REFRESH' => $this->handleRefresh($callbackQuery, $callbackData), + default => $this->handleDelete($callbackQuery, $callbackData), + }; + + if ($response !== null) { + return $response; } } catch (\Exception $e) { } return Request::emptyResponse(); } + + private function handleDelete(CallbackQuery $callbackQuery, array $callbackData): ?ServerResponse + { + $originalSenderId = $callbackData['sender_id']; + $callbackSenderId = $callbackQuery->getFrom()?->getId(); + if ($originalSenderId === $callbackSenderId || $callbackSenderId === 625413203) { // Me + return Request::deleteMessage([ + 'chat_id' => $callbackQuery->getMessage()->getChat()->getId(), + 'message_id' => $callbackQuery->getMessage()->getMessageId(), + ]); + } else { + // Only possible if the user already has an DM channel open with the bot + // but better than nothing + return Request::sendMessage([ + 'chat_id' => $callbackSenderId, + 'text' => 'Only the person sending the original message can delete the reply.', + ]); + } + return null; + } + + private function handleRefresh(CallbackQuery $callbackQuery, array $callbackData): ?ServerResponse + { + if (!$callbackQuery->getMessage()) { + return null; + } + + $attemptLimit = 3; + $query = '?tmfx'; + + $parts = explode($query, $callbackQuery->getMessage()->getText()); + $currentAttempt = isset($parts[1]) ? intval($parts[1])+1 : 1; + // The proper way to refresh to a URL is to talk to @WebpageBot + // A workaround is to just add a query string + $newUrl = $parts[0] . $query . $currentAttempt; + + $keyboard = $callbackQuery->getMessage()->getReplyMarkup(); + if ($currentAttempt >= $attemptLimit) { + $keyboard = new InlineKeyboard(array_filter( + $callbackQuery->getMessage()->getReplyMarkup()->getProperty('inline_keyboard')[0], + static fn ($x) => $x->getProperty('text') !== '🔄 Refresh', + )); + } + + return Request::editMessageText([ + 'chat_id' => $callbackQuery->getMessage()->getChat()->getId(), + 'message_id' => $callbackQuery->getMessage()->getMessageId(), + 'text'=> $newUrl, + 'reply_markup' => $keyboard, + ]); + } } \ No newline at end of file diff --git a/src/Commands/GenericmessageCommand.php b/src/Commands/GenericmessageCommand.php index d3590bc..1a5a557 100755 --- a/src/Commands/GenericmessageCommand.php +++ b/src/Commands/GenericmessageCommand.php @@ -51,14 +51,25 @@ class GenericmessageCommand extends SystemCommand 'reply_to_message_id' => $message->getMessageId(), 'disable_notification' => true, ]; + $buttons = []; if ($message->getFrom() !== null) { - $deleteButton = new InlineKeyboardButton([ + $buttons []= new InlineKeyboardButton([ 'text' => '❌ Delete', 'callback_data' => json_encode([ + 'action' => 'DELETE', // Since 2023-05-03 'sender_id' => $message->getFrom()?->getId(), ]), ]); - $data['reply_markup'] = new InlineKeyboard([$deleteButton]); + } + // VXTwitter /photo/n doesn't work with any query string, will revert to multiple images + if ($photoNo === '') { + $buttons []= new InlineKeyboardButton([ + 'text' => '🔄 Refresh', + 'callback_data' => json_encode(['action' => 'REFRESH']), + ]); + } + if (!empty($buttons)) { + $data['reply_markup'] = new InlineKeyboard($buttons); } return $this->replyToChat("https://vxtwitter.com/$author/status/$tweetId$photoNo", $data);