Add Email forwarding and reply integration
This commit is contained in:
parent
c5e63543c6
commit
0d6450ced4
|
|
@ -185,6 +185,25 @@ async function sendWhatsAppNotification(email, summary) {
|
|||
}
|
||||
}
|
||||
|
||||
const ORCHESTRATOR_URL = process.env.ORCHESTRATOR_URL || 'http://agents-api.agents-runtime.svc.cluster.local:8000/webhook';
|
||||
|
||||
async function forwardToOrchestrator(email) {
|
||||
console.log(`Forwarding email from owner to orchestrator: ${email.subject}`);
|
||||
try {
|
||||
await fetch(ORCHESTRATOR_URL, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
text: `Subject: ${email.subject}\n\n${email.body}`,
|
||||
sender: email.from,
|
||||
targetJid: 'EMAIL'
|
||||
})
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Failed to forward to orchestrator:', err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function processEmails() {
|
||||
console.log(`[${new Date().toISOString()}] Checking for new emails across ${accounts.length} accounts...`);
|
||||
try {
|
||||
|
|
@ -203,11 +222,15 @@ async function processEmails() {
|
|||
console.log(`Found ${emails.length} unread emails for ${account.name}.`);
|
||||
|
||||
for (const email of emails) {
|
||||
const analysis = await analyzeWithGemini(email);
|
||||
if (analysis.isImportant) {
|
||||
await sendWhatsAppNotification(email, analysis.summary);
|
||||
if (email.from.toLowerCase().includes('deepkoluguri@gmail.com')) {
|
||||
await forwardToOrchestrator(email);
|
||||
} else {
|
||||
console.log(`Skipped non-important email: ${email.subject}`);
|
||||
const analysis = await analyzeWithGemini(email);
|
||||
if (analysis.isImportant) {
|
||||
await sendWhatsAppNotification(email, analysis.summary);
|
||||
} else {
|
||||
console.log(`Skipped non-important email: ${email.subject}`);
|
||||
}
|
||||
}
|
||||
// Mark as processed using our custom label
|
||||
await markAsAgentRead(email, account.agentReadLabelId);
|
||||
|
|
@ -225,6 +248,41 @@ const app = express();
|
|||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
// Send an email (called by orchestrator)
|
||||
app.post('/api/send-email', async (req, res) => {
|
||||
const { to, subject, body } = req.body;
|
||||
if (!to || !subject || !body) return res.status(400).json({ error: 'Missing to, subject, or body' });
|
||||
|
||||
// Use the first connected account to send the email
|
||||
if (accounts.length === 0) return res.status(500).json({ error: 'No Gmail accounts connected' });
|
||||
const account = accounts[0];
|
||||
|
||||
try {
|
||||
const gmail = google.gmail({ version: 'v1', auth: account.client });
|
||||
const rawMessage = [
|
||||
`To: ${to}`,
|
||||
`Subject: ${subject}`,
|
||||
'Content-Type: text/plain; charset="UTF-8"',
|
||||
'',
|
||||
body
|
||||
].join('\n');
|
||||
|
||||
const encodedMessage = Buffer.from(rawMessage).toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
||||
|
||||
await gmail.users.messages.send({
|
||||
userId: 'me',
|
||||
requestBody: {
|
||||
raw: encodedMessage
|
||||
}
|
||||
});
|
||||
console.log(`Sent email to ${to} with subject: ${subject}`);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
console.error('Error sending email:', err.message);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Get all connected accounts
|
||||
app.get('/api/accounts', (req, res) => {
|
||||
res.json({
|
||||
|
|
|
|||
Loading…
Reference in New Issue