Created
April 5, 2017 05:11
-
-
Save DennisAlund/a7e2716ee0bff3fb0e75df145cb4a659 to your computer and use it in GitHub Desktop.
Firebase cloud function for Slack button actions in medium article https://medium.com/evenbit/151c1c98641d
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
| async function sendPingPongScore(pingId: string) { | |
| const payload = {attachments: []}; | |
| const ping = (await admin.database().ref("ping").child(pingId).once("value")).val(); | |
| const snap = await admin.database().ref("pong").orderByChild("ping").equalTo(pingId).limitToFirst(3).once("value"); | |
| const medalColors = ["#C98910", "#A8A8A8", "#965A38"]; | |
| let counter = 0; | |
| snap.forEach((childSnap) => { | |
| const pong = childSnap.val() as Pong; | |
| const color = medalColors[counter]; | |
| counter += 1; | |
| payload.attachments.push({ | |
| "fallback": "No ping pong scores for you today.", | |
| "color": color, | |
| "title": `#${counter} <@${pong.user_id}|${pong.user_name}>`, | |
| "fields": [ | |
| { | |
| "title": "Pong at", | |
| "value": (new Date(pong.ponged_at)).toString(), | |
| "short": true | |
| }, | |
| { | |
| "title": "Response time", | |
| "value": `${pong.ping_pong_time/1000} seconds`, | |
| "short": true | |
| } | |
| ] | |
| }); | |
| }); | |
| const installationRef = admin.database().ref("installations").child(ping.team); | |
| const installation = (await installationRef.once("value")).val(); | |
| return rp({ | |
| uri: installation.webhook.url, | |
| method: "POST", | |
| json: true, | |
| body: payload | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment