(function() {
var scriptTag = document.currentScript || document.querySelector('script[data-token]');
var publicToken = scriptTag ? scriptTag.getAttribute('data-token') : "";
if (!publicToken) return console.error("NEURO CORE: Токен доступа (data-token) не найден в скрипте Tilda!");

var gateUrl = "https://ai-balloon.ru/ai-gate.php";
var attachedImageBase64 = "";

var style = document.createElement('style');
style.innerHTML =
'.nb-widget-container { position: fixed; bottom: 25px; right: 25px; z-index: 999999; font-family: Arial, sans-serif; display: flex; flex-direction: column; align-items: flex-end; }' +
'.nb-chat-button { width: 60px; height: 60px; border-radius: 50%; background: #ff69b4; color: white; display: flex; align-items: center; justify-content: center; font-size: 28px; cursor: pointer; box-shadow: 0 4px 15px rgba(255,105,180,0.4); transition: transform 0.2s; }' +
'.nb-chat-button:hover { transform: scale(1.05); }' +
'.nb-chat-window { width: 360px; height: 500px; background: white; border-radius: 16px; box-shadow: 0 5px 25px rgba(0,0,0,0.15); border: 1px solid #ffd1dc; display: none; flex-direction: column; overflow: hidden; margin-bottom: 15px; }' +
'.nb-chat-window.active { display: flex; }' +
'.nb-header { background: #ff69b4; padding: 15px; color: white; display: flex; align-items: center; gap: 10px; font-weight: bold; font-size: 15px; }' +
'.nb-avatar { width: 30px; height: 30px; border-radius: 50%; background: white; display: flex; align-items: center; justify-content: center; font-size: 18px; }' +
'.nb-messages { flex: 1; padding: 15px; overflow-y: auto; background: #fff5f8; display: flex; flex-direction: column; gap: 10px; }' +
'.nb-msg { max-width: 80%; padding: 10px 14px; border-radius: 12px; font-size: 13.5px; line-height: 1.4; word-wrap: break-word; }' +
'.nb-msg.user { background: white; color: #333; align-self: flex-start; border: 1px solid #ffd1dc; }' +
'.nb-msg.bot { background: #ffd1dc; color: #333; align-self: flex-end; }' +
'.nb-footer { padding: 12px; background: white; border-top: 1px solid #ffe5ec; display: flex; gap: 8px; align-items: center; position: relative; }' +
'.nb-input { flex: 1; padding: 10px 10px 10px 40px; border: 1px solid #ddd; border-radius: 20px; font-size: 13px; outline: none; box-sizing: border-box; }' +
'.nb-input:focus { border-color: #ff69b4; }' +
'.nb-send { background: #ff69b4; color: white; border: none; padding: 10px 18px; border-radius: 15px; cursor: pointer; font-size: 13px; font-weight: bold; flex-shrink: 0; }' +
'.nb-clip-btn { position: absolute; left: 22px; bottom: 20px; font-size: 18px; color: #aaa; cursor: pointer; transition: color 0.2s; z-index: 5; }' +
'.nb-clip-btn:hover { color: #ff69b4; }' +
'.nb-preview-box { display: none; position: absolute; bottom: 65px; left: 15px; background: white; border: 1px solid #ffd1dc; padding: 5px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); align-items: center; gap: 5px; z-index: 10; }' +
'.nb-preview-img { width: 45px; height: 45px; object-fit: cover; border-radius: 4px; }' +
'.nb-preview-del { font-size: 14px; color: red; cursor: pointer; font-weight: bold; padding: 0 4px; }' +
'@media (max-width: 480px) { .nb-chat-window { width: 300px; height: 420px; } }';
document.head.appendChild(style);

var container = document.createElement('div');
container.className = "nb-widget-container";
container.innerHTML =
'<div class="nb-chat-window" id="nbWin">' +
'<div class="nb-header"><div class="nb-avatar">🎈</div> <span>Катя | Аэродизайнер</span></div>' +
'<div class="nb-messages" id="nbMsgs"></div>' +
'<div class="nb-preview-box" id="nbPreviewBox">' +
'<img class="nb-preview-img" id="nbPreviewImg" src="">' +
'<div class="nb-preview-del" id="nbPreviewDel">✕</div>' +
'</div>' +
'<div class="nb-footer">' +
'<input type="file" id="nbFileInput" accept="image/*" style="display:none;">' +
'<div class="nb-clip-btn" id="nbClipBtn">📎</div>' +
'<input type="text" class="nb-input" id="nbInp" placeholder="Спросите Катю или вставьте фото (Ctrl+V)...">' +
'<button class="nb-send" id="nbSend">Отправить</button>' +
'</div>' +
'</div>' +
'<div class="nb-chat-button" id="nbBtn">💬</div>';
document.body.appendChild(container);

var nbBtn = document.getElementById('nbBtn');
var nbWin = document.getElementById('nbWin');
var nbMsgs = document.getElementById('nbMsgs');
var nbInp = document.getElementById('nbInp');
var nbSend = document.getElementById('nbSend');
var nbClipBtn = document.getElementById('nbClipBtn');
var nbFileInput = document.getElementById('nbFileInput');
var nbPreviewBox = document.getElementById('nbPreviewBox');
var nbPreviewImg = document.getElementById('nbPreviewImg');
var nbPreviewDel = document.getElementById('nbPreviewDel');

var chatId = localStorage.getItem('nb_chat_id');
if (!chatId) {
chatId = 'chat_' + Math.random().toString(36).substring(2, 11);
localStorage.setItem('nb_chat_id', chatId);
}

nbBtn.addEventListener('click', function() {
nbWin.classList.toggle('active');
if (nbWin.classList.contains('active') && nbMsgs.children.length === 0) {
loadInitialBotSettings();
}
});

nbClipBtn.addEventListener('click', function() {
nbFileInput.click();
});

nbFileInput.addEventListener('change', function(e) {
if (e.target.files && e.target.files[0]) processAndPreviewFile(e.target.files[0]);
});

nbInp.addEventListener('paste', function(e) {
var items = (e.clipboardData || e.originalEvent.clipboardData).items;
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") !== -1) {
var file = items[i].getAsFile();
processAndPreviewFile(file);
e.preventDefault();
}
}
});

function processAndPreviewFile(file) {
var reader = new FileReader();
reader.onload = function(event) {
attachedImageBase64 = event.target.result;
nbPreviewImg.src = event.target.result;
nbPreviewBox.style.display = "flex";
};
reader.readAsDataURL(file);
}

nbPreviewDel.addEventListener('click', function() {
attachedImageBase64 = "";
nbPreviewBox.style.display = "none";
nbFileInput.value = "";
});

function loadInitialBotSettings() {
nbMsgs.innerHTML = '<div style="color:#aaa; font-style:italic; font-size:12px; text-align:center; margin-top:50px;">Катя надувает шары и подключается к чату...</div>';

// Меняем метод на GET и прокидываем токен прямо в строке запроса!
fetch(gateUrl + "?public_token=" + publicToken, {
method: "GET"
})
.then(function(res) { return res.json(); })
.then(function(data) {
nbMsgs.innerHTML = "";
if(data.success) {
if(data.color) {
nbBtn.style.background = data.color;
nbBtn.style.boxShadow = "0 4px 15px " + data.color + "66";
document.querySelector('.nb-header').style.background = data.color;
nbSend.style.background = data.color;
}
var welcome = data.welcome_text || "Привет! ✨ Я Катя, ведущий аэродизайнер студии. Чем могу вам помочь? 🎈";
appendMessage(welcome, 'bot');
} else {
appendMessage("Привет! Рада вас видеть. Какая композиция из шаров вас интересует? ✨🎈", 'bot');
}
})
["catch"](function() {
nbMsgs.innerHTML = "";
appendMessage("Привет! Рада вас видеть. Какая композиция из шаров вас интересует? ✨🎈", 'bot');
});
}

function sendMessage() {
var text = nbInp.value.trim();
if (!text && !attachedImageBase64) return;

if (attachedImageBase64) {
var imgHtml = '<img src="' + attachedImageBase64 + '" style="max-width:100%; border-radius:8px; margin-bottom:5px; display:block;">';
appendMessage(imgHtml + text, 'user');
} else {
appendMessage(text, 'user');
}

nbInp.value = "";
var currentImgToSend = attachedImageBase64;

attachedImageBase64 = "";
nbPreviewBox.style.display = "none";
nbFileInput.value = "";

var loaderId = 'loading_' + Date.now();
var loaderDiv = document.createElement('div');
loaderDiv.className = "nb-msg bot";
loaderDiv.id = loaderId;
loaderDiv.style.fontStyle = "italic";
loaderDiv.style.color = "#aaa";
loaderDiv.innerText = currentImgToSend ? "Катя сканирует фото и считает смету поштучно..." : "Катя отвечает...";
nbMsgs.appendChild(loaderDiv);
nbMsgs.scrollTop = nbMsgs.scrollHeight;

fetch(gateUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
action: "chat",
public_token: publicToken,
chat_id: chatId,
message: text,
image_data: currentImgToSend
})
})
.then(function(res) { return res.json(); })
.then(function(data) {
var el = document.getElementById(loaderId);
if(el) el.remove();
if (data.reply) {
appendMessage(data.reply, 'bot');
} else {
appendMessage("Ой, у меня ленточка запуталась! Повторите запрос. 🎈", 'bot');
}
})
.catch(function() {
var el = document.getElementById(loaderId);
if(el) el.remove();
appendMessage("Ошибка соединения со шлюзом аэродизайна.", 'bot');
});
}

function appendMessage(text, sender) {
var msgDiv = document.createElement('div');
msgDiv.className = 'nb-msg ' + sender;

var formattedText = text.replace(/\[([\s\S]*?)\]\(([\s\S]*?)\)/g, function(match, label, href) {
return '<a href="' + href + '" target="_blank" style="color:#ff69b4; font-weight:bold; text-decoration:underline;">' + label + '</a>';
});

msgDiv.innerHTML = formattedText.replace(/\n/g, '<br>');
nbMsgs.appendChild(msgDiv);
nbMsgs.scrollTop = nbMsgs.scrollHeight;
}

nbSend.addEventListener('click', sendMessage);
nbInp.addEventListener('keypress', function(e) {
if (e.key === 'Enter') sendMessage();
});
})();



Made on
Tilda