New Message
-
+
+
Enter multiple phone numbers separated by commas
@@ -495,27 +505,88 @@
}
async function sendCompose() {
- const to = document.getElementById('compose-to').value.trim();
+ const toInput = document.getElementById('compose-to').value.trim();
const text = document.getElementById('compose-msg').value.trim();
- if (!to || !text) { document.getElementById('compose-error').textContent = 'Fill all fields'; return; }
+ const error = document.getElementById('compose-error');
+
+ error.textContent = '';
+
+ if (!toInput) {
+ error.textContent = 'Please enter at least one phone number';
+ return;
+ }
+
+ if (!text) {
+ error.textContent = 'Please enter a message';
+ return;
+ }
+
+ // Split numbers by comma
+ const recipients = toInput
+ .split(',')
+ .map(number => number.trim())
+ .filter(number => number.length > 0);
+
+ // Remove duplicate numbers
+ const uniqueRecipients = [...new Set(recipients)];
+
+ if (uniqueRecipients.length === 0) {
+ error.textContent = 'Please enter at least one phone number';
+ return;
+ }
+
+ // Basic phone number validation
+ const invalidNumber = uniqueRecipients.find(number => {
+ return !/^\+?[0-9\s\-()]+$/.test(number);
+ });
+
+ if (invalidNumber) {
+ error.textContent = `Invalid phone number: ${invalidNumber}`;
+ return;
+ }
+
+ const sendButton = document.querySelector(
+ '#compose-modal .modal-actions button:last-child'
+ );
+
+ sendButton.disabled = true;
+ sendButton.textContent = 'Sending...';
try {
- const r = await fetch('/api/sms/send', {
+ const response = await fetch('/api/sms/send', {
method: 'POST',
- headers: { 'Content-Type': 'application/json', 'X-API-Key': apiKey },
- body: JSON.stringify({ to, text })
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-API-Key': apiKey
+ },
+ body: JSON.stringify({
+ to: uniqueRecipients,
+ text: text
+ })
});
- if (r.ok) {
- closeCompose();
- await loadMessages();
- openThread(to);
- } else {
- const e = await r.json();
- document.getElementById('compose-error').textContent = e.error || 'Failed';
- }
- } catch (e) { document.getElementById('compose-error').textContent = 'Error'; }
- }
+ const data = await response.json();
+
+ if (!response.ok) {
+ throw new Error(data.error || 'Failed to send SMS');
+ }
+
+ closeCompose();
+
+ await loadMessages();
+
+ // Open the conversation when sending to one number
+ if (uniqueRecipients.length === 1) {
+ openThread(uniqueRecipients[0]);
+ }
+
+ } catch (err) {
+ error.textContent = err.message || 'Failed to send SMS';
+ } finally {
+ sendButton.disabled = false;
+ sendButton.textContent = 'Send';
+ }
+ }
function showAbout() { document.getElementById('about-modal').style.display = 'flex'; }
function closeAbout() { document.getElementById('about-modal').style.display = 'none'; }
@@ -531,4 +602,4 @@
setInterval(() => { if (apiKey) { loadMessages(); } }, 15000);