Support refreshing preview
This commit is contained in:
@@ -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,9 +13,25 @@ class CallbackqueryCommand extends SystemCommand
|
||||
{
|
||||
try {
|
||||
$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();
|
||||
if ($originalSenderId === $callbackSenderId) {
|
||||
if ($originalSenderId === $callbackSenderId || $callbackSenderId === 625413203) { // Me
|
||||
return Request::deleteMessage([
|
||||
'chat_id' => $callbackQuery->getMessage()->getChat()->getId(),
|
||||
'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.',
|
||||
]);
|
||||
}
|
||||
} 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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user