Telegram Cron umgebaut
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Task-Definitionen fuer den Telegram-Cron.
|
||||
*
|
||||
* Diese Datei enthaelt die Liste der Cron-Aufgaben mit Zeitplan, Aktivierung,
|
||||
* Anzeigename und Handler-Closure. Die eigentliche fachliche Logik ruft dabei
|
||||
* Funktionen aus den Hilfsdateien auf, damit die Task-Liste selbst gut lesbar bleibt.
|
||||
*/
|
||||
|
||||
return [
|
||||
[
|
||||
'key' => 'send_tcrb_latest_v',
|
||||
'label' => 'Letzten T-CrB-Wert mit Filter V senden',
|
||||
'enabled' => true,
|
||||
'schedule' => 'every-minute',
|
||||
'handler' => static function (PDO $pdo, array $telegramConfig, DateTimeImmutable $nowBerlin) use ($stateFile): array {
|
||||
$latestObservation = fetchLatestTcrbObservationByFilter($pdo, 'V');
|
||||
if ($latestObservation === null) {
|
||||
return [
|
||||
'sent' => 0,
|
||||
'updated' => 0,
|
||||
'message' => 'Keine T-CrB-Beobachtung mit Filter V gefunden.',
|
||||
];
|
||||
}
|
||||
|
||||
$state = loadCronState($stateFile);
|
||||
$taskKey = 'send_tcrb_latest_v';
|
||||
$currentSignature = buildTcrbObservationSignature($latestObservation);
|
||||
$lastSignature = (string) (($state[$taskKey]['last_signature'] ?? ''));
|
||||
|
||||
if ($currentSignature !== '' && $currentSignature === $lastSignature) {
|
||||
return [
|
||||
'sent' => 0,
|
||||
'updated' => 0,
|
||||
'message' => 'Kein neuer V-Messpunkt seit der letzten Telegram-Nachricht.',
|
||||
];
|
||||
}
|
||||
|
||||
$recipients = fetchTelegramRecipients($pdo);
|
||||
if ($recipients === []) {
|
||||
return [
|
||||
'sent' => 0,
|
||||
'updated' => 0,
|
||||
'message' => 'Keine verbundenen Telegram-Empfaenger vorhanden.',
|
||||
];
|
||||
}
|
||||
|
||||
$messageText = buildTcrbLatestVMessage($latestObservation, $nowBerlin);
|
||||
$chartObservations = fetchRecentTcrbObservationsByFilterWindow($pdo, 'V', '-2 months');
|
||||
$chartSvg = buildTcrbVChartSvg($chartObservations, $latestObservation);
|
||||
$chartPackage = createTelegramChartPackage($chartSvg);
|
||||
$sentCount = 0;
|
||||
$chartSentCount = 0;
|
||||
|
||||
try {
|
||||
foreach ($recipients as $recipient) {
|
||||
$chatId = trim((string) ($recipient['telegram_chat_id'] ?? ''));
|
||||
if ($chatId === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$messageSent = telegramSendBotMessage($telegramConfig, $chatId, $messageText);
|
||||
$chartSent = false;
|
||||
if (($chartPackage['kind'] ?? '') === 'photo') {
|
||||
$chartSent = telegramSendBotPhoto(
|
||||
$telegramConfig,
|
||||
$chatId,
|
||||
(string) $chartPackage['path'],
|
||||
'T CrB V-Diagramm'
|
||||
);
|
||||
} elseif (($chartPackage['kind'] ?? '') === 'document') {
|
||||
$chartSent = telegramSendBotDocument(
|
||||
$telegramConfig,
|
||||
$chatId,
|
||||
(string) $chartPackage['path'],
|
||||
'T CrB V-Diagramm'
|
||||
);
|
||||
}
|
||||
|
||||
if ($chartSent) {
|
||||
$chartSentCount++;
|
||||
}
|
||||
|
||||
if ($messageSent || $chartSent) {
|
||||
$sentCount++;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
cleanupTelegramChartPackage($chartPackage);
|
||||
}
|
||||
|
||||
if ($sentCount < 1) {
|
||||
return [
|
||||
'sent' => 0,
|
||||
'updated' => 0,
|
||||
'message' => 'Telegram-Versand konnte keinen Empfaenger erfolgreich erreichen.',
|
||||
];
|
||||
}
|
||||
|
||||
$state[$taskKey] = [
|
||||
'last_signature' => $currentSignature,
|
||||
'last_sent_at' => $nowBerlin->format(DateTimeInterface::ATOM),
|
||||
'last_observed_at_utc' => (string) ($latestObservation['observed_at_utc'] ?? ''),
|
||||
'last_filter' => 'V',
|
||||
];
|
||||
saveCronState($stateFile, $state);
|
||||
|
||||
return [
|
||||
'sent' => $sentCount,
|
||||
'updated' => 0,
|
||||
'message' => sprintf(
|
||||
'T-CrB V-Messpunkt %s an %d Telegram-Empfaenger gesendet, Diagramm an %d.',
|
||||
(string) ($latestObservation['observed_at_utc'] ?? 'unbekannt'),
|
||||
$sentCount,
|
||||
$chartSentCount
|
||||
),
|
||||
];
|
||||
},
|
||||
],
|
||||
[
|
||||
'key' => 'cleanup_expired_link_tokens',
|
||||
'label' => 'Abgelaufene Telegram-Verknuepfungscodes aufraeumen',
|
||||
'enabled' => true,
|
||||
'schedule' => 'every-15-minutes',
|
||||
'handler' => static function (PDO $pdo, array $telegramConfig, DateTimeImmutable $nowBerlin): array {
|
||||
$stmt = $pdo->prepare("
|
||||
UPDATE `app_users`
|
||||
SET
|
||||
`telegram_link_token` = NULL,
|
||||
`telegram_link_token_expires_at` = NULL,
|
||||
`updated_at` = NOW()
|
||||
WHERE `telegram_link_token` IS NOT NULL
|
||||
AND `telegram_link_token` <> ''
|
||||
AND `telegram_link_token_expires_at` IS NOT NULL
|
||||
AND `telegram_link_token_expires_at` < NOW()
|
||||
");
|
||||
$stmt->execute();
|
||||
|
||||
return [
|
||||
'sent' => 0,
|
||||
'updated' => (int) $stmt->rowCount(),
|
||||
'message' => 'Abgelaufene Verbindungscodes bereinigt.',
|
||||
];
|
||||
},
|
||||
],
|
||||
[
|
||||
'key' => 'daily_morning_report_placeholder',
|
||||
'label' => 'Morgendlichen Telegram-Bericht vorbereiten',
|
||||
'enabled' => false,
|
||||
'schedule' => 'daily-08:00',
|
||||
'handler' => static function (PDO $pdo, array $telegramConfig, DateTimeImmutable $nowBerlin): array {
|
||||
return [
|
||||
'sent' => 0,
|
||||
'updated' => 0,
|
||||
'message' => 'Platzhalter fuer den spaeteren Morgenbericht.',
|
||||
];
|
||||
},
|
||||
],
|
||||
[
|
||||
'key' => 'daily_evening_report_placeholder',
|
||||
'label' => 'Abendlichen Telegram-Bericht vorbereiten',
|
||||
'enabled' => false,
|
||||
'schedule' => 'daily-19:30',
|
||||
'handler' => static function (PDO $pdo, array $telegramConfig, DateTimeImmutable $nowBerlin): array {
|
||||
return [
|
||||
'sent' => 0,
|
||||
'updated' => 0,
|
||||
'message' => 'Platzhalter fuer den spaeteren Abendbericht.',
|
||||
];
|
||||
},
|
||||
],
|
||||
[
|
||||
'key' => 'connected_users_heartbeat_placeholder',
|
||||
'label' => 'Verbundene Telegram-Nutzer pruefen',
|
||||
'enabled' => false,
|
||||
'schedule' => 'hourly-minute-05',
|
||||
'handler' => static function (PDO $pdo, array $telegramConfig, DateTimeImmutable $nowBerlin): array {
|
||||
$count = (int) $pdo->query("
|
||||
SELECT COUNT(*)
|
||||
FROM `app_users`
|
||||
WHERE `telegram_chat_id` IS NOT NULL
|
||||
AND `telegram_chat_id` <> ''
|
||||
")->fetchColumn();
|
||||
|
||||
return [
|
||||
'sent' => 0,
|
||||
'updated' => 0,
|
||||
'message' => 'Aktuell verbundene Telegram-Nutzer: ' . $count,
|
||||
];
|
||||
},
|
||||
],
|
||||
];
|
||||
Reference in New Issue
Block a user