Last active
December 13, 2015 22:18
-
-
Save Mattieuga/4983322 to your computer and use it in GitHub Desktop.
Sample of using Parse with the SendGrid Cloud Module. This example will send an email when other users comment on a picture in Anypic (https://parse.com/anypic).
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
// Require the SendGrid Cloud Module | |
var sendgrid = require("sendgrid"); | |
sendgrid.initialize("you@parse.com", "your_password"); | |
// Run this Cloud Function every time a new Activity (such as a comment) | |
// is saved | |
Parse.Cloud.afterSave("Activity", function(request, response) { | |
// Check if the activity type is a comment | |
var activity = request.object; | |
if (activity.get("type") === "comment") { | |
// Get the owner of the photo commented on | |
var targetUser = activity.get("toUser"); | |
targetUser.fetch({ | |
success: function(targetUser) { | |
// Send an email if we have the user's email | |
if (targetUser.get("email") && targetUser.get("email").length > 0) { | |
sendgrid.sendEmail({ | |
to: targetUser.get("email"), | |
from: "anypic@parse.com", | |
subject: request.user.get("displayName") + " commented on your picture!", | |
text: "Check it out at anypic.org!" | |
}, { | |
success: function() {}, | |
error: function() {} | |
}); | |
} | |
}, | |
error: function() {} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment