Last active
March 2, 2024 13:00
-
-
Save rushimusmaximus/bcb847b89457b6936d50 to your computer and use it in GitHub Desktop.
Google Apps Script to post filtered Gmail messages to 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
/* | |
The intent of this script is for posting filtered Gmail messages to Slack. | |
This script could be used on its own with manually-marked messages, but it | |
is most useful it when combined with a Gmail filter. The script assumes that | |
target messages have had a specific label set on them and have been starred. | |
The Apps Script can then be set to run periodically. | |
2015/02 cmyers, rush | |
The MIT License (MIT) | |
Copyright (c) 2015 DF Studio | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
function checkForMessages() { | |
var slackChannel = 'THE_SLACK_CHANNEL'; | |
var webhookURL = 'THE_WEBHOOK_URL'; | |
var slackUsername = 'THE_USERNAME'; | |
var label = GmailApp.getUserLabelByName('THE_LABEL_ASSIGNED_WITH_THE_FILTER'); | |
var threads = label.getThreads(); | |
for (var t = 0; t < threads.length; t++){ | |
var thread = threads[t]; | |
if (thread.hasStarredMessages()){ | |
var messages = thread.getMessages(); | |
for (var m = 0; m < messages.length; m++){ | |
var message = messages[m]; | |
if (message.isStarred()){ | |
postToSlack(webhookURL, slackChannel, slackUsername, message.getPlainBody()); | |
message.unstar(); | |
} | |
} | |
} | |
} | |
} | |
function postToSlack(url, channel, username, text) { | |
var payload = { | |
'channel': channel, | |
'username': username, | |
'text': text | |
}; | |
var payloadJson = JSON.stringify(payload); | |
var options = { | |
'method': 'post', | |
'contentType': 'json', | |
'payload': payloadJson | |
}; | |
UrlFetchApp.fetch(url, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great script 👍
If needed PHP alternative here