Skip to content

Instantly share code, notes, and snippets.

@psapir
Created October 28, 2019 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psapir/732b8175b50673b8cc55a81dc0c70f14 to your computer and use it in GitHub Desktop.
Save psapir/732b8175b50673b8cc55a81dc0c70f14 to your computer and use it in GitHub Desktop.
// Change these settings with your Webhook URL and the options for your notification body
var webhookURL = "[YOUR SLACK WEBHOOK URL]";
var slackNotificationBody = {
"text": "",
"attachments": [
{
"text": "",
"color":"#9C1A22"
}
]
};
function SFNotifications() {
// Search MC Notification Codes
var threads = GmailApp.search("subject:Verify your identity in Salesforce Marketing Cloud newer_than:1d");
for( var i = 0; i < threads.length; ++i ) {
// get last message in thread
var messagesCount = threads[i].getMessageCount();
var lastMessage = threads[i].getMessages()[messagesCount-1];
var body = lastMessage.getPlainBody();
var start = body.indexOf("prompted.")+9;
var end = body.indexOf("If you");
var activationCode = body.substring(start,end).trim();
var account = lastMessage.getFrom();
slackNotificationBody.text = activationCode;
slackNotificationBody.attachments[0].text = "New MC Code for: " + account;
SendNotification();
// mark message as unread
threads[i].markUnread();
// move to trash
threads[i].moveToTrash();
}
// Search MC Nofitication Codes
var threads = GmailApp.search("subject:instructions to activate newer_than:1d");
for( var i = 0; i < threads.length; ++i ) {
// get last message in thread
var messagesCount = threads[i].getMessageCount();
var lastMessage = threads[i].getMessages()[messagesCount-1];
var body = lastMessage.getPlainBody();
var start = body.indexOf("browser:")+8;
var end = body.indexOf("This");
var activationCode = body.substring(start,end).trim();
var account = lastMessage.getFrom();
slackNotificationBody.text = activationCode;
slackNotificationBody.attachments[0].text = "New MC Code for: " + account;
SendNotification();
// mark message as unread
threads[i].markUnread();
// move to trash
threads[i].moveToTrash();
}
// Search Salesforce Notification Codes
var threads = GmailApp.search("subject:Verify your identity in Salesforce from:noreply@salesforce.com newer_than:1d");
var threads2 = GmailApp.search("subject:Verify your identity in Your Developer Edition from:info@salesforcedevs.com newer_than:1d");
var threads3 = GmailApp.search("subject:Sandbox: Verify your identity in Salesforce from:noreply@salesforce.com newer_than:1d");
var threads4 = threads.concat(threads2).concat(threads3);
for( var i = 0; i < threads4.length; ++i ) {
// get last message in thread
var messagesCount = threads[i].getMessageCount();
var lastMessage = threads[i].getMessages()[messagesCount-1];
var body = lastMessage.getPlainBody();
var start = body.indexOf("Verification Code:")+18;
var end = start+6;
var activationCode = body.substring(start,end).trim();
slackNotificationBody.text = activationCode;
slackNotificationBody.attachments[0].text = "cloud: New Salesforce Verification Code";
SendNotification();
// mark message as unread
threads[i].markUnread();
// move to trash
threads[i].moveToTrash();
}
}
function SendNotification() {
var payload = JSON.stringify(slackNotificationBody);
var headers = {
"Accept":"application/json",
"Content-Type":"application/json",
"Authorization":"Basic _authcode_"
};
var options = { "method":"POST",
"headers": headers,
"payload" : payload
};
var response = UrlFetchApp.fetch(webhookURL, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment