Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 08:35:59 +08:00
commit 90883a4d25
287 changed files with 75058 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
#!/usr/bin/env node
/**
* List Google Chat Spaces
*
* Usage: node list_spaces.js <account>
*
* Examples:
* node list_spaces.js psd
* node list_spaces.js kh
*/
const { google } = require('googleapis');
const path = require('path');
const { getAuthClient } = require(path.join(__dirname, '..', 'auth', 'token_manager'));
async function listSpaces(account) {
const auth = await getAuthClient(account);
const chat = google.chat({ version: 'v1', auth });
const response = await chat.spaces.list({
pageSize: 100,
});
// Get read state for each space to find unreads
const spacesWithState = await Promise.all(
(response.data.spaces || []).map(async space => {
try {
// Get space read state
const stateResponse = await chat.spaces.spaceReadState.get({
name: `${space.name}/spaceReadState`,
});
return {
name: space.name,
displayName: space.displayName,
type: space.type,
spaceType: space.spaceType,
lastReadTime: stateResponse.data.lastReadTime,
};
} catch (e) {
return {
name: space.name,
displayName: space.displayName,
type: space.type,
spaceType: space.spaceType,
lastReadTime: null,
};
}
})
);
const spaces = spacesWithState;
return {
success: true,
account,
spaces,
metadata: {
timestamp: new Date().toISOString(),
count: spaces.length,
}
};
}
// CLI interface
async function main() {
const account = process.argv[2];
if (!account) {
console.error(JSON.stringify({
error: 'Missing account',
usage: 'node list_spaces.js <account>'
}));
process.exit(1);
}
try {
const result = await listSpaces(account);
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error(JSON.stringify({
error: error.message,
account,
}));
process.exit(1);
}
}
main();
module.exports = { listSpaces };

View File

@@ -0,0 +1,128 @@
#!/usr/bin/env node
/**
* Read Google Chat Messages from a Space
*
* Usage: node read_messages.js <account> <space-name> [options]
*
* Options:
* --max Maximum messages to return (default: 50)
*
* Examples:
* node read_messages.js psd spaces/AAAA1234567
* node read_messages.js psd spaces/AAAA1234567 --max 100
*/
const { google } = require('googleapis');
const path = require('path');
const fs = require('fs');
const os = require('os');
const { getAuthClient } = require(path.join(__dirname, '..', 'auth', 'token_manager'));
// Load user mapping from iCloud preferences
let allUserMappings = {};
const mappingPath = path.join(
os.homedir(),
'Library/Mobile Documents/com~apple~CloudDocs/Geoffrey/knowledge/chat_user_mapping.json'
);
if (fs.existsSync(mappingPath)) {
try {
allUserMappings = JSON.parse(fs.readFileSync(mappingPath, 'utf8'));
} catch (e) {
// Ignore errors
}
}
async function readMessages(account, spaceName, options = {}) {
const auth = await getAuthClient(account);
const chat = google.chat({ version: 'v1', auth });
// Get space members to build userId -> displayName mapping
const membersResponse = await chat.spaces.members.list({
parent: spaceName,
pageSize: 100,
});
const userNameMap = {};
for (const membership of (membersResponse.data.memberships || [])) {
if (membership.member && membership.member.name) {
const userId = membership.member.name;
userNameMap[userId] = membership.member.displayName || userId;
}
}
const response = await chat.spaces.messages.list({
parent: spaceName,
pageSize: options.max || 50,
orderBy: 'createTime desc',
});
// Get account-specific user mapping
const userMapping = allUserMappings[account] || {};
const messages = (response.data.messages || []).map(msg => {
const senderId = msg.sender?.name;
const bareId = senderId?.replace('users/', '');
const senderName = userMapping[bareId] || userNameMap[senderId] || msg.sender?.displayName || senderId;
return {
name: msg.name,
sender: senderName,
senderId: bareId,
senderType: msg.sender?.type,
text: msg.text,
createTime: msg.createTime,
threadName: msg.thread?.name,
};
});
return {
success: true,
account,
spaceName,
messages,
metadata: {
timestamp: new Date().toISOString(),
count: messages.length,
}
};
}
// CLI interface
async function main() {
const args = process.argv.slice(2);
const account = args[0];
const spaceName = args[1];
if (!account || !spaceName) {
console.error(JSON.stringify({
error: 'Missing arguments',
usage: 'node read_messages.js <account> <space-name> [--max N]'
}));
process.exit(1);
}
// Parse options
const options = {};
for (let i = 2; i < args.length; i++) {
if (args[i] === '--max') {
options.max = parseInt(args[++i], 10);
}
}
try {
const result = await readMessages(account, spaceName, options);
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error(JSON.stringify({
error: error.message,
account,
spaceName,
}));
process.exit(1);
}
}
main();
module.exports = { readMessages };

View File

@@ -0,0 +1,106 @@
#!/usr/bin/env node
/**
* Send Google Chat Message
*
* Usage: node send_message.js <account> <space-name> --text <message>
*
* Options:
* --text Message text (required)
* --thread Thread name to reply to (optional)
*
* Examples:
* node send_message.js psd spaces/AAAA1234567 --text "Hello team!"
* node send_message.js psd spaces/AAAA1234567 --text "Reply" --thread spaces/AAAA/threads/BBBB
*/
const { google } = require('googleapis');
const path = require('path');
const { getAuthClient } = require(path.join(__dirname, '..', 'auth', 'token_manager'));
async function sendMessage(account, spaceName, options) {
const auth = await getAuthClient(account);
const chat = google.chat({ version: 'v1', auth });
const requestBody = {
text: options.text,
};
if (options.thread) {
requestBody.thread = {
name: options.thread,
};
}
const response = await chat.spaces.messages.create({
parent: spaceName,
requestBody,
});
return {
success: true,
account,
message: {
name: response.data.name,
text: response.data.text,
createTime: response.data.createTime,
space: spaceName,
thread: response.data.thread?.name,
},
metadata: {
timestamp: new Date().toISOString(),
}
};
}
// CLI interface
async function main() {
const args = process.argv.slice(2);
const account = args[0];
const spaceName = args[1];
if (!account || !spaceName) {
console.error(JSON.stringify({
error: 'Missing arguments',
usage: 'node send_message.js <account> <space-name> --text <message>'
}));
process.exit(1);
}
// Parse options
const options = {};
for (let i = 2; i < args.length; i++) {
switch (args[i]) {
case '--text':
options.text = args[++i];
break;
case '--thread':
options.thread = args[++i];
break;
}
}
if (!options.text) {
console.error(JSON.stringify({
error: 'Missing --text option',
usage: 'node send_message.js <account> <space-name> --text <message>'
}));
process.exit(1);
}
try {
const result = await sendMessage(account, spaceName, options);
console.log(JSON.stringify(result, null, 2));
} catch (error) {
console.error(JSON.stringify({
error: error.message,
account,
spaceName,
}));
process.exit(1);
}
}
main();
module.exports = { sendMessage };