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
+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,