feat: send weather to group + personal (+14085505485) simultaneously

This commit is contained in:
Antigravity 2026-05-29 00:11:43 -04:00
parent 16ca326820
commit 08a18e825c
2 changed files with 69 additions and 36 deletions

View File

@ -6,7 +6,6 @@ metadata:
labels: labels:
app: weather-report app: weather-report
spec: spec:
# 7:00 AM ET = 11:00 UTC (EDT offset UTC-4)
schedule: "0 11 * * *" schedule: "0 11 * * *"
timeZone: "America/New_York" # K8s 1.27+ supports timeZone natively timeZone: "America/New_York" # K8s 1.27+ supports timeZone natively
concurrencyPolicy: Forbid concurrencyPolicy: Forbid
@ -30,6 +29,7 @@ spec:
const ZIP = '16046'; const ZIP = '16046';
const GATEWAY = 'http://whatsapp-gateway.ai-agents.svc.cluster.local:5001/send-message'; const GATEWAY = 'http://whatsapp-gateway.ai-agents.svc.cluster.local:5001/send-message';
const GROUP = 'https://chat.whatsapp.com/0Txfg7iZBbvIuvTbTqqgAX'; const GROUP = 'https://chat.whatsapp.com/0Txfg7iZBbvIuvTbTqqgAX';
const PERSONAL = '+14085505485';
function weatherEmoji(desc) { function weatherEmoji(desc) {
const d = desc.toLowerCase(); const d = desc.toLowerCase();
@ -119,7 +119,10 @@ spec:
`_Powered by Agentic OS • hq.applaude.net_ 🏠`, `_Powered by Agentic OS • hq.applaude.net_ 🏠`,
].filter(l => l !== null && l !== undefined).join('\n'); ].filter(l => l !== null && l !== undefined).join('\n');
const body = JSON.stringify({ to: GROUP, message: msg }); // Helper: send msg to one recipient
function sendTo(to, label) {
return new Promise((resolve) => {
const body = JSON.stringify({ to, message: msg });
const gwUrl = new URL(GATEWAY); const gwUrl = new URL(GATEWAY);
const opts = { const opts = {
hostname: gwUrl.hostname, hostname: gwUrl.hostname,
@ -128,23 +131,31 @@ spec:
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) } headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) }
}; };
const req = http.request(opts, (r) => { const req = http.request(opts, (r) => {
let resp = ''; let resp = '';
r.on('data', d => resp += d); r.on('data', d => resp += d);
r.on('end', () => { r.on('end', () => {
try {
const parsed = JSON.parse(resp); const parsed = JSON.parse(resp);
if (parsed.success) { if (parsed.success) {
console.log(`[WeatherBot] ✅ Weather report sent to group. MsgID: ${parsed.messageId}`); console.log(`[WeatherBot] ✅ Sent to ${label}. MsgID: ${parsed.messageId}`);
} else { } else {
console.error(`[WeatherBot] ❌ Gateway error: ${parsed.error}`); console.error(`[WeatherBot] ❌ ${label} error: ${parsed.error}`);
process.exit(1);
} }
} catch(e) { console.error(`[WeatherBot] ${label} parse error:`, e.message); }
resolve();
}); });
}); });
req.on('error', e => { console.error('[WeatherBot] Request failed:', e.message); process.exit(1); }); req.on('error', e => { console.error(`[WeatherBot] ${label} request failed:`, e.message); resolve(); });
req.write(body); req.write(body);
req.end(); req.end();
});
}
// Send to group then personal number
sendTo(GROUP, 'WhatsApp Group')
.then(() => sendTo(PERSONAL, 'Personal (+14085505485)'))
.catch(e => { console.error('[WeatherBot] Fatal:', e.message); process.exit(1); });
} catch (e) { } catch (e) {
console.error('[WeatherBot] Parse error:', e.message); console.error('[WeatherBot] Parse error:', e.message);

View File

@ -4,27 +4,49 @@ const GATEWAY = process.env.WHATSAPP_GATEWAY_URL ||
'http://whatsapp-gateway.ai-agents.svc.cluster.local:5001/send-message'; 'http://whatsapp-gateway.ai-agents.svc.cluster.local:5001/send-message';
const DEFAULT_GROUP = process.env.WHATSAPP_GROUP || const DEFAULT_GROUP = process.env.WHATSAPP_GROUP ||
'https://chat.whatsapp.com/0Txfg7iZBbvIuvTbTqqgAX'; 'https://chat.whatsapp.com/0Txfg7iZBbvIuvTbTqqgAX';
const PERSONAL_NUMBER = process.env.WHATSAPP_PERSONAL || '+14085505485';
async function sendToRecipient(to, message) {
const res = await fetch(GATEWAY, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ to, message }),
});
return res.json();
}
export async function POST(request) { export async function POST(request) {
try { try {
const { message, to } = await request.json(); const { message, to, personalOnly } = await request.json();
if (!message) { if (!message) {
return NextResponse.json({ error: 'message is required' }, { status: 400 }); return NextResponse.json({ error: 'message is required' }, { status: 400 });
} }
const res = await fetch(GATEWAY, { // personalOnly=true → only me; default → group + me
method: 'POST', const targets = personalOnly
headers: { 'Content-Type': 'application/json' }, ? [{ to: PERSONAL_NUMBER, label: 'personal' }]
body: JSON.stringify({ to: to || DEFAULT_GROUP, message }), : [
}); { to: to || DEFAULT_GROUP, label: 'group' },
{ to: PERSONAL_NUMBER, label: 'personal' },
];
const data = await res.json(); const results = await Promise.allSettled(
targets.map(t => sendToRecipient(t.to, message))
);
if (!res.ok || !data.success) { const successful = results.filter(r => r.status === 'fulfilled' && r.value?.success);
return NextResponse.json({ error: data.error || 'Gateway error' }, { status: 502 }); const failed = results.filter(r => r.status !== 'fulfilled' || !r.value?.success);
if (successful.length === 0) {
return NextResponse.json({ error: 'All recipients failed', details: failed }, { status: 502 });
} }
return NextResponse.json({ success: true, messageId: data.messageId, recipient: data.recipient }); return NextResponse.json({
success: true,
sent: successful.length,
failed: failed.length,
messageId: successful[0]?.value?.messageId,
});
} catch (err) { } catch (err) {
return NextResponse.json({ error: err.message }, { status: 500 }); return NextResponse.json({ error: err.message }, { status: 500 });
} }