gsp ,google apps script ,类似于云函数 优点是:gsp 集成 gmail 不经第三方,可以按分类过滤垃圾、推广邮件 免费版一天最多 20000 次请求,大概 4-5s 拉一次邮件刚好够用
/*
Author: 風信子 t.me/TeleDele, umzr: github.com/umzr
/*
set Webhook
https://api.telegram.org/bot<TelegramBotToken>/setWebhook?url=<>
*/
var clientID = "-100215133123"
var TelegramBotToken = "4234421:..";
var TelegramBotAPI = "https://api.telegram.org/bot" + TelegramBotToken + "/";
function continuousCheck() {
var runDuration = 60000;
var startTime = new Date().getTime();
var i = 0;
while (new Date().getTime() - startTime < runDuration) {
searchUnreadEmail();
i++;
Utilities.sleep(5000);
Logger.log("--->" + i);
}
}
function searchUnreadEmail() {
var threads = GmailApp.search('is:unread category:primary -category:social -category:promotions newer_than:7d');// 过滤条件
for (var i = threads.length - 1; i >= 0; i--) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
if (message.isUnread()) {
var sender = message.getFrom().replace(/<|>/g, '');
var recipient = message.getTo().replace(/.*<(.+)>/, '$1').trim();
var subject = message.getSubject();
var date = message.getDate();
var plainBody = message.getPlainBody();
var formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), 'yyyy-MM-dd HH:mm:ss');
// 移除软连字符和其他特殊字符
plainBody = plainBody.replace(/[\u200B-\u200D\uFEFF\u00AD\s]+/g, ' ').trim();
// 删除多余的空白
plainBody = plainBody.replace(/(\s{22,})/g, '');
// plainBody = plainBody.replace(/<([^>]+)>/g, '<a href="$1">链接</a>');
console.log("plainBody:" + plainBody);
plainBody = plainBody.replace(/<?( https?:\/\/[^\s/$.?#].[^\s]*)>?/g, function(match, p1) {
var url = p1.replace(/[<>]/g, ''); // 去掉尖括号
var domain = url.split('/')[2]; // 提取域名部分
return '<a href="' + url + '">' + domain + '...</a>';
});
console.log("plainBody:" + plainBody);
// 截取纯文本的前 800 个字符
if (plainBody.length > 800) {
plainBody = plainBody.substring(0, 800);
// 判断是否在 a 标签内
var lastOpeningTag = plainBody.lastIndexOf('<a ');
var lastClosingTag = plainBody.lastIndexOf('</a>');
// 如果最后一个 '<a ' 在截取后没有被完整闭合
if (lastOpeningTag > lastClosingTag) {
// 查找完整的闭合标签
var closingTagEnd = plainBody.indexOf('>', lastOpeningTag);
// 如果闭合标签存在,截取到标签结束的位置
if (closingTagEnd !== -1) {
plainBody = plainBody.substring(0, closingTagEnd + 1);
} else {
// 否则直接删除未完整的标签部分
plainBody = plainBody.substring(0, lastOpeningTag);
}
}
}
var telegramMessage = "<b><u>" + subject + "</u>\n" + formattedDate + "\nFrom:" + sender +
"</b>\n\n" + plainBody + "\n\n" + recipient +
' <a href="https://mail.google.com/mail/u/0/#inbox/'
+ message.getId() + '"><u>Link</u></a>';
console.log("TG:" + telegramMessage);
// 发送到 Telegram
pushTelegramBotMessage(telegramMessage, message.getId());
// 标记消息为已读
message.markRead();
}
}
}
}
// Function to push message to Telegram Bot
function pushTelegramBotMessage(message, messageId) {
var payload = {
"method": "sendMessage",
"chat_id": clientID,
"text": message,
"parse_mode": "HTML"
};
var options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
UrlFetchApp.fetch(TelegramBotAPI + "sendMessage", options);
}```
以上做了些修改,复制过去设置自己的参数 chatid ,token ,设置个触发器就能用
1
DefoliationM 113 天前 via Android
谷歌官方不是有 Gmail bot 吗
|
2
villivateur 113 天前
同楼上,你 telegram 搜一搜 gmail 的 bot 呢
|
3
vasto OP |
4
lovestudykid 113 天前
@DefoliationM https://telegram.org/privacy/gmailbot
这个应该是 telegram 官方,google 是不可能官方在 telegram 上提供 bot 的 |
5
Rrrrrr 113 天前
@villivateur #2 有没有微软的 bot
|
6
scys 113 天前
用了:D 感谢
|