Skip to content

Instantly share code, notes, and snippets.

@berlysia
Last active May 26, 2023 03:38
Show Gist options
  • Save berlysia/5bfce4a35cae56d6694cce87da2ee37c to your computer and use it in GitHub Desktop.
Save berlysia/5bfce4a35cae56d6694cce87da2ee37c to your computer and use it in GitHub Desktop.
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