Skip to content

Instantly share code, notes, and snippets.

@markgoho
Created May 26, 2019 01:20
Show Gist options
  • Save markgoho/9c54af9925b50a43547df21d4b5ae0f8 to your computer and use it in GitHub Desktop.
Save markgoho/9c54af9925b50a43547df21d4b5ae0f8 to your computer and use it in GitHub Desktop.
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as request from 'request';
admin.initializeApp();
// when a new user is registered
export const createUser = functions.firestore
.document('users/{userId}')
.onCreate(async (snap, _) => {
const { firstName, lastName } = snap.data();
const slackWebhook = 'OC_slack1';
const message = `user ${firstName} ${lastName} just registered!`;
try {
await request.post(slackWebhook, { json: { text: message } });
return res.status(200).send('slack message sent successfully');
} catch (e) {
return res.status(500).send('error occured whens ending slack message');
}
});
// when a new activity is created
export const createActivity = functions.firestore
.document('teamActivity/{Id}')
.onCreate(async (snap, _) => {
const { firstName, lastName, activity, description, link, points } = snap.data();
const slackWebhook = 'OC_slack2';
const message = `${firstName} ${lastName} just added the activity ${activity} for ${points} points with the description "${description}." Here's the link ${link}.`;
try {
await request.post(slackWebhook, { json: { text: message } });
return res.status(200).send('slack message sent successfully');
} catch (e) {
return res.status(500).semd('error occured whens ending slack message');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment