Deploy on 2026-06-05 16:08:53
This commit is contained in:
@@ -13,7 +13,7 @@ class AiAnalyzer
|
||||
{
|
||||
$config = require __DIR__ . '/../../../config/ai.php';
|
||||
$this->apiKey = $config['gemini']['api_key'] ?? null;
|
||||
$this->model = $config['gemini']['model'] ?? 'gemini-1.5-flash-latest';
|
||||
$this->model = $config['gemini']['model'] ?? 'gemini-flash-lite-latest';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Services\Crawler;
|
||||
|
||||
use App\Services\Database\Connection;
|
||||
use App\Services\Database\ActivityLogger;
|
||||
use App\Services\Notification\TelegramNotifier;
|
||||
use PDO;
|
||||
use Throwable;
|
||||
|
||||
@@ -13,17 +14,20 @@ class Collector
|
||||
private RssParser $rssParser;
|
||||
private AiAnalyzer $aiAnalyzer;
|
||||
private ActivityLogger $logger;
|
||||
private TelegramNotifier $notifier;
|
||||
|
||||
public function __construct(
|
||||
Connection $connection,
|
||||
RssParser $rssParser,
|
||||
AiAnalyzer $aiAnalyzer,
|
||||
ActivityLogger $logger
|
||||
ActivityLogger $logger,
|
||||
TelegramNotifier $notifier
|
||||
) {
|
||||
$this->pdo = $connection->getPdo();
|
||||
$this->rssParser = $rssParser;
|
||||
$this->aiAnalyzer = $aiAnalyzer;
|
||||
$this->logger = $logger;
|
||||
$this->notifier = $notifier;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,6 +80,12 @@ class Collector
|
||||
'new_organizations' => $results['new_organizations'],
|
||||
]));
|
||||
|
||||
// Send Telegram notification if enabled
|
||||
if ($this->getSetting('telegram_enabled') === '1') {
|
||||
$this->notifier->loadSettings();
|
||||
$this->notifier->notifyCollectorResults($results);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
@@ -203,6 +213,25 @@ class Collector
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger Telegram notification if enabled
|
||||
if ($this->getSetting('telegram_enabled') === '1') {
|
||||
$orgName = '';
|
||||
if ($orgId) {
|
||||
$orgStmt = $this->pdo->prepare("SELECT name FROM organizations WHERE id = ?");
|
||||
$orgStmt->execute([$orgId]);
|
||||
$orgName = $orgStmt->fetchColumn() ?: '';
|
||||
}
|
||||
$this->notifier->loadSettings();
|
||||
$this->notifier->notifyNewOpportunity([
|
||||
'title' => $entry['title'],
|
||||
'type' => $analysis['opportunity_type'] ?? $analysis['type'] ?? 'other',
|
||||
'score' => $score,
|
||||
'url' => $entry['url'],
|
||||
'description' => $analysis['summary'] ?? $entry['description'],
|
||||
'org_name' => $orgName,
|
||||
]);
|
||||
}
|
||||
|
||||
} catch (Throwable $e) {
|
||||
// Log but don't fail
|
||||
}
|
||||
@@ -246,4 +275,19 @@ class Collector
|
||||
);
|
||||
return $stmt->fetchAll() ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a setting by key from the database.
|
||||
*/
|
||||
private function getSetting(string $key): ?string
|
||||
{
|
||||
try {
|
||||
$stmt = $this->pdo->prepare("SELECT `value` FROM settings WHERE `key` = ?");
|
||||
$stmt->execute([$key]);
|
||||
$val = $stmt->fetchColumn();
|
||||
return $val !== false ? $val : null;
|
||||
} catch (Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,21 +2,49 @@
|
||||
|
||||
namespace App\Services\Notification;
|
||||
|
||||
use App\Services\Database\Connection;
|
||||
use PDO;
|
||||
use Throwable;
|
||||
|
||||
class TelegramNotifier
|
||||
{
|
||||
private ?string $botToken;
|
||||
private ?string $chatId;
|
||||
private PDO $pdo;
|
||||
private ?string $botToken = null;
|
||||
private ?string $chatId = null;
|
||||
private ?string $lastError = null;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
$this->botToken = $_ENV['TELEGRAM_BOT_TOKEN'] ?? null;
|
||||
$this->chatId = $_ENV['TELEGRAM_CHAT_ID'] ?? null;
|
||||
$this->pdo = $connection->getPdo();
|
||||
$this->loadSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure from database settings.
|
||||
* Load settings from database settings table.
|
||||
*/
|
||||
public function loadSettings(): void
|
||||
{
|
||||
$this->botToken = $this->getSetting('telegram_bot_token');
|
||||
$this->chatId = $this->getSetting('telegram_chat_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to retrieve setting by key.
|
||||
*/
|
||||
private function getSetting(string $key): ?string
|
||||
{
|
||||
try {
|
||||
$stmt = $this->pdo->prepare("SELECT `value` FROM settings WHERE `key` = ?");
|
||||
$stmt->execute([$key]);
|
||||
$val = $stmt->fetchColumn();
|
||||
return $val !== false ? $val : null;
|
||||
} catch (Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure manually (for testing unsaved parameters).
|
||||
*/
|
||||
public function configure(string $botToken, string $chatId): void
|
||||
{
|
||||
@@ -24,12 +52,23 @@ class TelegramNotifier
|
||||
$this->chatId = $chatId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the last HTTP or API error message.
|
||||
*/
|
||||
public function getLastError(): ?string
|
||||
{
|
||||
return $this->lastError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a notification to Telegram.
|
||||
*/
|
||||
public function send(string $message, string $type = 'info'): bool
|
||||
{
|
||||
$this->lastError = null;
|
||||
|
||||
if (!$this->botToken || !$this->chatId) {
|
||||
$this->lastError = "Telegram token or chat ID is missing.";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -54,19 +93,62 @@ class TelegramNotifier
|
||||
'disable_web_page_preview' => true,
|
||||
]);
|
||||
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => "Content-Type: application/json\r\n",
|
||||
'content' => $payload,
|
||||
'timeout' => 10,
|
||||
],
|
||||
'ssl' => ['verify_peer' => false, 'verify_peer_name' => false],
|
||||
]);
|
||||
if (function_exists('curl_init')) {
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
|
||||
$response = @file_get_contents($url, false, $context);
|
||||
return $response !== false;
|
||||
$response = curl_exec($ch);
|
||||
if ($response === false) {
|
||||
$this->lastError = "Connection error: " . curl_error($ch);
|
||||
curl_close($ch);
|
||||
return false;
|
||||
}
|
||||
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
$resDecoded = json_decode($response, true);
|
||||
$this->lastError = $resDecoded['description'] ?? "API error HTTP {$httpCode}";
|
||||
error_log("Telegram API returned code {$httpCode}: {$response}");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => "Content-Type: application/json\r\n",
|
||||
'content' => $payload,
|
||||
'timeout' => 10,
|
||||
],
|
||||
'ssl' => ['verify_peer' => false, 'verify_peer_name' => false],
|
||||
]);
|
||||
|
||||
$response = @file_get_contents($url, false, $context);
|
||||
if ($response === false) {
|
||||
$error = error_get_last();
|
||||
$this->lastError = $error['message'] ?? "Connection timed out.";
|
||||
return false;
|
||||
}
|
||||
|
||||
$resDecoded = json_decode($response, true);
|
||||
if (($resDecoded['ok'] ?? false) === false) {
|
||||
$this->lastError = $resDecoded['description'] ?? "API error";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$this->lastError = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user