feat: add Telegram contact form API (PHP) and connect form endpoint
This commit is contained in:
64
public/api/contact.php
Normal file
64
public/api/contact.php
Normal 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']);
|
||||
}
|
||||
Reference in New Issue
Block a user