124 lines
4.2 KiB
PHP
Executable File
124 lines
4.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Telegram\Actions;
|
|
|
|
use Telegram\TelegramSender;
|
|
use Telegram\Converters\HtmlToTelegramHtmlConverter;
|
|
|
|
class TelegramActions
|
|
{
|
|
private TelegramSender $sender;
|
|
private HtmlToTelegramHtmlConverter $converter;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->sender = new TelegramSender();
|
|
$this->converter = new HtmlToTelegramHtmlConverter();
|
|
}
|
|
|
|
public function sendTemplate(int $chatId, string $htmlContent, ?string $command = null): array
|
|
{
|
|
$content = $this->converter->convert($htmlContent);
|
|
$images = $this->converter->extractImages($htmlContent);
|
|
|
|
if (!empty($images)) {
|
|
$contentWithoutImages = $this->converter->removeImages($htmlContent);
|
|
$content = $this->converter->convert($contentWithoutImages);
|
|
|
|
if (count($images) === 1) {
|
|
return $this->sender->sendPhoto($chatId, $images[0], $content);
|
|
}
|
|
|
|
return $this->sender->sendMediaGroup($chatId, $images, $content);
|
|
}
|
|
|
|
return $this->sender->sendMessage($chatId, $content);
|
|
}
|
|
|
|
public function sendScheduledMessage(int $chatId, string $htmlContent, ?array $buttons = null): array
|
|
{
|
|
$content = $this->converter->convert($htmlContent);
|
|
$images = $this->converter->extractImages($htmlContent);
|
|
|
|
if (!empty($images)) {
|
|
$contentWithoutImages = $this->converter->removeImages($htmlContent);
|
|
$content = $this->converter->convert($contentWithoutImages);
|
|
|
|
if ($buttons) {
|
|
$keyboard = $this->sender->createInlineKeyboard($buttons);
|
|
if (count($images) === 1) {
|
|
return $this->sender->sendPhoto($chatId, $images[0], $content, $keyboard);
|
|
}
|
|
return $this->sender->sendMediaGroup($chatId, $images, $content);
|
|
}
|
|
|
|
if (count($images) === 1) {
|
|
return $this->sender->sendPhoto($chatId, $images[0], $content);
|
|
}
|
|
|
|
return $this->sender->sendMediaGroup($chatId, $images, $content);
|
|
}
|
|
|
|
return $this->sender->sendMessage($chatId, $content, $buttons);
|
|
}
|
|
|
|
public function sendWithTranslation(int $chatId, string $htmlContent, array $translations): array
|
|
{
|
|
$content = $this->converter->convert($htmlContent);
|
|
|
|
$buttons = [];
|
|
foreach ($translations as $lang => $translatedText) {
|
|
$buttons[] = [
|
|
'text' => "🌐 " . strtoupper($lang),
|
|
'callback_data' => "translate:{$lang}"
|
|
];
|
|
}
|
|
|
|
$keyboard = $this->sender->createInlineKeyboard($buttons);
|
|
|
|
return $this->sender->sendMessage($chatId, $content, $keyboard);
|
|
}
|
|
|
|
public function translateMessage(int $chatId, string $originalText, string $targetLang, string $translatedText): array
|
|
{
|
|
$message = "🌐 <b>Traducción (" . strtoupper($targetLang) . ")</b>\n\n";
|
|
$message .= $translatedText;
|
|
|
|
$message .= "\n\n━━━━━━━━━━━━━━━━━\n";
|
|
$message .= "<i>Original:</i>\n" . $originalText;
|
|
|
|
return $this->sender->sendMessage($chatId, $message);
|
|
}
|
|
|
|
public function handleCallbackQuery(array $callbackData): array
|
|
{
|
|
$parts = explode(':', $callbackData['data']);
|
|
$action = $parts[0] ?? '';
|
|
|
|
return match ($action) {
|
|
'translate' => [
|
|
'action' => 'translate',
|
|
'lang' => $parts[1] ?? null
|
|
],
|
|
default => ['action' => 'unknown']
|
|
};
|
|
}
|
|
|
|
public function sendWelcomeMessage(int $chatId, string $userName, ?string $welcomeText = null, ?string $buttonText = null, ?string $groupLink = null): array
|
|
{
|
|
$message = $welcomeText ?? "¡Hola {$userName}! 👋\n\nUsa /comandos para ver los comandos disponibles.";
|
|
|
|
$buttons = [];
|
|
if ($buttonText && $groupLink) {
|
|
$buttons[] = [
|
|
'text' => $buttonText,
|
|
'url' => $groupLink
|
|
];
|
|
}
|
|
|
|
$keyboard = !empty($buttons) ? $this->sender->createInlineKeyboard($buttons) : null;
|
|
|
|
return $this->sender->sendMessage($chatId, $message, $keyboard);
|
|
}
|
|
}
|