feat: added ability to send form data to tg
This commit is contained in:
@@ -22,15 +22,31 @@ callbackButtons.forEach((button) => {
|
||||
const modalForms = document.querySelectorAll('.modal__form');
|
||||
const callbackForms = document.querySelectorAll('.callback__form');
|
||||
|
||||
const BOT_TOKEN = '';
|
||||
const CHAT_ID = '';
|
||||
|
||||
[...modalForms, ...callbackForms].forEach((form) => {
|
||||
form.addEventListener('submit', (event) => {
|
||||
event.preventDefault();
|
||||
const formData = new FormData(event.currentTarget);
|
||||
const name = formData.get('name');
|
||||
const phone = formData.get('phone');
|
||||
console.log(name, phone)
|
||||
const message = `Имя: ${name} Телефон ${phone}`;
|
||||
sendMessage(message);
|
||||
event.currentTarget.reset();
|
||||
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}`;
|
||||
|
||||
fetch(url)
|
||||
.then((data) => {
|
||||
console.log('Message sent successfully:', data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error sending message:', error);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user