feat: added php script
This commit is contained in:
@@ -31,22 +31,26 @@ const CHAT_ID = '';
|
||||
const formData = new FormData(event.currentTarget);
|
||||
const name = formData.get('name');
|
||||
const phone = formData.get('phone');
|
||||
const message = `Имя: ${name} Телефон ${phone}`;
|
||||
sendMessage(message);
|
||||
// const message = `Имя: ${name} Телефон ${phone}`;
|
||||
// sendMessage(message);
|
||||
event.currentTarget.reset();
|
||||
fetch('../../send.php', {
|
||||
method: 'POST',
|
||||
data: { name, phone },
|
||||
});
|
||||
if (callbackModal) callbackModal.classList.remove('active');
|
||||
if (thanksModal) thanksModal.classList.add('active');
|
||||
});
|
||||
});
|
||||
|
||||
function sendMessage(message) {
|
||||
const url = `https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${CHAT_ID}&parse_mode=html&text=${message}`;
|
||||
// function sendMessage(message) {
|
||||
// const url = `https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${CHAT_ID}&parse_mode=html&text=${message}`;
|
||||
|
||||
fetch(url)
|
||||
.then((data) => {
|
||||
console.log('Message sent successfully:', data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error sending message:', error);
|
||||
});
|
||||
}
|
||||
// fetch(url)
|
||||
// .then((data) => {
|
||||
// console.log('Message sent successfully:', data);
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error('Error sending message:', error);
|
||||
// });
|
||||
// }
|
||||
|
||||
36
send.php
Normal file
36
send.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
$token = "";
|
||||
$chat_id = "";
|
||||
|
||||
//Определяем переменные для передачи данных из нашей формы
|
||||
if ($_POST['act'] == 'order') {
|
||||
$name = ($_POST['name']);
|
||||
$phone = ($_POST['phone']);
|
||||
|
||||
//Собираем в массив то, что будет передаваться боту
|
||||
$arr = array(
|
||||
'Имя:' => $name,
|
||||
'Телефон:' => $phone
|
||||
);
|
||||
|
||||
//Настраиваем внешний вид сообщения в телеграме
|
||||
foreach($arr as $key => $value) {
|
||||
$txt .= "<b>".$key."</b> ".$value."%0A";
|
||||
};
|
||||
|
||||
//Передаем данные боту
|
||||
$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");
|
||||
|
||||
//Выводим сообщение об успешной отправке
|
||||
if ($sendToTelegram) {
|
||||
alert('Спасибо! Ваша заявка принята. Мы свяжемся с вами в ближайшее время.');
|
||||
}
|
||||
|
||||
//А здесь сообщение об ошибке при отправке
|
||||
else {
|
||||
alert('Что-то пошло не так. ПОпробуйте отправить форму ещё раз.');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user