Initial commit
This commit is contained in:
154
skills/google-workspace/gmail/list_messages.js
Normal file
154
skills/google-workspace/gmail/list_messages.js
Normal file
@@ -0,0 +1,154 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* List Gmail Messages
|
||||
*
|
||||
* Usage: node list_messages.js <account> [options]
|
||||
*
|
||||
* Options:
|
||||
* --query Gmail search query (default: empty = all)
|
||||
* --label Filter by label (e.g., INBOX, UNREAD, SENT)
|
||||
* --max Maximum messages to return (default: 10)
|
||||
* --unread Show only unread messages
|
||||
*
|
||||
* Examples:
|
||||
* node list_messages.js psd
|
||||
* node list_messages.js psd --unread --max 5
|
||||
* node list_messages.js personal --query "from:amazon.com"
|
||||
* node list_messages.js consulting --label SENT
|
||||
*/
|
||||
|
||||
const { google } = require('googleapis');
|
||||
const path = require('path');
|
||||
const { getAuthClient } = require(path.join(__dirname, '..', 'auth', 'token_manager'));
|
||||
|
||||
async function listMessages(account, options = {}) {
|
||||
const auth = await getAuthClient(account);
|
||||
const gmail = google.gmail({ version: 'v1', auth });
|
||||
|
||||
// Build query
|
||||
let query = options.query || '';
|
||||
if (options.unread) {
|
||||
query = query ? `${query} is:unread` : 'is:unread';
|
||||
}
|
||||
|
||||
// List messages
|
||||
const listParams = {
|
||||
userId: 'me',
|
||||
maxResults: options.max || 10,
|
||||
};
|
||||
|
||||
if (query) {
|
||||
listParams.q = query;
|
||||
}
|
||||
|
||||
if (options.label) {
|
||||
listParams.labelIds = [options.label.toUpperCase()];
|
||||
}
|
||||
|
||||
const response = await gmail.users.messages.list(listParams);
|
||||
const messages = response.data.messages || [];
|
||||
|
||||
if (messages.length === 0) {
|
||||
return {
|
||||
success: true,
|
||||
account,
|
||||
messages: [],
|
||||
metadata: {
|
||||
timestamp: new Date().toISOString(),
|
||||
count: 0,
|
||||
query: query || '(all)',
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Get message details
|
||||
const messageDetails = await Promise.all(
|
||||
messages.map(async (msg) => {
|
||||
const detail = await gmail.users.messages.get({
|
||||
userId: 'me',
|
||||
id: msg.id,
|
||||
format: 'metadata',
|
||||
metadataHeaders: ['From', 'To', 'Subject', 'Date'],
|
||||
});
|
||||
|
||||
const headers = detail.data.payload.headers;
|
||||
const getHeader = (name) => {
|
||||
const header = headers.find(h => h.name.toLowerCase() === name.toLowerCase());
|
||||
return header ? header.value : '';
|
||||
};
|
||||
|
||||
return {
|
||||
id: msg.id,
|
||||
threadId: msg.threadId,
|
||||
from: getHeader('From'),
|
||||
to: getHeader('To'),
|
||||
subject: getHeader('Subject'),
|
||||
date: getHeader('Date'),
|
||||
snippet: detail.data.snippet,
|
||||
labels: detail.data.labelIds,
|
||||
isUnread: detail.data.labelIds.includes('UNREAD'),
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
account,
|
||||
messages: messageDetails,
|
||||
metadata: {
|
||||
timestamp: new Date().toISOString(),
|
||||
count: messageDetails.length,
|
||||
query: query || '(all)',
|
||||
nextPageToken: response.data.nextPageToken,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// CLI interface
|
||||
async function main() {
|
||||
const args = process.argv.slice(2);
|
||||
const account = args[0];
|
||||
|
||||
if (!account) {
|
||||
console.error(JSON.stringify({
|
||||
error: 'Missing account',
|
||||
usage: 'node list_messages.js <account> [--unread] [--query "..."] [--max N] [--label LABEL]'
|
||||
}));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Parse options
|
||||
const options = {};
|
||||
for (let i = 1; i < args.length; i++) {
|
||||
switch (args[i]) {
|
||||
case '--unread':
|
||||
options.unread = true;
|
||||
break;
|
||||
case '--query':
|
||||
options.query = args[++i];
|
||||
break;
|
||||
case '--max':
|
||||
options.max = parseInt(args[++i], 10);
|
||||
break;
|
||||
case '--label':
|
||||
options.label = args[++i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await listMessages(account, options);
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
} catch (error) {
|
||||
console.error(JSON.stringify({
|
||||
error: error.message,
|
||||
account,
|
||||
}));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
module.exports = { listMessages };
|
||||
126
skills/google-workspace/gmail/read_message.js
Normal file
126
skills/google-workspace/gmail/read_message.js
Normal file
@@ -0,0 +1,126 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Read Gmail Message
|
||||
*
|
||||
* Usage: node read_message.js <account> <message-id>
|
||||
*
|
||||
* Examples:
|
||||
* node read_message.js psd 18d1234567890abc
|
||||
*/
|
||||
|
||||
const { google } = require('googleapis');
|
||||
const path = require('path');
|
||||
const { getAuthClient } = require(path.join(__dirname, '..', 'auth', 'token_manager'));
|
||||
|
||||
async function readMessage(account, messageId) {
|
||||
const auth = await getAuthClient(account);
|
||||
const gmail = google.gmail({ version: 'v1', auth });
|
||||
|
||||
const response = await gmail.users.messages.get({
|
||||
userId: 'me',
|
||||
id: messageId,
|
||||
format: 'full',
|
||||
});
|
||||
|
||||
const message = response.data;
|
||||
const headers = message.payload.headers;
|
||||
|
||||
const getHeader = (name) => {
|
||||
const header = headers.find(h => h.name.toLowerCase() === name.toLowerCase());
|
||||
return header ? header.value : '';
|
||||
};
|
||||
|
||||
// Extract body
|
||||
let body = '';
|
||||
let htmlBody = '';
|
||||
|
||||
function extractBody(payload) {
|
||||
if (payload.body && payload.body.data) {
|
||||
const decoded = Buffer.from(payload.body.data, 'base64').toString('utf8');
|
||||
if (payload.mimeType === 'text/plain') {
|
||||
body = decoded;
|
||||
} else if (payload.mimeType === 'text/html') {
|
||||
htmlBody = decoded;
|
||||
}
|
||||
}
|
||||
|
||||
if (payload.parts) {
|
||||
for (const part of payload.parts) {
|
||||
extractBody(part);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extractBody(message.payload);
|
||||
|
||||
// Get attachments info
|
||||
const attachments = [];
|
||||
function findAttachments(payload) {
|
||||
if (payload.filename && payload.body && payload.body.attachmentId) {
|
||||
attachments.push({
|
||||
filename: payload.filename,
|
||||
mimeType: payload.mimeType,
|
||||
size: payload.body.size,
|
||||
attachmentId: payload.body.attachmentId,
|
||||
});
|
||||
}
|
||||
if (payload.parts) {
|
||||
for (const part of payload.parts) {
|
||||
findAttachments(part);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
findAttachments(message.payload);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
account,
|
||||
message: {
|
||||
id: message.id,
|
||||
threadId: message.threadId,
|
||||
from: getHeader('From'),
|
||||
to: getHeader('To'),
|
||||
cc: getHeader('Cc'),
|
||||
subject: getHeader('Subject'),
|
||||
date: getHeader('Date'),
|
||||
body: body || htmlBody,
|
||||
isHtml: !body && !!htmlBody,
|
||||
labels: message.labelIds,
|
||||
attachments,
|
||||
},
|
||||
metadata: {
|
||||
timestamp: new Date().toISOString(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// CLI interface
|
||||
async function main() {
|
||||
const [account, messageId] = process.argv.slice(2);
|
||||
|
||||
if (!account || !messageId) {
|
||||
console.error(JSON.stringify({
|
||||
error: 'Missing arguments',
|
||||
usage: 'node read_message.js <account> <message-id>'
|
||||
}));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await readMessage(account, messageId);
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
} catch (error) {
|
||||
console.error(JSON.stringify({
|
||||
error: error.message,
|
||||
account,
|
||||
messageId,
|
||||
}));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
module.exports = { readMessage };
|
||||
130
skills/google-workspace/gmail/search_messages.js
Normal file
130
skills/google-workspace/gmail/search_messages.js
Normal file
@@ -0,0 +1,130 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Search Gmail Messages
|
||||
*
|
||||
* Usage: node search_messages.js <account> <query>
|
||||
*
|
||||
* Supports all Gmail search operators:
|
||||
* from: - Sender
|
||||
* to: - Recipient
|
||||
* subject: - Subject line
|
||||
* has:attachment - Has attachments
|
||||
* after: - After date (YYYY/MM/DD)
|
||||
* before: - Before date
|
||||
* is:unread - Unread only
|
||||
* is:starred - Starred only
|
||||
* label: - By label
|
||||
* filename: - Attachment filename
|
||||
*
|
||||
* Examples:
|
||||
* node search_messages.js psd "from:boss@psd.org"
|
||||
* node search_messages.js personal "subject:invoice after:2024/01/01"
|
||||
* node search_messages.js consulting "has:attachment from:client"
|
||||
*/
|
||||
|
||||
const { google } = require('googleapis');
|
||||
const path = require('path');
|
||||
const { getAuthClient } = require(path.join(__dirname, '..', 'auth', 'token_manager'));
|
||||
|
||||
async function searchMessages(account, query, maxResults = 20) {
|
||||
const auth = await getAuthClient(account);
|
||||
const gmail = google.gmail({ version: 'v1', auth });
|
||||
|
||||
const response = await gmail.users.messages.list({
|
||||
userId: 'me',
|
||||
q: query,
|
||||
maxResults,
|
||||
});
|
||||
|
||||
const messages = response.data.messages || [];
|
||||
|
||||
if (messages.length === 0) {
|
||||
return {
|
||||
success: true,
|
||||
account,
|
||||
query,
|
||||
messages: [],
|
||||
metadata: {
|
||||
timestamp: new Date().toISOString(),
|
||||
count: 0,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Get message details
|
||||
const messageDetails = await Promise.all(
|
||||
messages.map(async (msg) => {
|
||||
const detail = await gmail.users.messages.get({
|
||||
userId: 'me',
|
||||
id: msg.id,
|
||||
format: 'metadata',
|
||||
metadataHeaders: ['From', 'To', 'Subject', 'Date'],
|
||||
});
|
||||
|
||||
const headers = detail.data.payload.headers;
|
||||
const getHeader = (name) => {
|
||||
const header = headers.find(h => h.name.toLowerCase() === name.toLowerCase());
|
||||
return header ? header.value : '';
|
||||
};
|
||||
|
||||
return {
|
||||
id: msg.id,
|
||||
threadId: msg.threadId,
|
||||
from: getHeader('From'),
|
||||
to: getHeader('To'),
|
||||
subject: getHeader('Subject'),
|
||||
date: getHeader('Date'),
|
||||
snippet: detail.data.snippet,
|
||||
labels: detail.data.labelIds,
|
||||
isUnread: detail.data.labelIds.includes('UNREAD'),
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
account,
|
||||
query,
|
||||
messages: messageDetails,
|
||||
metadata: {
|
||||
timestamp: new Date().toISOString(),
|
||||
count: messageDetails.length,
|
||||
nextPageToken: response.data.nextPageToken,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// CLI interface
|
||||
async function main() {
|
||||
const [account, ...queryParts] = process.argv.slice(2);
|
||||
const query = queryParts.join(' ');
|
||||
|
||||
if (!account || !query) {
|
||||
console.error(JSON.stringify({
|
||||
error: 'Missing arguments',
|
||||
usage: 'node search_messages.js <account> <query>',
|
||||
examples: [
|
||||
'node search_messages.js psd "from:boss@psd.org"',
|
||||
'node search_messages.js personal "subject:invoice after:2024/01/01"',
|
||||
]
|
||||
}));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await searchMessages(account, query);
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
} catch (error) {
|
||||
console.error(JSON.stringify({
|
||||
error: error.message,
|
||||
account,
|
||||
query,
|
||||
}));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
module.exports = { searchMessages };
|
||||
175
skills/google-workspace/gmail/send_message.js
Normal file
175
skills/google-workspace/gmail/send_message.js
Normal file
@@ -0,0 +1,175 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Send Gmail Message
|
||||
*
|
||||
* Usage: node send_message.js <account> --to <email> --subject <subject> --body <body>
|
||||
*
|
||||
* Options:
|
||||
* --to Recipient email (required)
|
||||
* --subject Email subject (required)
|
||||
* --body Email body (required)
|
||||
* --cc CC recipients (comma-separated)
|
||||
* --bcc BCC recipients (comma-separated)
|
||||
* --reply-to Message ID to reply to
|
||||
*
|
||||
* Examples:
|
||||
* node send_message.js psd --to "john@example.com" --subject "Hello" --body "Message here"
|
||||
* node send_message.js personal --to "friend@gmail.com" --subject "Lunch?" --body "Are you free?" --cc "other@gmail.com"
|
||||
*/
|
||||
|
||||
const { google } = require('googleapis');
|
||||
const path = require('path');
|
||||
const { getAuthClient } = require(path.join(__dirname, '..', 'auth', 'token_manager'));
|
||||
|
||||
async function sendMessage(account, options) {
|
||||
const auth = await getAuthClient(account);
|
||||
const gmail = google.gmail({ version: 'v1', auth });
|
||||
|
||||
// Get sender email
|
||||
const profile = await gmail.users.getProfile({ userId: 'me' });
|
||||
const fromEmail = profile.data.emailAddress;
|
||||
|
||||
// Build email
|
||||
const emailLines = [
|
||||
`From: ${fromEmail}`,
|
||||
`To: ${options.to}`,
|
||||
];
|
||||
|
||||
if (options.cc) {
|
||||
emailLines.push(`Cc: ${options.cc}`);
|
||||
}
|
||||
|
||||
if (options.bcc) {
|
||||
emailLines.push(`Bcc: ${options.bcc}`);
|
||||
}
|
||||
|
||||
emailLines.push(
|
||||
`Subject: ${options.subject}`,
|
||||
'Content-Type: text/plain; charset=utf-8',
|
||||
'',
|
||||
options.body
|
||||
);
|
||||
|
||||
// If replying, add thread info
|
||||
if (options.replyTo) {
|
||||
const original = await gmail.users.messages.get({
|
||||
userId: 'me',
|
||||
id: options.replyTo,
|
||||
format: 'metadata',
|
||||
metadataHeaders: ['Message-ID', 'References'],
|
||||
});
|
||||
|
||||
const headers = original.data.payload.headers;
|
||||
const messageIdHeader = headers.find(h => h.name === 'Message-ID');
|
||||
const referencesHeader = headers.find(h => h.name === 'References');
|
||||
|
||||
if (messageIdHeader) {
|
||||
const references = referencesHeader
|
||||
? `${referencesHeader.value} ${messageIdHeader.value}`
|
||||
: messageIdHeader.value;
|
||||
emailLines.splice(2, 0, `In-Reply-To: ${messageIdHeader.value}`);
|
||||
emailLines.splice(3, 0, `References: ${references}`);
|
||||
}
|
||||
}
|
||||
|
||||
const email = emailLines.join('\r\n');
|
||||
const encodedEmail = Buffer.from(email).toString('base64')
|
||||
.replace(/\+/g, '-')
|
||||
.replace(/\//g, '_')
|
||||
.replace(/=+$/, '');
|
||||
|
||||
const sendParams = {
|
||||
userId: 'me',
|
||||
requestBody: {
|
||||
raw: encodedEmail,
|
||||
},
|
||||
};
|
||||
|
||||
if (options.replyTo) {
|
||||
const original = await gmail.users.messages.get({
|
||||
userId: 'me',
|
||||
id: options.replyTo,
|
||||
format: 'minimal',
|
||||
});
|
||||
sendParams.requestBody.threadId = original.data.threadId;
|
||||
}
|
||||
|
||||
const response = await gmail.users.messages.send(sendParams);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
account,
|
||||
sent: {
|
||||
id: response.data.id,
|
||||
threadId: response.data.threadId,
|
||||
to: options.to,
|
||||
subject: options.subject,
|
||||
},
|
||||
metadata: {
|
||||
timestamp: new Date().toISOString(),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// CLI interface
|
||||
async function main() {
|
||||
const args = process.argv.slice(2);
|
||||
const account = args[0];
|
||||
|
||||
if (!account) {
|
||||
console.error(JSON.stringify({
|
||||
error: 'Missing account',
|
||||
usage: 'node send_message.js <account> --to <email> --subject <subject> --body <body>'
|
||||
}));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Parse options
|
||||
const options = {};
|
||||
for (let i = 1; i < args.length; i++) {
|
||||
switch (args[i]) {
|
||||
case '--to':
|
||||
options.to = args[++i];
|
||||
break;
|
||||
case '--subject':
|
||||
options.subject = args[++i];
|
||||
break;
|
||||
case '--body':
|
||||
options.body = args[++i];
|
||||
break;
|
||||
case '--cc':
|
||||
options.cc = args[++i];
|
||||
break;
|
||||
case '--bcc':
|
||||
options.bcc = args[++i];
|
||||
break;
|
||||
case '--reply-to':
|
||||
options.replyTo = args[++i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!options.to || !options.subject || !options.body) {
|
||||
console.error(JSON.stringify({
|
||||
error: 'Missing required options: --to, --subject, --body',
|
||||
usage: 'node send_message.js <account> --to <email> --subject <subject> --body <body>'
|
||||
}));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await sendMessage(account, options);
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
} catch (error) {
|
||||
console.error(JSON.stringify({
|
||||
error: error.message,
|
||||
account,
|
||||
}));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
module.exports = { sendMessage };
|
||||
Reference in New Issue
Block a user