Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hdknr/56aafe1a4e9754e02bd8b01ede7f665c to your computer and use it in GitHub Desktop.
Save hdknr/56aafe1a4e9754e02bd8b01ede7f665c to your computer and use it in GitHub Desktop.
GAS(google app script)で指定ラベルのGmailをslackに通知
var postUrl = "https://hooks.slack.com/services/xxxxxxxxxxxxxxxx";
var postChannel = "#chanel_name";
function myFunction() {
// 未読の指定ラベル を検索
var threads = GmailApp.search('is:unread label:to_me');
var count = threads.length;
Logger.log("対象件数:" + threads.length);
for(var i = 0; i < count; i++) {
var lastDate = threads[i].getLastMessageDate();
var datetime = lastDate.getFullYear() + "/" + (lastDate.getMonth() + 1) + "/" + lastDate.getDate()
+ " " + lastDate.getHours() + ":" + lastDate.getMinutes() + ":" + lastDate.getSeconds();
Logger.log(datetime + " 件名:[" + threads[i].getFirstMessageSubject() + "]");
 
 //slackに通知
sendHttpPost("件名:" + threads[i].getFirstMessageSubject() + " link:" + threads[i].getPermalink(), "GMAIL");
//既読にする。
//threads[i].markRead();
}
}
function sendHttpPost(message, username)
{
var jsonData =
{
"channel" : postChannel,
"username" : username,
"text" : message
};
var payload = JSON.stringify(jsonData);
var options =
{
"method" : "post",
"contentType" : "application/json",
"payload" : payload
};
UrlFetchApp.fetch(postUrl, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment