Skip to content

Instantly share code, notes, and snippets.

@hsinjungwu
Last active September 27, 2021 05:17
Show Gist options
  • Save hsinjungwu/f56faab6a92e53460a12df26df38cda1 to your computer and use it in GitHub Desktop.
Save hsinjungwu/f56faab6a92e53460a12df26df38cda1 to your computer and use it in GitHub Desktop.
gmail to line
function run() {
var msg = queryMail();
if (msg.length > 0) {
lineMessage(msg)
}
}
var token = 'xxx'; //your token
function lineMessage(msg){
var option = {
method: 'post',
headers: { Authorization: 'Bearer ' + token },
payload: {
message: msg
}
};
UrlFetchApp.fetch('https://notify-api.line.me/api/notify', option);
}
function queryMail() {
var rtnMsg = ""
var condition = 'subject:"刷卡通知" is:unread';
var threads = GmailApp.search(condition);
var messages = GmailApp.getMessagesForThreads(threads);
for (var i = 0; i < messages.length; i++) {
for (var j = 0; j < messages[i].length; j++) {
var mail = messages[i][j];
if (mail.isUnread()) {
rtnMsg += "\n" + Utilities.formatDate(mail.getDate(), "GMT+8", "yyyy/MM/dd") + " " + mail.getPlainBody();
mail.markRead();
}
}
}
for (var i = 0; i < messages.length; i++) {
threads[i].moveToArchive();
}
return rtnMsg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment