This code automates email command processing by retrieving emails, extracting commands, processing them, generating responses, and sending them back to the sender. It utilizes a custom importer
module for various functions like email searching, sending, command filtering, and result storage.
npm run import -- "scan commands email"
var importer = require('../Core');
var {
searchImap,
sendEmail,
getOauthClient,
filterCommand,
storeResult
} = importer.import("search imap messages",
"send email",
"filter command permission",
"store rpc result");
var options = {
calendarId: 'Commands'
};
function scanCommandsEmail() {
return searchImap('*', 'megamind', 1, 'Megamind')
.then(messages => messages.flatten())
// get settings for each
.then(friends => {
const hasCommands = friends.map(f => Object.assign(f, {
id: 'Email: ' + id,
command: (/megamind\s*(.*)/ig).exec(f.subject)[1].trim()
})).filter(f => f.command.length > 0
// filter out responses from my own INBOX so I can test this service myself
&& !f.body.match(/^\s* Mm\s*$/gm));
return importer.runAllPromises(hasCommands.map(f => resolve => {
return filterCommand(f.command, f.date, f.id, f.email)
.then(props => resolve(Object.assign(f, props)));
}))
})
// generate and send responses
.then(friends => {
return importer.runAllPromises(friends.map(f => resolve => {
return storeResult(f)
.then(response => sendEmail(
f.email,
JSON.stringify(response, null, 4) + '\n Mm\n',
f.subject))
.catch(e => console.log(e))
.then(r => resolve(r))
}))
})
.catch(e => console.log(e))
}
module.exports = scanCommandsEmail;
if(typeof $ !== 'undefined') {
$.async();
scanCommandsEmail()
.then(r => $.sendResult(r))
.catch((e) => $.sendError(e))
}
const { searchImap, sendEmail, getOauthClient, filterCommand, storeResult } = require('../Core');
const scanCommandsEmail = async () => {
try {
const messages = await searchImap('*','megamind', 1, 'Megamind');
const flattenedMessages = messages.flatten();
const friends = await getFriends(flattenedMessages);
const hasCommands = await filterFriends(friends);
const responses = await getResponses(hasCommands);
await sendResponses(responses);
} catch (error) {
console.error(error);
}
};
const getFriends = async (messages) => {
// get settings for each
const hasCommands = messages.map((message) => ({
id: `Email: ${message.id}`,
command: (new RegExp('megamind (.*?)')).exec(message.subject)[1].trim(),
date: message.date,
email: message.email,
}));
return hasCommands.filter((f) => f.command.length > 0 &&!f.body.match(/^\s* Mm\s*$/gm));
};
const filterFriends = async (friends) => {
const promises = friends.map(async (friend) => {
return filterCommand(friend.command, friend.date, friend.id, friend.email);
});
return Promise.all(promises);
};
const getResponses = async (friends) => {
const promises = friends.map(async (friend) => {
return storeResult(friend).then((response) => ({
...friend,
response,
}));
});
return Promise.all(promises);
};
const sendResponses = async (responses) => {
await Promise.all(responses.map(async (response) => {
await sendEmail(response.email, JSON.stringify(response.response, null, 4) + '\n Mm\n', response.subject);
return response;
}));
};
module.exports = scanCommandsEmail;
if (typeof $!== 'undefined') {
$.async();
scanCommandsEmail()
.then((r) => $.sendResult(r))
.catch((e) => $.sendError(e));
}
This code defines a function scanCommandsEmail
that processes incoming emails containing commands, filters them, retrieves relevant information, and sends back responses.
Here's a breakdown:
Imports: It imports several functions from a custom importer
module, including searchImap
for fetching emails, sendEmail
for sending responses, filterCommand
for processing commands, and storeResult
for storing results.
Email Scanning: The scanCommandsEmail
function uses searchImap
to retrieve emails from a specific mailbox (likely the sender's inbox) matching certain criteria (sender, subject, date range).
Command Extraction: It extracts commands from the email subjects and filters out irrelevant emails.
Command Processing: It uses filterCommand
to process each extracted command, likely retrieving additional information or performing actions based on the command.
Response Generation: It generates responses based on the processed commands and stores them using storeResult
.
Email Sending: It sends the generated responses back to the sender using sendEmail
.
Error Handling: The code includes error handling using .catch
blocks to log any errors that occur during the process.
Essentially, this code automates a process of receiving commands via email, processing them, and sending back automated responses.