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;
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,
]);
}
}