const BOT_TOKEN = "8478402212:AAEIEjKhLVfp_kPAn6t5U3AbTxJ6Y95wHpc";
const CHAT_ID = "-1003127461389";
const photoInput = document.getElementById('photo');
const preview = document.getElementById('preview');
// Escape user text for Telegram MarkdownV2
function escapeMarkdownV2(text) {
if (!text) return '';
return text.replace(/([_*\[\]()~`>#+\-=|{}\.!])/g, '\\$1');
}
// Image preview
photoInput.addEventListener('change', () => {
const file = photoInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = e => {
preview.src = e.target.result;
preview.style.display = 'block';
};
reader.readAsDataURL(file);
} else {
preview.style.display = 'none';
}
});
// Load and display submissions from localStorage
function loadSubmissions() {
const submissions = JSON.parse(localStorage.getItem('submissions') || '[]');
const list = document.getElementById('submissionList');
list.innerHTML = '';
submissions.forEach(sub => {
const li = document.createElement('li');
li.innerHTML = `Anonymous :
${sub.datetime}
${sub.status}`;
if (sub.image) {
const img = document.createElement('img');
img.src = sub.image;
li.appendChild(img);
}
list.appendChild(li);
});
}
function saveSubmission(status, datetime, image=null) {
const submissions = JSON.parse(localStorage.getItem('submissions') || '[]');
submissions.unshift({status, datetime, image});
localStorage.setItem('submissions', JSON.stringify(submissions));
}
loadSubmissions();
// Form submission (plain-text, no parse_mode)
document.getElementById('telegramForm').addEventListener('submit', async (e) => {
e.preventDefault();
const statusRaw = document.getElementById('status').value || '';
const photo = photoInput.files[0];
const now = new Date();
const datetimeRaw = now.toLocaleString('en-GB', { hour12: false });
// Keep submission storage readable (use raw text)
try {
let response;
if (photo) {
const formData = new FormData();
formData.append('chat_id', CHAT_ID);
// Send plain caption (no parse_mode) to avoid entity parsing issues
formData.append('caption', `Status: ${statusRaw}\nDate & Time: ${datetimeRaw}`);
formData.append('document', photo);
response = await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendDocument`, {
method: 'POST',
body: formData
});
} else {
const text = `Status:\n${statusRaw}\n\n=========================\nDate & Time: ${datetimeRaw}`;
response = await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: CHAT_ID,
text: text
// parse_mode removed on purpose
})
});
}
// Show full response for debugging
let dataText;
try {
const data = await response.json();
console.log('Telegram response JSON:', data);
dataText = data;
if (data.ok) {
// Save to localStorage — for images we still want the actual preview data
if (photo) {
const reader = new FileReader();
reader.onload = e => {
saveSubmission(statusRaw, datetimeRaw, e.target.result);
loadSubmissions();
};
reader.readAsDataURL(photo);
} else {
saveSubmission(statusRaw, datetimeRaw);
loadSubmissions();
}
// Reset form & preview
document.getElementById('telegramForm').reset();
preview.src = '';
preview.style.display = 'none';
// Show Thank You popup
const popup = document.getElementById('thankYouPopup');
popup.style.display = 'flex';
document.getElementById('closePopup').onclick = () => {
popup.style.display = 'none';
};
} else {
// Show helpful error from Telegram
alert('❌ Failed to send: ' + (data.description || JSON.stringify(data)));
}
} catch (err) {
// If response is not JSON, show the raw text
const raw = await response.text();
console.error('Telegram non-JSON response:', raw);
alert('❌ Failed to send (non-JSON response). See console for details.');
}
} catch (err) {
console.error(err);
alert('⚠️ Error sending message. See console for details.');
}
});
-->