Userverbindung Telegram ist drin
This commit is contained in:
@@ -32,6 +32,66 @@ function telegramWebhookRespond(array $payload, int $statusCode = 200): never
|
||||
exit;
|
||||
}
|
||||
|
||||
function telegramHttpPostJson(string $url, array $payload): bool
|
||||
{
|
||||
$jsonPayload = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
if ($jsonPayload === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (function_exists('curl_init')) {
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($jsonPayload),
|
||||
],
|
||||
CURLOPT_POSTFIELDS => $jsonPayload,
|
||||
CURLOPT_TIMEOUT => 15,
|
||||
]);
|
||||
|
||||
$body = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
return $body !== false;
|
||||
}
|
||||
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => implode("\r\n", [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($jsonPayload),
|
||||
]),
|
||||
'content' => $jsonPayload,
|
||||
'timeout' => 15,
|
||||
'ignore_errors' => true,
|
||||
],
|
||||
]);
|
||||
|
||||
$body = @file_get_contents($url, false, $context);
|
||||
|
||||
return $body !== false;
|
||||
}
|
||||
|
||||
function telegramSendBotMessage(array $telegramConfig, string $chatId, string $messageText): void
|
||||
{
|
||||
$botToken = trim((string) ($telegramConfig['bot_token'] ?? ''));
|
||||
if ($botToken === '' || $chatId === '' || $messageText === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
telegramHttpPostJson(
|
||||
'https://api.telegram.org/bot' . $botToken . '/sendMessage',
|
||||
[
|
||||
'chat_id' => $chatId,
|
||||
'text' => $messageText,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$expectedSecret = trim((string) ($telegramConfig['webhook_secret'] ?? ''));
|
||||
if ($expectedSecret !== '') {
|
||||
$providedSecret = (string) ($_SERVER['HTTP_X_TELEGRAM_BOT_API_SECRET_TOKEN'] ?? '');
|
||||
@@ -102,6 +162,18 @@ $stmtUpdate->execute([
|
||||
':id' => (int) $user['id'],
|
||||
]);
|
||||
|
||||
$welcomeName = $telegramUsername !== '' ? '@' . ltrim($telegramUsername, '@') : 'bei Telegram';
|
||||
$welcomeMessage = implode("\n", [
|
||||
'Willkommen bei SkyView.',
|
||||
'',
|
||||
'Dein Telegram-Konto wurde erfolgreich mit deinem SkyView-Benutzer verbunden.',
|
||||
'Du kannst jetzt spaeter Hinweise und Inhalte direkt vom Bot erhalten.',
|
||||
'',
|
||||
'Verbunden als: ' . $welcomeName,
|
||||
]);
|
||||
|
||||
telegramSendBotMessage($telegramConfig, $chatId, $welcomeMessage);
|
||||
|
||||
telegramWebhookRespond([
|
||||
'ok' => true,
|
||||
'status' => 'connected',
|
||||
|
||||
Reference in New Issue
Block a user