File: /var/www/heifetz/heifetz-app/models/Modules/TelegramModule.php
<?php
declare(strict_types=1);
namespace Models\Modules;
use Core\Models\CoreHelper;
use Helpers\LogHelper;
use Telegram\Bot\Api;
class TelegramModule extends AbstractModule
{
const NAME = 'telegram';
const DATA = [
'title' => 'Телеграм бот',
'description' => 'Модуль обеспечивает работу телеграм бота',
'image' => '/assets/modules/telegram.png',
'template' => 'telegram',
];
const FIELD_ERROR_API_KEY = 'error_api_key';
const FIELD_ERROR_USERS_IDS = 'error_users_ids';
const FIELD_ERROR_CHAT_ID = 'error_chat_id';
const FIELD_API_KEY = 'api_key';
const FIELD_BOT_NAME = 'bot_name';
public static function getErrorApiKey(): ?string
{
return self::getConfig()[self::FIELD_ERROR_API_KEY] ?? null;
}
public static function getErrorUsersIds(): ?array
{
return self::getConfig()[self::FIELD_ERROR_USERS_IDS] ?? null;
}
public static function getErrorChatId(): ?string
{
return self::getConfig()[self::FIELD_ERROR_CHAT_ID] ?? null;
}
public static function getApiKey(): ?string
{
return self::getConfig()[self::FIELD_API_KEY] ?? null;
}
public static function getBotName(): ?string
{
return self::getConfig()[self::FIELD_BOT_NAME] ?? null;
}
public static function isTelegramNotifierActive(): bool
{
return self::isActive() && !empty(self::getApiKey());
}
public static function updateWebhook(string $apiKey): void
{
$bot = new Api($apiKey);
$url = CoreHelper::$domain . '/api/v1/telegram/webhook';
$result = $bot->setWebhook(compact('url'));
$logger = LogHelper::create('setWebhook', 'TelegramApi');
$logger->info($result ? 'Webhook updated.' : 'Webhook not updated.');
}
}