feat: send weather to group + personal (+14085505485) simultaneously
This commit is contained in:
parent
16ca326820
commit
08a18e825c
|
|
@ -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,32 +119,43 @@ 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
|
||||||
const gwUrl = new URL(GATEWAY);
|
function sendTo(to, label) {
|
||||||
const opts = {
|
return new Promise((resolve) => {
|
||||||
hostname: gwUrl.hostname,
|
const body = JSON.stringify({ to, message: msg });
|
||||||
port: parseInt(gwUrl.port) || 80,
|
const gwUrl = new URL(GATEWAY);
|
||||||
path: gwUrl.pathname,
|
const opts = {
|
||||||
method: 'POST',
|
hostname: gwUrl.hostname,
|
||||||
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) }
|
port: parseInt(gwUrl.port) || 80,
|
||||||
};
|
path: gwUrl.pathname,
|
||||||
|
method: 'POST',
|
||||||
const req = http.request(opts, (r) => {
|
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) }
|
||||||
let resp = '';
|
};
|
||||||
r.on('data', d => resp += d);
|
const req = http.request(opts, (r) => {
|
||||||
r.on('end', () => {
|
let resp = '';
|
||||||
const parsed = JSON.parse(resp);
|
r.on('data', d => resp += d);
|
||||||
if (parsed.success) {
|
r.on('end', () => {
|
||||||
console.log(`[WeatherBot] ✅ Weather report sent to group. MsgID: ${parsed.messageId}`);
|
try {
|
||||||
} else {
|
const parsed = JSON.parse(resp);
|
||||||
console.error(`[WeatherBot] ❌ Gateway error: ${parsed.error}`);
|
if (parsed.success) {
|
||||||
process.exit(1);
|
console.log(`[WeatherBot] ✅ Sent to ${label}. MsgID: ${parsed.messageId}`);
|
||||||
}
|
} else {
|
||||||
|
console.error(`[WeatherBot] ❌ ${label} error: ${parsed.error}`);
|
||||||
|
}
|
||||||
|
} catch(e) { console.error(`[WeatherBot] ${label} parse error:`, e.message); }
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
req.on('error', e => { console.error(`[WeatherBot] ${label} request failed:`, e.message); resolve(); });
|
||||||
|
req.write(body);
|
||||||
|
req.end();
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
req.on('error', e => { console.error('[WeatherBot] Request failed:', e.message); process.exit(1); });
|
|
||||||
req.write(body);
|
// Send to group then personal number
|
||||||
req.end();
|
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);
|
||||||
|
|
|
||||||
|
|
@ -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 });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue