const https = require('https'); const config = require('./config'); async function sendSMS(text, recipientOverride) { const gw = config.smsGateway; const recipients = recipientOverride ? [recipientOverride] : [gw.recipientNumber]; const data = JSON.stringify({ recipients, message: text, }); return new Promise((resolve, reject) => { const req = https.request( { hostname: 'api.textbee.dev', path: `/api/v1/gateway/devices/${gw.deviceId}/send-sms`, method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': gw.apiKey, 'Content-Length': Buffer.byteLength(data), }, }, (res) => { let body = ''; res.on('data', (chunk) => (body += chunk)); res.on('end', () => { if (res.statusCode >= 200 && res.statusCode < 300) { resolve(body); } else { reject(new Error(`TextBee ${res.statusCode}: ${body}`)); } }); } ); req.on('error', reject); req.write(data); req.end(); }); } module.exports = { sendSMS };