<?php
namespace App\Library\TelegramBot;
use App\Entity\Image;
use App\Entity\TelegramUser;
use App\Exception\TelegramBot\MessageEditedBySameTextException;
use App\Exception\TelegramBot\MessageToEditNotFoundException;
use App\Library\TelegramBot\TelegramObject\Update;
use App\Library\Utils\Other\Other;
use App\Service\EntityManagerService;
use App\Service\Image\ImageService;
use App\Service\TelegramBot\TelegramBotService;
use App\Service\TelegramBotNavigation\TelegramBotNavigationService;
use App\Service\TelegramMessage\TelegramMessageService;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
abstract class BaseTelegramBot extends AbstractTelegramBot
{
/**
* @var string
*/
private $innerBotId;
/**
* @var TelegramBotService
*/
protected $telegramBotService;
/**
* @var TelegramBotNavigationService
*/
protected $telegramBotNavigationService;
/**
* @var ImageService
*/
protected $imageService;
public function __construct(LoggerInterface $logger, EntityManagerInterface $em,
TelegramMessageService $telegramMessageService, EntityManagerService $emService,
TelegramBotService $telegramBotService,
TelegramBotNavigationService $telegramBotNavigationService,
ImageService $imageService)
{
parent::__construct($logger, $em, $telegramMessageService, $emService);
$this->telegramBotService = $telegramBotService;
$this->telegramBotNavigationService = $telegramBotNavigationService;
$this->imageService = $imageService;
}
public function getInnerBotId(): ?string
{
if (!$this->innerBotId) {
throw new \Exception("Inner bot id is not set");
}
return $this->innerBotId;
}
protected function setInnerBotId(string $botId): void
{
$this->innerBotId = $botId;
}
public function getTelegramBotEntity(): \App\Entity\TelegramBot
{
return $this->telegramBotService->createOrGetDefault(["innerBotId" => $this->getInnerBotId()]);
}
public function setNavigationItems(TelegramUser $user, string $navigationGroupId, array $navigationItems): void
{
foreach ($navigationItems as $key => $value) {
if (strpos($key, $navigationGroupId . "__") !== 0) {
throw new \Exception("Navigation item keys should start with " . $navigationGroupId . "__");
}
}
$nav = $this->telegramBotNavigationService->getOrCreateNavigation($user, $this->getInnerBotId());
$nav->setNavigationGroupData($navigationGroupId, $navigationItems);
$this->em->flush();
}
public function getNavigationItems(TelegramUser $user, string $navigationGroupId): ?array
{
$nav = $this->telegramBotNavigationService->getOrCreateNavigation($user, $this->getInnerBotId());
return $nav->getNavigationGroupData($navigationGroupId);
}
public function getNavigationData(TelegramUser $user): ?array
{
$nav = $this->telegramBotNavigationService->getOrCreateNavigation($user, $this->getInnerBotId());
return $nav->getData();
}
public function setNavigationSelectedItemId(TelegramUser $user, string $navigationGroupId, ?string $selectedItemId): void
{
$nav = $this->telegramBotNavigationService->getOrCreateNavigation($user, $this->getInnerBotId());
$nav->setSelectedItemId($navigationGroupId, $selectedItemId);
$this->em->flush();
}
public function getNavigationSelectedItem(TelegramUser $user, string $navigationGroupId)
{
$nav = $this->telegramBotNavigationService->getOrCreateNavigation($user, $this->getInnerBotId());
return $nav->getSelectedItem($navigationGroupId);
}
public function setNavigationSelectedItemIdByUpdate(TelegramUser $user, Update $update): void
{
$selectedItemId = $update->getCallbackQuery()->getDataField();
$navigationGroupId = $this->getNavigationMenuIdByUpdate($update);
$this->setNavigationSelectedItemId($user, $navigationGroupId, $selectedItemId);
$this->em->flush();
}
public function getNavigationMenuIdByUpdate(Update $update): string
{
$selectedItemId = $update->getCallbackQuery()->getDataField();
$navigationGroupId = explode("__", $selectedItemId)[0];
return $navigationGroupId;
}
public function downloadMessagePhoto(Update $update): Image
{
$path = parent::downloadMessagePhoto($update);
$image = $this->imageService->createOrGetDefault(["path" => $path]);
return $image;
}
public function setEditableMessageId(TelegramUser $user, string $messageId, ?string $telegramMessageId): self
{
$nav = $this->telegramBotNavigationService->getOrCreateNavigation($user, $this->getInnerBotId());
$nav->setEditableMessageId($messageId, $telegramMessageId);
$this->em->flush();
return $this;
}
public function getEditableMessageId(TelegramUser $user, string $messageId): ?string
{
$nav = $this->telegramBotNavigationService->getOrCreateNavigation($user, $this->getInnerBotId());
return $nav->getEditableMessageId($messageId);
}
public function editOrSendEditableMessage(TelegramUser $user, string $chatId, string $messageId,
string $text = null, array $replyMarkup = null, string $imageFileId = null): void
{
$telegramMessageId = $this->getEditableMessageId($user, $messageId);
try {
$this->editRemoteMessage($chatId, $telegramMessageId, $text, $replyMarkup, $imageFileId);
} catch (MessageToEditNotFoundException $e) {
$telegramMessageId = null;
$sentMessage = $this->sendMessage($chatId, $text, $replyMarkup, $imageFileId);
$telegramMessageId = $sentMessage->getMessageId();
$this->setEditableMessageId($user, $messageId, $telegramMessageId);
} catch (MessageEditedBySameTextException $e) {
//do nothing
}
}
}