Support refreshing preview

This commit is contained in:
2023-05-05 20:37:10 +00:00
parent cc302e5431
commit a89c59cf76
2 changed files with 74 additions and 16 deletions

View File

@@ -2,6 +2,8 @@
namespace Bot\Command\System; namespace Bot\Command\System;
use Longman\TelegramBot\Commands\SystemCommand; use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\CallbackQuery;
use Longman\TelegramBot\Entities\InlineKeyboard;
use Longman\TelegramBot\Entities\ServerResponse; use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
@@ -11,9 +13,25 @@ class CallbackqueryCommand extends SystemCommand
{ {
try { try {
$callbackQuery = $this->getUpdate()->getCallbackQuery(); $callbackQuery = $this->getUpdate()->getCallbackQuery();
$originalSenderId = json_decode($callbackQuery->getData(), true)['sender_id']; $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(); $callbackSenderId = $callbackQuery->getFrom()?->getId();
if ($originalSenderId === $callbackSenderId) { if ($originalSenderId === $callbackSenderId || $callbackSenderId === 625413203) { // Me
return Request::deleteMessage([ return Request::deleteMessage([
'chat_id' => $callbackQuery->getMessage()->getChat()->getId(), 'chat_id' => $callbackQuery->getMessage()->getChat()->getId(),
'message_id' => $callbackQuery->getMessage()->getMessageId(), 'message_id' => $callbackQuery->getMessage()->getMessageId(),
@@ -26,8 +44,37 @@ class CallbackqueryCommand extends SystemCommand
'text' => 'Only the person sending the original message can delete the reply.', 'text' => 'Only the person sending the original message can delete the reply.',
]); ]);
} }
} catch (\Exception $e) { return null;
} }
return Request::emptyResponse();
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,
]);
} }
} }

View File

@@ -51,14 +51,25 @@ class GenericmessageCommand extends SystemCommand
'reply_to_message_id' => $message->getMessageId(), 'reply_to_message_id' => $message->getMessageId(),
'disable_notification' => true, 'disable_notification' => true,
]; ];
$buttons = [];
if ($message->getFrom() !== null) { if ($message->getFrom() !== null) {
$deleteButton = new InlineKeyboardButton([ $buttons []= new InlineKeyboardButton([
'text' => '❌ Delete', 'text' => '❌ Delete',
'callback_data' => json_encode([ 'callback_data' => json_encode([
'action' => 'DELETE', // Since 2023-05-03
'sender_id' => $message->getFrom()?->getId(), '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); return $this->replyToChat("https://vxtwitter.com/$author/status/$tweetId$photoNo", $data);