Last active
September 11, 2015 02:27
-
-
Save horimislime/fa23218d3457bc944ded to your computer and use it in GitHub Desktop.
GAS script that find specific email from Gmail, extract body from it, then send it to a slack channel
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 token = "..."; | |
var channelId = "..."; | |
function run() { | |
var threads = GmailApp.search('subject:"send-to-slack" label:inbox', 0, 500); | |
if (threads.length == 0) { | |
return; | |
} else if (threads.length > 1) { | |
Logger.log("Illegal number of threads."); | |
return; | |
} | |
var thread = threads[0]; | |
if (thread.getMessageCount() != 1) { | |
Logger.log("A thread should have one message."); | |
return; | |
} | |
sendToSlack(thread.getMessages()[0].getPlainBody()); | |
thread.moveToTrash(); | |
} | |
function sendToSlack(message) { | |
var params = [ | |
"token=" + token, | |
"text=" + message, | |
"channel=" + channelId, | |
"as_user=true" | |
]; | |
var resp = UrlFetchApp.fetch("https://slack.com/api/chat.postMessage?" + params.join("&")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment