Last active
May 26, 2023 03:38
-
-
Save berlysia/5bfce4a35cae56d6694cce87da2ee37c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkEmailsAndSendToDiscord() { | |
impl("prod") | |
} | |
function dev() { | |
impl("dev") | |
} | |
const YOUR_QUERY = ""; | |
const configs = { | |
prod: { | |
discordWebhookUrl: 'your discord webhook URL', | |
getThreads: () => GmailApp.search('is:unread ' + YOUR_QUERY), | |
}, | |
dev: { | |
discordWebhookUrl: 'your discord webhook URL', | |
getThreads: () => GmailApp.getInboxThreads(), | |
} | |
} | |
function impl(env) { | |
const config = configs[env]; | |
if (!config) return; | |
const discordWebhookUrl = config.discordWebhookUrl; | |
const threads = config.getThreads(); | |
Logger.log("threads: "+(threads.length)); | |
let count = 0; | |
for (const thread of threads) { | |
// メールの本文を取得 | |
const messages = thread.getMessages(); | |
Logger.log("messages: "+(messages.length)); | |
for (const message of messages) { | |
if (!message.isUnread()) continue; | |
Logger.log(message.getSubject()) | |
const subject = message.getSubject(); | |
const body = message.getPlainBody(); | |
const timestamp = message.getDate(); | |
// メールの内容をDiscordに送信 | |
UrlFetchApp.fetch(discordWebhookUrl, { | |
method: 'post', | |
payload: JSON.stringify({ | |
content: ``, | |
"embeds": [ | |
{ | |
"type": "rich", | |
"title": subject, | |
"description": body.slice(0, 1800), | |
"color": 0x00FFFF, | |
"timestamp": timestamp.toISOString(), | |
} | |
] | |
}), | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
}); | |
count++; | |
// メールを既読にする | |
message.markRead(); | |
} | |
} | |
Logger.log("送信完了: " + count); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment