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';
$headerIntroSub = 'AAVSO-WebObs-Daten mit waehlbaren Filtern in einem gemeinsamen Plotly-Diagramm.';
$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
{
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("
SELECT `filter_code`, COUNT(*) AS `entry_count`
FROM `aavso_tcrb_observations`
@@ -182,8 +206,8 @@ foreach ($chartDatasets as $dataset) {
$customData[] = [
$point['jd'],
$point['observer_code'] !== '' ? $point['observer_code'] : '—',
$point['magnitude_error'] !== null ? number_format((float) $point['magnitude_error'], 3, '.', '') : '—',
$point['observer_code'] !== '' ? $point['observer_code'] : '—',
$point['magnitude_error'] !== null ? number_format((float) $point['magnitude_error'], 3, '.', '') : '—',
];
}
@@ -472,17 +496,21 @@ $plotlyPayload = [
<span>Messpunkte</span>
</div>
<div class="tcrb-summary-box">
<strong><?= h((string) $summary['min_utc']) ?></strong>
<span>Erster UTC-Zeitpunkt</span>
<strong><?= h(formatGermanDate($summary['min_utc'])) ?></strong>
<span>Erster Beobachtungszeitpunkt</span>
</div>
<div class="tcrb-summary-box">
<strong><?= h((string) $summary['max_utc']) ?></strong>
<span>Letzter UTC-Zeitpunkt</span>
<strong><?= h(formatGermanDate($summary['max_utc'])) ?></strong>
<span>Letzter Beobachtungszeitpunkt</span>
</div>
<div class="tcrb-summary-box">
<strong><?= number_format((float) $summary['min_mag'], 3, '.', '') ?> bis <?= number_format((float) $summary['max_mag'], 3, '.', '') ?></strong>
<span>Magnitudenbereich</span>
</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 class="tcrb-chart-wrap">
+2 -1
View File
@@ -14,6 +14,7 @@ require_once __DIR__ . '/../auth_helpers.php';
$config = require __DIR__ . '/../../config/database.php';
$telegramConfig = require __DIR__ . '/../../config/telegram.php';
require_once __DIR__ . '/../../config/telegram_message_footer.php';
$dsn = sprintf(
'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',
[
'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';
$telegramConfig = require __DIR__ . '/../../config/telegram.php';
require_once __DIR__ . '/../../config/telegram_message_footer.php';
$dsn = sprintf(
'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',
[
'chat_id' => $chatId,
'text' => $messageText,
'text' => appendTelegramServiceFooter($messageText),
]
);
}
+40 -13
View File
@@ -22,6 +22,7 @@ $rootDir = dirname(__DIR__, 2);
$stateFile = __DIR__ . DIRECTORY_SEPARATOR . 'telegram_cron_state.json';
$dbConfig = require $rootDir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database.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(
'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
{
$observedUtc = trim((string) ($observation['observed_at_utc'] ?? ''));
$observedText = $observedUtc !== '' ? $observedUtc . ' UTC' : 'unbekannt';
$observedText = formatGermanDateTime($observedUtc);
$magnitude = formatDecimal($observation['magnitude'] ?? null, 3);
$magnitudeError = $observation['magnitude_error'] !== null
? ' +/- ' . formatDecimal($observation['magnitude_error'], 3)
? ' ± ' . formatDecimal($observation['magnitude_error'], 3)
: '';
$jd = formatDecimal($observation['jd'] ?? null, 6);
$observerCode = trim((string) ($observation['observer_code'] ?? ''));
$nextPossibleEruptionDate = calculateNextPossibleTcrbEruptionDate($nowBerlin);
$lines = [
'SkyView T CrB Update',
'SkyViews TCrBotti mit dem Update',
'',
'Magnitude: ' . $magnitude . $magnitudeError,
'Zeitpunkt: ' . $observedText,
'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);
}
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
{
$width = 1200;
@@ -1051,7 +1078,7 @@ function telegramSendBotMessage(array $telegramConfig, string $chatId, string $m
'https://api.telegram.org/bot' . $botToken . '/sendMessage',
[
'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',
[
'chat_id' => $chatId,
'caption' => $caption,
'caption' => appendTelegramServiceFooter($caption),
],
'document',
$filePath,
@@ -1090,7 +1117,7 @@ function telegramSendBotPhoto(array $telegramConfig, string $chatId, string $fil
'https://api.telegram.org/bot' . $botToken . '/sendPhoto',
[
'chat_id' => $chatId,
'caption' => $caption,
'caption' => appendTelegramServiceFooter($caption),
],
'photo',
$filePath,