Google Apps Script + Server酱:把 Google Workspace 事件推送到微信
效果:Gmail 收到特定邮件、Sheets 数据变更或 Calendar 日程提醒时,自动通过 Server酱 推送消息到微信。
前置条件
- Server酱 SendKey:登录 sct.ftqq.com,在「SendKey」页面复制。详见 30 秒获取 SendKey。
- Google 账号:需有访问 Gmail / Google Sheets / Google Calendar 等 Workspace 服务的权限。
- Server酱 支持推送到微信服务号/企业微信/钉钉/飞书等多个通道,详见 /getting-started/channels/。
配置步骤
1. 将 SendKey 存入 Script Properties
打开 Apps Script 编辑器,在编辑器中运行一次以下函数,将 SendKey 写入脚本属性:
function setupSendKey() {
PropertiesService.getScriptProperties().setProperty('SERVERCHAN_SENDKEY', '你的SENDKEY');
}
运行后即完成写入。
2. 编写推送函数
在同一个脚本项目中新建函数,用 UrlFetchApp.fetch() 调用 Server酱 API:
function sendServerChan(title, desp) {
var sendKey = PropertiesService.getScriptProperties().getProperty('SERVERCHAN_SENDKEY');
var url = 'https://sctapi.ftqq.com/' + sendKey + '.send';
UrlFetchApp.fetch(url, {
method: 'post',
payload: {
title: title,
desp: desp
},
muteHttpExceptions: true
});
}
3. 配置触发器
可根据需要配置时间驱动触发器或事件触发器(如 onEdit),在触发函数中调用 sendServerChan 推送消息。在 Apps Script 编辑器中添加触发器。
完整示例
以下示例展示如何调用 sendServerChan 发送一条测试消息:
function testNotification() {
sendServerChan('测试标题', '这是一条来自 Google Apps Script 的测试消息。');
}
配置时间驱动触发器,定时运行 testNotification 即可。
如果使用 Server酱³(SendKey 以 sctp 开头),API 地址改为
https://你的UID.push.ft07.com/send/你的SENDKEY.send,其中 UID 是 sctp 后面的数字。
常见问题
SendKey 应该硬编码在脚本里吗?
不建议。推荐使用 PropertiesService.getScriptProperties() 将 SendKey 存储为脚本属性,避免在代码中暴露密钥。
消息没有收到怎么办?
先检查 SendKey 是否正确写入 Script Properties,再确认 UrlFetchApp.fetch 返回的响应 JSON 中 code 是否为 0。更多排查步骤见 /getting-started/faq/。
每天能发多少条?
免费额度为每天 5 条,每分钟上限 50 条。订阅会员目前特价最低 3 元/月,一天最多可发 1000 条(价格详情)。
触发器执行了但没收到推送?
检查 Apps Script 执行记录,确认 UrlFetchApp.fetch 是否报错。Gmail 场景需注意首次运行授权,未授权的脚本不会执行网络请求。
相关集成
- /integrations/python/ —— Python 脚本推送
- /integrations/qinglong/ —— 青龙面板任务通知
- /integrations/uptime-kuma/ —— Uptime Kuma 宕机告警