Skip to content

Instantly share code, notes, and snippets.

@horimislime
Last active September 11, 2015 02:27
Show Gist options
  • Save horimislime/fa23218d3457bc944ded to your computer and use it in GitHub Desktop.
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
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