Skip to content

Instantly share code, notes, and snippets.

@BEE-Plugin
Created Oct 23, 2020
Embed
What would you like to do?
BEE Plugin: How to send email from mentions in a comment
// Sample onComment callback for bee config
onComment: function (response) {
// Extract the comments from the response
var comments = Object.values(response.comments);
// Gets all words that start with '@', until a space
var handleRegex = /@[^\s]+/g
// Use reducer to extract the @ mentions from the content
function mentionsReducer(acc, comment) {
var handles = comment.content.match(handleRegex);
if (!handles) return acc;
return acc.concat(handles);
}
// Get all of the mentions
var mentions = comments.reduce(mentionsReducer, []);
// Define example users. This will need to be substituted in your own application
var userList = [{
username: "John Doe",
email_address: "johndoe@example.com",
handle: "@JohnDoe"
},
{
username: "Jane Doe",
email_address: "janedoe@example.com",
handle: "@JaneDoe"
}]
// Compare each value in handles array with the handle property of each example user
const activeUsers = userList.filter((user) => mentions.includes(user.handle));
// Function to send your email to mentioned user
function sendEmail(user) {
//send your email (ie through ajax call to your server)...
console.log('user email: ', user.email_address)
}
// Loop through your array and pull the respective email address for the handle matches
activeUsers.map(sendEmail)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment