Created
May 26, 2019 01:20
-
-
Save markgoho/9c54af9925b50a43547df21d4b5ae0f8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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