function main() { const now = (new Date()).getTime(); const threads = settings.flatMap(({ channel, query }) => { const threads = GmailApp .search(query(now)); console.log(`send ${threads.length} messages`); console.log(`query: ${query(now)}`); return threads.map(thread => { // 最新のメールだけ取得する const message = thread.getMessages().pop(); send(message, channel); return thread; }); }); // 最後にまとめて既読にする threads.forEach(thread => thread.markRead()); } function send(message, channel) { const body = { text: message.getSubject(), ...blocks( header(message.getSubject()), context(mrkdwn([ `Date: ${message.getDate()}`, `From: ${message.getFrom()}`, `To: ${message.getTo()}`, ].join('\n'))), section(message.getPlainBody()), ), }; try { UrlFetchApp.fetch(channel, { method: 'POST', contentType: 'application/json', payload: JSON.stringify(body), muteHttpExceptions: true, }); } catch (e) { console.error(e); } }