Last active
August 6, 2019 09:09
-
-
Save ijin/d5e8a3144b2b4c8719ed3c877c769d99 to your computer and use it in GitHub Desktop.
Google Apps Script for PaperCall -> Slack
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
var slackIncomingWebhookUrl = 'https://xxxxxxxxxxxx'; | |
var postChannel = "#serverlessdays2019"; | |
var postIcon = ":papercall:"; | |
var postUser = "PaperCall"; | |
var postColor = "#FF7F50"; | |
var messageFallback = "The attachment must be viewed as plain text."; | |
var messagePretext = "CFPの応募があったよ!\nhttps://www.papercall.io/slsdaystokyo2019"; | |
var title = "Serverless Days Tokyo CFP"; | |
var searchString = "New Submission For ServerlessDays Tokyo 2019"; | |
var maxThreads = 100; | |
function searchContactMail() { | |
var query = searchString; | |
var messages = getMessages(query); | |
var newCount = 0; | |
var totalCount = 0; | |
for (var i = 0 ; i < messages.length; i++) { | |
totalCount += messages[i].length; | |
for (var j = 0; j < messages[i].length; j++) { | |
var msg = messages[i][j]; | |
if (!msg.isStarred()) { | |
//Logger.log("starred: " + msg.isStarred()); | |
console.info("date: " + msg.getDate()); | |
msg.star() | |
newCount++; | |
} | |
} | |
} | |
if (newCount > 0) { | |
submitValuesToSlack(newCount, totalCount); | |
} | |
//Logger.log("Total messages: " + totalCount); | |
console.info("Total messages: " + totalCount); | |
} | |
function getMessages(query) { | |
var threads = GmailApp.search(query, 0, maxThreads); | |
var messages = GmailApp.getMessagesForThreads(threads); | |
//Logger.log("threads:" + threads.length); | |
console.info("threads:" + threads.length); | |
return messages | |
} | |
function submitValuesToSlack(newCount, totalCount) { | |
var fields = [ | |
{ | |
"title" : "新規応募", | |
"value" : newCount, | |
"short" : false | |
}, | |
{ | |
"title" : "総応募件数", | |
"value" : totalCount, | |
"short" : false | |
} | |
]; | |
var attachments = [{ | |
"fallback" : messageFallback, | |
"pretext" : messagePretext, | |
"mrkdwn_in" : ["pretext"], | |
"color" : postColor, | |
"fields" : fields | |
}] | |
var payload = { | |
"channel": postChannel, | |
"username": postUser, | |
"icon_emoji": postIcon, | |
"link_names": 1, | |
"attachments": attachments | |
}; | |
var options = { | |
'method': 'post', | |
'payload': JSON.stringify(payload) | |
}; | |
var response = UrlFetchApp.fetch(slackIncomingWebhookUrl, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment