Skip to content

Commit

Permalink
Merge pull request #814 from ONEARMY/master
Browse files Browse the repository at this point in the history
add slack-hooks integration functions
  • Loading branch information
chrismclarke authored Dec 4, 2019
2 parents a201620 + 90ffda8 commit 5585f21
Show file tree
Hide file tree
Showing 5 changed files with 518 additions and 139 deletions.
4 changes: 3 additions & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
"google-auth-library": "^2.0.1",
"googleapis": "^35.0.0",
"log-update": "^3.2.0",
"sharp": "^0.22.1"
"request": "^2.88.0",
"sharp": "^0.23.3"
},
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/cors": "^2.8.5",
"@types/dateformat": "^3.0.0",
"@types/fs-extra": "^5.0.5",
"@types/request": "^2.48.3",
"@types/sharp": "^0.22.1",
"concurrently": "^4.1.1",
"tslint": "^5.12.0",
Expand Down
86 changes: 86 additions & 0 deletions functions/src/Integrations/firebase-slack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { CONFIG } from '../config/config'
import * as functions from 'firebase-functions'
import * as request from 'request'

const project = CONFIG.service.project_id
// add prefix to dev site
const prefix = project === 'precious-plastics-v4-dev' ? '[DEV] ' : ''
const SLACK_WEBHOOK_URL = CONFIG.integrations.slack_webhook

export const notifyNewPin = functions.firestore
.document('v2_mappins/{pinId}')
.onCreate((snapshot, context) => {
const info = snapshot.data()
const id = info._id
const type = info.type
const loc = info.location
// console.log(info);
request.post(
SLACK_WEBHOOK_URL,
{
json: {
text: `📍 *New ${type}* pin from ${id}. \n Location here (soon our own map) https://google.com/maps/@${loc.lat},${loc.lng},14z`,
},
},
(err, res) => {
if (err) {
console.error(err)
return
} else {
return res
}
},
)
})
export const notifyNewHowTo = functions.firestore
.document('v2_howtos/{id}')
.onCreate((snapshot, context) => {
const info = snapshot.data()
const user = info._createdBy
const title = info.title
const slug = info.slug
// console.log(info);
request.post(
SLACK_WEBHOOK_URL,
{
json: {
text: `📓 Yeah! New How To "* ${title} *" by ${user}
check it out: https://community.preciousplastic.com/how-to/${slug}`,
},
},
(err, res) => {
if (err) {
console.error(err)
return
} else {
return res
}
},
)
})
export const notifyNewEvent = functions.firestore
.document('v2_events/{id}')
.onCreate((snapshot, context) => {
const info = snapshot.data()
const user = info._createdBy
const url = info.url
const location = info.location.country
console.info(info)
request.post(
SLACK_WEBHOOK_URL,
{
json: {
text: `📅 Jeej new event in *${location}* by ${user} posted here:
${url}`,
},
},
(err, res) => {
if (err) {
console.error(err)
} else {
console.log('post success')
return res
}
},
)
})
5 changes: 5 additions & 0 deletions functions/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const c = config() as configVars
// strip additional character escapes (\\n -> \n)
c.service.private_key = c.service.private_key.replace(/\\n/g, '\n')

export const CONFIG = c
export const SERVICE_ACCOUNT_CONFIG = c.service
export const ANALYTICS_CONFIG = c.analytics
/************** Interfaces ************** */
Expand All @@ -27,10 +28,14 @@ interface IAnalytics {
tracking_code: string
view_id: string
}
interface IIntergrations {
slack_webhook: string
}

interface configVars {
service: IServiceAccount
analytics: IAnalytics
integrations: IIntergrations
}

// if passing complex config variables, may want to
Expand Down
4 changes: 4 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { api } from './exports/api'
import { weeklyTasks, dailyTasks } from './exports/tasks'
import { DH_Exports } from './DaveHakkensNL'
import * as Integrations from './Integrations/firebase-slack'

// the following endpoints are exposed for use by various triggers
// see individual files for more informaiton
Expand All @@ -10,3 +11,6 @@ exports.dailyTasks = dailyTasks
exports.DHSite_getUser = DH_Exports.DHSite_getUser
exports.DHSite_migrateAvatar = DH_Exports.DHSite_migrateAvatar
exports.DHSite_login = DH_Exports.DHSite_login
exports.notifyNewPin = Integrations.notifyNewPin
exports.notifyNewHowTo = Integrations.notifyNewHowTo
exports.notifyNewEvent = Integrations.notifyNewEvent
Loading

0 comments on commit 5585f21

Please sign in to comment.