GASを練習したいと思い、また自分自身Gmailのメールを見逃してしまう事が多いのでそれをなんとか出来ないかと思いスクリプトを作成しました。
gas.js
constLINE_NOTIFY_TOKEN=PropertiesService.getScriptProperties().getProperty('LINE_NOTIFY_TOKEN')constendPoint='https://notify-api.line.me/api/notify'// 1. 転送したいメールの送信元アドレスを指定constfromAddress=[''].join(' OR ')// 2. トリガーの設定間隔と合わせるconstminutesInterval=5functionmain(){constnotices=fetchNotices()if(notices.length===0){return}for(constnoticeofnotices){send(notice)}}functionfetchNotices(){constnow=Math.floor(newDate().getTime()/1000)constintervalMinutesAgo=now-(60*minutesInterval)// 3. 検索条件を設定constquery=`(is:unread from:(${fromAddress}) after:${intervalMinutesAgo})`// 4. メールを取得するconstthreads=GmailApp.search(query)if(threads.length===0){return[]}constmails=GmailApp.getMessagesForThreads(threads)constnotices=[]for(constmessagesofmails){constlatestMessage=messages.pop()constnotice=`
--------------------------------------
件名: ${latestMessage.getSubject()}受信日: ${latestMessage.getDate().toLocaleString()}
From: ${latestMessage.getFrom()}
--------------------------------------
${latestMessage.getPlainBody().slice(0,350)}
`notices.push(notice)// 5. メールを既読にするlatestMessage.markRead()}returnnotices}functionsend(notice){if(LINE_NOTIFY_TOKEN===null){Logger.log('LINE_NOTIFY_TOKEN is not set.')return}constoptions={'method':'POST','headers':{'Authorization':`Bearer ${LINE_NOTIFY_TOKEN}`},'payload':{'message':notice},}UrlFetchApp.fetch(endPoint,options)}
GmailにおけるThreadとMessageの違いについて理解に時間がかかりました。
Thread: あるメールとそのメールに対する一連の返信(配列みたいになる)
Message: 単体のメール1つ
GASもLINE APIも、本当便利…
GASって、他にもいろんな事できるんですね…Googleスプレッドシートを活用して議事録をいじったり…
まだまだ知らない事ばかりなので、一度ガッツリ時間を取って勉強したい。