📢 转载信息
原文链接:https://sspai.com/post/103972
原文作者:小胡小胡0009
虽然市面上的邮件客户端或插件已具备单封邮件的总结与翻译功能,但往往「缺乏对当天所有邮件进行全局摘要的能力」。因此我借助系统自带邮件应用 (Mail app) 和扣子智能体,实现了以下的工作流:
「通过 AppleScript 获取 Mail app 的邮件内容,调用扣子智能体总结,并将总结内容发送到自己的邮箱里,每天自动执行。」
具体的实现流程可以分为三步:
- 获取邮件内容
- 调用扣子智能体总结内容
- 配置自动操作自动执行
最终实现的效果:每天你会准时收到一封包含所有未读邮件内容总结的邮件,如下图所示。
获取邮件内容
为避免影响文章整体的阅读体验,我将获取邮件内容的完整 AppleScript 放在文末,这里主要说明几个核心逻辑:
- 我在脚本中指定了邮箱账户
iCloud和邮箱文件夹newsletter,因为newsletter文件夹会归集我所有的邮件订阅; - 脚本只会获取未读邮件的内容,并会在成功获取内容后将邮件设置为已读;
- 获取到的邮件内容会以 txt 文件形式保存到指定文件夹中,使用邮件主题作为文件名。既能简化数据存储,又能实现与后续 AI 处理步骤的解耦,方便独立调试;
- 获取邮件的 message-ID 并拼接成
message://%3c'messageID'%3e格式的 URI。这种 URI 可以在点击时激活 Mail app,并以弹窗形式打开对应的邮件,方便我在阅读摘要后快速打开原文。
经过这一步,我们已经获取了指定邮箱文件夹中未读邮件的内容,并以 txt 文件的形式保存在文件夹中。
调用扣子智能体总结邮件内容
我使用 JavaScript 脚本来读取上一步保存的文件内容,并调用扣子 API 总结邮件内容。当然,你也可以使用 DeepSeek 或是任意一家厂商的 API。使用扣子 API 是看中了它的异步查询功能。完整的脚本同样放在文末。
脚本执行逻辑
- 读取指定目录下的所有文件:脚本会遍历指定文件夹目录下的所有文件;
- 筛选当天创建的
.txt文件:只处理创建日期为当天且扩展名为.txt的文件; - 读取文件内容并调用发起对话接口:对每个有效文件,读取内容后调用 扣子 API 的 chat 接口,提交内容进行处理;
- 轮询等待 AI 处理完成:通过轮询 chat/retrieve 接口,等待 AI 处理状态变为
completed; - 查询 AI 结果并整理内容:查询 chat/message/list 接口,获取 AI 的回答内容,并将结果格式化为 HTML,累计到
allContents变量; - 所有文件处理完毕后发送邮件:当所有有效文件都处理完毕后,使用 nodemailer 通过邮箱发送一封邮件,内容为当天所有文件的 AI 总结;
- 邮件内容包括未读邮件数量和所有 AI 总结:邮件主题为当天日期,正文包括未读邮件数量和所有文件的 AI 总结内容。
扣子智能体设置及调用
在扣子空间创建智能体,并设置好智能体的 prompt。在这个页面的地址栏中,你可以获取到 Bot_ID ,作为参数调用 API。
扣子 API 调用分为三步:发起对话 - 查询状态 - 查询结果。
「第一步发起对话」,相当于向扣子提交一个处理申请。
这一步你需要提供 API Key、Bot_ID 以及自定义的 User_ID ,扣子会返回 Chat_id 和 Conversation_ID 作为唯一标识。
「第二步查询状态」,这一步相当于询问扣子我们的申请处理好了没有,当获取到 completed 状态后,再去获取处理结果,否则可能会获取到不完整的内容。
在这一步需要提供上一步返回的 Chat_ID 和 Conversation_ID 来查询结果。
「第三步查询结果」,这一步就是获取智能体最终的处理结果。同样需要提供第一步返回的 Chat_ID 和 Conversation_ID 来查询结果。
配置自动操作自动执行
有了获取邮件内容的 AppleScript 脚本和调用扣子智能体的 JavaScript 脚本,我们还需要创建一个自动操作来定期执行它们。
打开「自动操作」app 创建一个「日历提醒」类型的自动操作:
添加「运行 AppleScript」操作,将 AppleScript 脚本内容粘贴进去,记得要在外围包裹执行语句,类似:
on run{input, parameters} AppleScript 脚本 end run
添加「运行 Shell 脚本」操作,导航到脚本所在文件夹,并执行 JavaScript 脚本:
cd 脚本路径 /opt/homebrew/bin/node 脚本名称
添加「显示通知」来提示执行完成。
自动操作配置页面如下图:
创建完成后,在日历 app 中新建日程,将日程设置每天重复,并在「提醒」配置项中选择「自定义 - 打开文件 - 其他 - 选择你创建的自动操作」,就能自动执行啦!
至此,我们已经完成了全部的设置流程,不过在使用时请注意隐私安全,建议仅对 Newsletter 或公开资讯类邮件使用此流程,避免上传包含个人敏感信息的私人邮件。
获取邮件内容的 AppleScript
tell application "Mail" -- 获取 iCloud 账户中的 newsletter 邮箱 set theAccount to account "iCloud" set theMailbox to mailbox "newsletter" of theAccount -- 获取 newsletter 邮箱中的所有未读邮件 set unreadMessages to (every message of theMailbox whose read status is false) -- 遍历所有未读邮件 repeat with eachMessage in unreadMessages -- 获取邮件的主题作为文件名的一部分 set theSubject to subject of eachMessage -- 获取邮件的正文并转换为纯文本格式 set theContent to content of eachMessage set plainTextContent to do shell script "echo " & quoted form of theContent & " | textutil -convert txt -stdin -stdout" -- 获取邮件的 message-ID 并生成 URI set messageID to message id of eachMessage set messageURL to "message://%3c" & messageID & "%3e" -- 创建文件名,移除文件名中的不合法字符 set cleanSubject to do shell script "echo " & quoted form of theSubject & " | tr -d '\"/:<>?\|+[]{};=,'" set fileName to (cleanSubject & ".txt") -- 使用指定的 POSIX 路径并转换为 AppleScript 路径格式 set savePath to POSIX file "保存 txt 文件的路径" -- 完整的文件路径 set filePath to (savePath as string) & fileName -- 将邮件内容写入到指定路径的 txt 文件 try set fileReference to open for access file filePath with write permission write plainTextContent to fileReference starting at eof -- 在文件末尾添加邮件 URI write return & "emailURL=" & messageURL to fileReference starting at eof close access fileReference on error errMsg close access file filePath display dialog "Error: " & errMsg end try -- 将邮件标记为已读 set read status of eachMessage to true end repeat end tell return input
调用扣子智能体的脚本
const fs = require('fs'); const path = require('path'); const axios = require('axios'); const nodemailer = require('nodemailer'); const directoryPath = '存放文本文件的目录路径'; // 指定存放文本文件的目录路径 const today = new Date().toLocaleDateString('zh-CN'); // 获取系统本地日期 let allContents = ''; // 用于存储所有查询接口返回的内容 let fileCounter = 0; // 用于文件计数 let validFileCount = 0; // 有效文件计数 let emailSent = false; // 添加一个标志来跟踪邮件是否已发送 // 读取指定目录下的文件 fs.readdir(directoryPath, async (err, files) => { if (err) { console.error('无法读取目录:', err); return; } const filePromises = files.map(file => processFile(file)); await Promise.all(filePromises); checkAllFilesProcessed(); }); async function processFile(file) { const filePath = path.join(directoryPath, file); try { const stats = await fs.promises.stat(filePath); const fileCreationDate = stats.birthtime.toLocaleDateString('zh-CN'); // 获取文件创建的本地日期 if (fileCreationDate === today && path.extname(file) === '.txt') { validFileCount++; // 增加有效文件计数 const content = await fs.promises.readFile(filePath, 'utf8'); await callAIAPI(content, file); } } catch (err) { console.error('处理文件时出错:', err); } } // 使用 async/await 优化异步处理 async function callAIAPI(content, fileName) { const apiUrl = 'https://api.coze.cn/v3/chat'; const headers = getHeaders(); // 根据 content 的长度设置 bot_id const botId = content.length > 32000 ? 'bot_id' : 'bot_id'; const data = { bot_id: botId, // 使用动态 bot_id user_id: '**', stream: false, auto_save_history: true, additional_messages: [ { role: 'user', content: content, content_type: 'text' } ] }; let attempts = 0; const maxAttempts = 5; while (attempts < maxAttempts) { try { const response = await axios.post(apiUrl, data, { headers, timeout: 5000 }); console.log('API响应:', response.data); const { id, conversation_id } = response.data.data; await new Promise(resolve => setTimeout(resolve, 1000)); // 增加等待时间,1秒钟 await pollConversationStatus(id, conversation_id, fileName); break; // 成功后退出循环 } catch (error) { attempts++; console.error(`API调用错误 (尝试 ${attempts}/${maxAttempts}):`, error); if (attempts >= maxAttempts) { console.error('多次尝试后仍然失败'); } } } } // 提取 headers 设置函数 function getHeaders() { return { 'Authorization': 'Bearer api_key', 'Content-Type': 'application/json' }; } // 轮询对话详情接口的函数 async function pollConversationStatus(chat_id, conversation_id, fileName) { const retrieveUrl = `https://api.coze.cn/v3/chat/retrieve?chat_id=${chat_id}&conversation_id=${conversation_id}`; const headers = getHeaders(); let pollCount = 0; const maxPollCount = 120; return new Promise((resolve, reject) => { const intervalId = setInterval(async () => { pollCount++; if (pollCount > maxPollCount) { clearInterval(intervalId); reject(new Error(`轮询超时,chat_id: ${chat_id}, conversation_id: ${conversation_id}`)); return; } try { const response = await axios.get(retrieveUrl, { headers }); // 取消打印轮询接口的日志 // console.log('对话详情API响应:', response.data); if (response.data.data.status === 'completed') { clearInterval(intervalId); await queryAIAPI(chat_id, conversation_id, fileName); resolve(); } } catch (error) { console.error('对话详情API调用错误:', error); clearInterval(intervalId); reject(error); } }, 1000); // 每秒调用一次 }); } // 调用查询接口的函数 async function queryAIAPI(chat_id, conversation_id, fileName) { const queryUrl = `https://api.coze.cn/v3/chat/message/list?chat_id=${chat_id}&conversation_id=${conversation_id}`; const headers = getHeaders(); try { const response = await axios.get(queryUrl, { headers }); console.log('查询API响应:', response.data); const contents = response.data.data
.filter(item => item.type === 'answer') // 过滤出 type 为 'answer' 的元素
.map(item => item.content)
.join('
'); // 读取文件的最后一行以获取 MessageID const filePath = path.join(directoryPath, fileName); const fileContent = await fs.promises.readFile(filePath, 'utf8'); const lastLine = fileContent.trim().split('
').pop(); const messageId = lastLine.split('=')[1]; // 提取 MessageID fileCounter++; // 增加文件计数 console.log(`文件计数: ${fileCounter}`); // 打印 fileCounter 的日志 const fileNameWithoutExt = fileName.replace(/\.[^/.]+$/, ""); // 删除文件扩展名 allContents += `${fileCounter}. ${fileNameWithoutExt}
${contents.replace(/\n/g, '
')}
`; // 将换行符替换为 <br> } catch (error) { console.error('查询API调用错误:', error); } } // 检查是否所有文件都已处理完毕 function checkAllFilesProcessed() { if (fileCounter === validFileCount && !emailSent) { emailSent = true; // 设置标志,表示邮件已发送 sendEmail(allContents); // 调用发送邮件函数 } } // 发送邮件的函数 function sendEmail(contents) { const transporter = nodemailer.createTransport({ host: 'smtp.qq.com', port: 465, secure: true, // 使用 SSL auth: { user: '邮箱地址', // 你的邮箱地址 pass: '邮箱授权码' // 你的邮箱授权码 } }); const mailOptions = { from: '邮箱地址', // 发送者邮箱地址 to: '邮箱地址', // 收件人邮箱地址 subject: `${today} 当日邮件总结`, // 邮件主题 html: `未读邮件数量: ${validFileCount}
${contents}` // 将内容类型改为 HTML }; transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.error('无法发送邮件:', error); } console.log('邮件已发送:', info.response); }); }
> 关注 少数派小红书,感受精彩数字生活 🍃
> 实用、好用的 正版软件,少数派为你呈现 🚀
🚀 想要体验更好更全面的AI调用?
欢迎使用青云聚合API,约为官网价格的十分之一,支持300+全球最新模型,以及全球各种生图生视频模型,无需翻墙高速稳定,文档丰富,小白也可以简单操作。
评论区