From e2f42de8cc1e5a04af1c002d2d6457a346288290 Mon Sep 17 00:00:00 2001 From: TangMo Date: Fri, 5 May 2023 23:01:11 +0000 Subject: [PATCH] Support vx multi-pic refresh --- src/Commands/CallbackqueryCommand.php | 17 ++++++++++++++--- src/Commands/GenericmessageCommand.php | 21 +++++++++++---------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/Commands/CallbackqueryCommand.php b/src/Commands/CallbackqueryCommand.php index 4dedda0..e3b60e5 100644 --- a/src/Commands/CallbackqueryCommand.php +++ b/src/Commands/CallbackqueryCommand.php @@ -56,11 +56,22 @@ class CallbackqueryCommand extends SystemCommand $attemptLimit = 3; $query = '?tmfx'; - $parts = explode($query, $callbackQuery->getMessage()->getText()); - $currentAttempt = isset($parts[1]) ? intval($parts[1])+1 : 1; + preg_match('/status\/\d+(?:\/photo\/(\d))?(?:\?tmfx(\d))?/', $callbackQuery->getMessage()->getText(), $matches); + $currentAttempt = empty($matches[2]) ? 1 : intval($matches[2])+1; + $photoNo = $matches[1] ?? null; // 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; + // But VXTwitter check for /photo/1, /photo/2, ... by looking at URI (including query string) ending with /1, /2 + // To maintain compatibility with both fx/vx, we will use https://vxtwitter.com/aaa/status/1234567890/photo/3?tmfx1/3 + $newUrl = explode($query, $callbackQuery->getMessage()->getText())[0]; + $newUrl = explode('/photo/', $newUrl)[0]; + if ($photoNo) { + $newUrl .= '/photo/' . $photoNo; + } + $newUrl .= $query . $currentAttempt; + if ($photoNo) { + $newUrl .= '/' . $photoNo; + } $keyboard = $callbackQuery->getMessage()->getReplyMarkup(); if ($currentAttempt >= $attemptLimit) { diff --git a/src/Commands/GenericmessageCommand.php b/src/Commands/GenericmessageCommand.php index 1a5a557..5f2d841 100755 --- a/src/Commands/GenericmessageCommand.php +++ b/src/Commands/GenericmessageCommand.php @@ -39,11 +39,13 @@ class GenericmessageCommand extends SystemCommand } $author = $matches[1]; $tweetId = $matches[2]; - $photoNo = ''; + $photoNo = null; + $photoNoText = ''; if (!empty($matches[3])) { preg_match('/(\/\d)$/', $matches[3], $photoNoMatches); if (isset($photoNoMatches[1])) { - $photoNo = '/photo' . $photoNoMatches[1]; + $photoNo = intval($photoNoMatches[1]); + $photoNoText = '/photo' . $photoNoMatches[1]; } } @@ -61,17 +63,16 @@ class GenericmessageCommand extends SystemCommand ]), ]); } - // 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']), - ]); - } + + $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); + return $this->replyToChat("https://vxtwitter.com/$author/status/$tweetId$photoNoText", $data); } } \ No newline at end of file