TCrB Bot hübscher gemacht

This commit is contained in:
2026-04-06 09:24:31 +02:00
parent 23df0ed163
commit a70e46b307
6 changed files with 104 additions and 21 deletions
+3
View File
@@ -0,0 +1,3 @@
php-server.log
php-server.err.log
php-server-error.log
+23
View File
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
function telegramServiceFooter(): string
{
return 'Ein Service des Astronomiemuseums der Sternwarte Sonneberg: https://astronomiemuseum.de';
}
function appendTelegramServiceFooter(string $text): string
{
$footer = telegramServiceFooter();
$trimmedText = trim($text);
if ($trimmedText === '') {
return $footer;
}
if (str_contains($trimmedText, $footer)) {
return $trimmedText;
}
return $trimmedText . "\n\n" . $footer;
}
+34 -6
View File
@@ -30,12 +30,36 @@ $headerIntroPre = 'Variabler Stern';
$headerIntroTitle = 'T CrB Lichtkurve'; $headerIntroTitle = 'T CrB Lichtkurve';
$headerIntroSub = 'AAVSO-WebObs-Daten mit waehlbaren Filtern in einem gemeinsamen Plotly-Diagramm.'; $headerIntroSub = 'AAVSO-WebObs-Daten mit waehlbaren Filtern in einem gemeinsamen Plotly-Diagramm.';
$errors = []; $errors = [];
$eruptionIntervalDays = 227.5687;
$lastEruptionUtc = new DateTimeImmutable('1946-02-09 00:00:00', new DateTimeZone('UTC'));
$todayUtc = new DateTimeImmutable('now', new DateTimeZone('UTC'));
$secondsPerDay = 86400;
$eruptionIntervalSeconds = (int) round($eruptionIntervalDays * $secondsPerDay);
$elapsedSecondsSinceLastEruption = max(0, $todayUtc->getTimestamp() - $lastEruptionUtc->getTimestamp());
$elapsedIntervals = (int) floor($elapsedSecondsSinceLastEruption / $eruptionIntervalSeconds);
$nextPossibleEruptionUtc = $lastEruptionUtc->modify('+' . (($elapsedIntervals + 1) * $eruptionIntervalSeconds) . ' seconds');
$daysUntilNextEruption = ($nextPossibleEruptionUtc->getTimestamp() - $todayUtc->getTimestamp()) / $secondsPerDay;
function h(?string $value): string function h(?string $value): string
{ {
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8'); return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
} }
function formatGermanDate(?string $value, bool $includeTime = true): string
{
if ($value === null || trim($value) === '') {
return '—';
}
try {
$date = new DateTimeImmutable($value, new DateTimeZone('UTC'));
} catch (Exception) {
return $value;
}
return $date->format($includeTime ? 'd.m.Y H:i:s' : 'd.m.Y');
}
$filterRows = $pdo->query(" $filterRows = $pdo->query("
SELECT `filter_code`, COUNT(*) AS `entry_count` SELECT `filter_code`, COUNT(*) AS `entry_count`
FROM `aavso_tcrb_observations` FROM `aavso_tcrb_observations`
@@ -182,8 +206,8 @@ foreach ($chartDatasets as $dataset) {
$customData[] = [ $customData[] = [
$point['jd'], $point['jd'],
$point['observer_code'] !== '' ? $point['observer_code'] : '—', $point['observer_code'] !== '' ? $point['observer_code'] : '—',
$point['magnitude_error'] !== null ? number_format((float) $point['magnitude_error'], 3, '.', '') : '—', $point['magnitude_error'] !== null ? number_format((float) $point['magnitude_error'], 3, '.', '') : '—',
]; ];
} }
@@ -472,17 +496,21 @@ $plotlyPayload = [
<span>Messpunkte</span> <span>Messpunkte</span>
</div> </div>
<div class="tcrb-summary-box"> <div class="tcrb-summary-box">
<strong><?= h((string) $summary['min_utc']) ?></strong> <strong><?= h(formatGermanDate($summary['min_utc'])) ?></strong>
<span>Erster UTC-Zeitpunkt</span> <span>Erster Beobachtungszeitpunkt</span>
</div> </div>
<div class="tcrb-summary-box"> <div class="tcrb-summary-box">
<strong><?= h((string) $summary['max_utc']) ?></strong> <strong><?= h(formatGermanDate($summary['max_utc'])) ?></strong>
<span>Letzter UTC-Zeitpunkt</span> <span>Letzter Beobachtungszeitpunkt</span>
</div> </div>
<div class="tcrb-summary-box"> <div class="tcrb-summary-box">
<strong><?= number_format((float) $summary['min_mag'], 3, '.', '') ?> bis <?= number_format((float) $summary['max_mag'], 3, '.', '') ?></strong> <strong><?= number_format((float) $summary['min_mag'], 3, '.', '') ?> bis <?= number_format((float) $summary['max_mag'], 3, '.', '') ?></strong>
<span>Magnitudenbereich</span> <span>Magnitudenbereich</span>
</div> </div>
<div class="tcrb-summary-box">
<strong><?= h($nextPossibleEruptionUtc->format('d.m.Y')) ?></strong>
<span>Naechster moeglicher Ausbruch<br>(in <?= number_format($daysUntilNextEruption, 1, ',', '.') ?> Tagen)</span>
</div>
</div> </div>
<div class="tcrb-chart-wrap"> <div class="tcrb-chart-wrap">
+2 -1
View File
@@ -14,6 +14,7 @@ require_once __DIR__ . '/../auth_helpers.php';
$config = require __DIR__ . '/../../config/database.php'; $config = require __DIR__ . '/../../config/database.php';
$telegramConfig = require __DIR__ . '/../../config/telegram.php'; $telegramConfig = require __DIR__ . '/../../config/telegram.php';
require_once __DIR__ . '/../../config/telegram_message_footer.php';
$dsn = sprintf( $dsn = sprintf(
'mysql:host=%s;dbname=%s;charset=%s', 'mysql:host=%s;dbname=%s;charset=%s',
@@ -217,7 +218,7 @@ function sendTelegramMessage(string $botToken, string $chatId, string $messageTe
'https://api.telegram.org/bot' . $botToken . '/sendMessage', 'https://api.telegram.org/bot' . $botToken . '/sendMessage',
[ [
'chat_id' => $chatId, 'chat_id' => $chatId,
'text' => $messageText, 'text' => appendTelegramServiceFooter($messageText),
] ]
); );
+2 -1
View File
@@ -6,6 +6,7 @@ error_reporting(E_ALL);
$config = require __DIR__ . '/../../config/database.php'; $config = require __DIR__ . '/../../config/database.php';
$telegramConfig = require __DIR__ . '/../../config/telegram.php'; $telegramConfig = require __DIR__ . '/../../config/telegram.php';
require_once __DIR__ . '/../../config/telegram_message_footer.php';
$dsn = sprintf( $dsn = sprintf(
'mysql:host=%s;dbname=%s;charset=%s', 'mysql:host=%s;dbname=%s;charset=%s',
@@ -87,7 +88,7 @@ function telegramSendBotMessage(array $telegramConfig, string $chatId, string $m
'https://api.telegram.org/bot' . $botToken . '/sendMessage', 'https://api.telegram.org/bot' . $botToken . '/sendMessage',
[ [
'chat_id' => $chatId, 'chat_id' => $chatId,
'text' => $messageText, 'text' => appendTelegramServiceFooter($messageText),
] ]
); );
} }
+39 -12
View File
@@ -22,6 +22,7 @@ $rootDir = dirname(__DIR__, 2);
$stateFile = __DIR__ . DIRECTORY_SEPARATOR . 'telegram_cron_state.json'; $stateFile = __DIR__ . DIRECTORY_SEPARATOR . 'telegram_cron_state.json';
$dbConfig = require $rootDir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database.php'; $dbConfig = require $rootDir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database.php';
$telegramConfig = require $rootDir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'telegram.php'; $telegramConfig = require $rootDir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'telegram.php';
require_once $rootDir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'telegram_message_footer.php';
$dsn = sprintf( $dsn = sprintf(
'mysql:host=%s;port=%d;dbname=%s;charset=%s', 'mysql:host=%s;port=%d;dbname=%s;charset=%s',
@@ -492,32 +493,58 @@ function fetchTelegramRecipients(PDO $pdo): array
function buildTcrbLatestVMessage(array $observation, DateTimeImmutable $nowBerlin): string function buildTcrbLatestVMessage(array $observation, DateTimeImmutable $nowBerlin): string
{ {
$observedUtc = trim((string) ($observation['observed_at_utc'] ?? '')); $observedUtc = trim((string) ($observation['observed_at_utc'] ?? ''));
$observedText = $observedUtc !== '' ? $observedUtc . ' UTC' : 'unbekannt'; $observedText = formatGermanDateTime($observedUtc);
$magnitude = formatDecimal($observation['magnitude'] ?? null, 3); $magnitude = formatDecimal($observation['magnitude'] ?? null, 3);
$magnitudeError = $observation['magnitude_error'] !== null $magnitudeError = $observation['magnitude_error'] !== null
? ' +/- ' . formatDecimal($observation['magnitude_error'], 3) ? ' ± ' . formatDecimal($observation['magnitude_error'], 3)
: ''; : '';
$jd = formatDecimal($observation['jd'] ?? null, 6); $jd = formatDecimal($observation['jd'] ?? null, 6);
$observerCode = trim((string) ($observation['observer_code'] ?? '')); $observerCode = trim((string) ($observation['observer_code'] ?? ''));
$nextPossibleEruptionDate = calculateNextPossibleTcrbEruptionDate($nowBerlin);
$lines = [ $lines = [
'SkyView T CrB Update', 'SkyViews TCrBotti mit dem Update',
'', '',
'Magnitude: ' . $magnitude . $magnitudeError, 'Magnitude: ' . $magnitude . $magnitudeError,
'Zeitpunkt: ' . $observedText, 'Zeitpunkt: ' . $observedText,
'JD: ' . $jd, 'JD: ' . $jd,
'Nächster möglicher Ausbruch:',
$nextPossibleEruptionDate,
]; ];
if ($observerCode !== '') {
$lines[] = 'Beobachter: ' . $observerCode;
}
$lines[] = '';
$lines[] = 'Stand: ' . $nowBerlin->format('d.m.Y H:i') . ' Europe/Berlin';
return implode("\n", $lines); return implode("\n", $lines);
} }
function calculateNextPossibleTcrbEruptionDate(DateTimeImmutable $referenceDate): string
{
$eruptionIntervalDays = 227.5687;
$lastEruptionUtc = new DateTimeImmutable('1946-02-09 00:00:00', new DateTimeZone('UTC'));
$referenceUtc = $referenceDate->setTimezone(new DateTimeZone('UTC'));
$secondsPerDay = 86400;
$eruptionIntervalSeconds = (int) round($eruptionIntervalDays * $secondsPerDay);
$elapsedSecondsSinceLastEruption = max(0, $referenceUtc->getTimestamp() - $lastEruptionUtc->getTimestamp());
$elapsedIntervals = (int) floor($elapsedSecondsSinceLastEruption / $eruptionIntervalSeconds);
$nextPossibleEruptionUtc = $lastEruptionUtc->modify('+' . (($elapsedIntervals + 1) * $eruptionIntervalSeconds) . ' seconds');
return $nextPossibleEruptionUtc->format('d.m.Y');
}
function formatGermanDateTime(?string $value): string
{
if ($value === null || trim($value) === '') {
return 'unbekannt';
}
try {
$date = new DateTimeImmutable($value, new DateTimeZone('UTC'));
} catch (Exception) {
return $value;
}
return $date->format('d.m.Y H:i:s') . ' UTC';
}
function buildTcrbVChartSvg(array $observations, array $latestObservation): string function buildTcrbVChartSvg(array $observations, array $latestObservation): string
{ {
$width = 1200; $width = 1200;
@@ -1051,7 +1078,7 @@ function telegramSendBotMessage(array $telegramConfig, string $chatId, string $m
'https://api.telegram.org/bot' . $botToken . '/sendMessage', 'https://api.telegram.org/bot' . $botToken . '/sendMessage',
[ [
'chat_id' => $chatId, 'chat_id' => $chatId,
'text' => $messageText, 'text' => appendTelegramServiceFooter($messageText),
] ]
); );
@@ -1069,7 +1096,7 @@ function telegramSendBotDocument(array $telegramConfig, string $chatId, string $
'https://api.telegram.org/bot' . $botToken . '/sendDocument', 'https://api.telegram.org/bot' . $botToken . '/sendDocument',
[ [
'chat_id' => $chatId, 'chat_id' => $chatId,
'caption' => $caption, 'caption' => appendTelegramServiceFooter($caption),
], ],
'document', 'document',
$filePath, $filePath,
@@ -1090,7 +1117,7 @@ function telegramSendBotPhoto(array $telegramConfig, string $chatId, string $fil
'https://api.telegram.org/bot' . $botToken . '/sendPhoto', 'https://api.telegram.org/bot' . $botToken . '/sendPhoto',
[ [
'chat_id' => $chatId, 'chat_id' => $chatId,
'caption' => $caption, 'caption' => appendTelegramServiceFooter($caption),
], ],
'photo', 'photo',
$filePath, $filePath,