feat: add Telegram contact form API (PHP) and connect form endpoint

This commit is contained in:
2026-03-14 19:41:25 +03:00
parent 851dd6173b
commit 9a3785a13c
2 changed files with 65 additions and 1 deletions

64
public/api/contact.php Normal file
View File

@@ -0,0 +1,64 @@
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: https://sag24.ru');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit;
}
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
echo json_encode(['error' => 'Invalid JSON']);
exit;
}
$name = htmlspecialchars(trim($data['name'] ?? ''), ENT_QUOTES);
$phone = htmlspecialchars(trim($data['phone'] ?? ''), ENT_QUOTES);
$email = htmlspecialchars(trim($data['email'] ?? ''), ENT_QUOTES);
$message = htmlspecialchars(trim($data['message'] ?? ''), ENT_QUOTES);
if (!$name || !$message) {
http_response_code(400);
echo json_encode(['error' => 'Missing required fields']);
exit;
}
$BOT_TOKEN = '8138813013:AAElH2L5NspRLSdiFjDz6Qf32n4G24P_cj8';
$CHAT_ID = '-5230603582';
$text = "🔔 <b>Заявка с sag24.ru</b>\n\n";
$text .= "👤 <b>Имя:</b> {$name}\n";
if ($phone) $text .= "📱 <b>Телефон:</b> {$phone}\n";
if ($email) $text .= "📧 <b>Email:</b> {$email}\n";
$text .= "\n💬 <b>Сообщение:</b>\n{$message}\n";
$text .= "\n" . date('d.m.Y H:i', time() + 3 * 3600) . " MSK";
$payload = json_encode([
'chat_id' => $CHAT_ID,
'text' => $text,
'parse_mode' => 'HTML',
]);
$ch = curl_init("https://api.telegram.org/bot{$BOT_TOKEN}/sendMessage");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
]);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
echo json_encode(['success' => true]);
} else {
http_response_code(500);
echo json_encode(['error' => 'Failed to send notification']);
}

View File

@@ -1,5 +1,5 @@
// Настройки для связи и формы // Настройки для связи и формы
export const WHATSAPP_PHONE = '74953637476' // без + export const WHATSAPP_PHONE = '74953637476' // без +
export const TELEGRAM_USERNAME = 'sag24ru' // @username без @ export const TELEGRAM_USERNAME = 'sag24ru' // @username без @
export const FORM_ENDPOINT = '' // https://formspree.io/f/XXXXXXX — оставить пустым, будет mailto: export const FORM_ENDPOINT = '/api/contact.php'
export const EMAIL = 'info@sag24.ru' export const EMAIL = 'info@sag24.ru'