Skip to content

Commit 57e9756

Browse files
jhuleattracheldoshthatfiredevrachelmyersegilmorez
authored
New samples (#984)
Co-authored-by: Rachel Collins <[email protected]> Co-authored-by: rosariopf <[email protected]> Co-authored-by: Rosário Pereira Fernandes <[email protected]> Co-authored-by: Rachel Myers <[email protected]> Co-authored-by: Rachel Myers <[email protected]> Co-authored-by: egilmorez <[email protected]> Co-authored-by: Tyler Stark <[email protected]> Co-authored-by: Daniel Lee <[email protected]> Co-authored-by: Pavel Jbanov <[email protected]>
1 parent 6c927b0 commit 57e9756

File tree

148 files changed

+14755
-74
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+14755
-74
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
matrix:
1515
node-version:
16-
- 14.x
16+
- 16.x
1717
steps:
1818
- uses: actions/checkout@v1
1919
- uses: actions/setup-node@v1

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ lerna-debug.log
88
*~
99
service-account-credentials.json
1010
**/package-lock.json
11+
# keep the root package-lock so that tools are consistent
12+
!/package-lock.json
1113
**/ui-debug.log
1214
**/database-debug.log
1315
**/firebase-debug.log
1416
**/pubsub-debug.log
1517
**/tsconfig-compile.json
16-
yarn.lock
17-
pnpm-lock.yaml
18+
yarn.lock

2nd-gen/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TODO: readme

2nd-gen/alerts-to-discord/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Don't accidentally push Discord Webhook URL to prod
2+
functions/.env

2nd-gen/alerts-to-discord/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Quickstart: Send Firebase Alerts to Discord
2+
3+
This quickstart demonstrates how to trigger a function based on a Firebase Alert, and send information about the alert to a channel in a Discord server.
4+
5+
<img width="639" alt="Screen Shot 2022-04-29 at 11 12 12 AM" src="https://user-images.githubusercontent.com/3759507/165973290-2f6e6937-7c07-4006-b52d-813aa195e7cf.png">
6+
7+
## Set up and Deploy
8+
9+
### Discord Webhook URL
10+
11+
The sample uses [Discord Webhooks](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) to send alerts to a Discord channel. You'll need to create a Webhook and hook it up the function by [creating an environment variable](https://firebase.google.com/docs/functions/config-env#env-variables):
12+
13+
1. Follow the "MAKING A WEBHOOK" instructions in the [Discord docs](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks).
14+
1. Copy the Webhook URL
15+
1. Create a `.env` file in the `functions` directory
16+
1. Add the `DISCORD_WEBHOOK_URL` variable and set it to your Webhook URL:
17+
```bash
18+
DISCORD_WEBHOOK_URL="<your webhook url>"
19+
```
20+
21+
### Deploy
22+
Deploy functions using the Firebase CLI:
23+
24+
```bash
25+
$ firebase deploy
26+
```
27+
28+
## License
29+
30+
© Google, 2022. Licensed under an [Apache-2](../../../LICENSE) license.
31+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"functions": {
3+
"predeploy": [
4+
"npm --prefix \"$RESOURCE_DIR\" run lint"
5+
]
6+
}
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
es2017: true,
5+
node: true,
6+
},
7+
extends: [
8+
"eslint:recommended",
9+
"google",
10+
],
11+
rules: {
12+
quotes: ["error", "double"],
13+
},
14+
};
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
// [START v2import]
17+
const {
18+
onNewFatalIssuePublished,
19+
} = require("firebase-functions/v2/alerts/crashlytics");
20+
const logger = require("firebase-functions/logger");
21+
// [END v2import]
22+
23+
const fetch = require("node-fetch");
24+
25+
/**
26+
* Posts a message to Discord with Discord's Webhook API
27+
*
28+
* @param {string} botName - The bot username to display
29+
* @param {string} messageBody - The message to post (Discord MarkDown)
30+
*/
31+
async function postMessageToDiscord(botName, messageBody) {
32+
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
33+
if (!webhookUrl) {
34+
throw new Error(
35+
"No webhook URL found. Set the Discord Webhook URL before deploying. Learn more about Discord webhooks here: https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks",
36+
);
37+
}
38+
39+
return fetch(webhookUrl, {
40+
method: "POST",
41+
headers: {"Content-Type": "application/json"},
42+
body: JSON.stringify(
43+
// Here's what the Discord API supports in the payload:
44+
// https://discord.com/developers/docs/resources/webhook#execute-webhook-jsonform-params
45+
{
46+
content: messageBody,
47+
username: botName,
48+
},
49+
),
50+
});
51+
}
52+
53+
// [START v2Alerts]
54+
/**
55+
* function triggered by Crashlytics that publishes a message
56+
* to Discord whenever a new fatal issue occurs.
57+
*/
58+
// [START v2CrashlyticsAlertTrigger]
59+
exports.postmessagetodiscord = onNewFatalIssuePublished(async (event) => {
60+
// [END v2CrashlyticsAlertTrigger]
61+
// [START v2CrashlyticsEventPayload]
62+
// construct a helpful message to send to Discord
63+
const {id, title, subtitle, appVersion} = event.data.payload.issue;
64+
const message = `
65+
🚨 New fatal issue in version ${appVersion} 🚨
66+
67+
**${title}**
68+
69+
${subtitle}
70+
71+
id: \`${id}\`
72+
`;
73+
// [END v2CrashlyticsEventPayload]
74+
75+
try {
76+
// [START v2SendToDiscord]
77+
const response = await postMessageToDiscord("Crashlytics Bot", message);
78+
if (response.ok) {
79+
logger.info(
80+
`Posted fatal Crashlytics alert ${id} to Discord`,
81+
event.data.payload,
82+
);
83+
} else {
84+
throw new Error(response.error);
85+
}
86+
// [END v2SendToDiscord]
87+
} catch (error) {
88+
logger.error(
89+
`Unable to post fatal Crashlytics alert ${id} to Discord`,
90+
error,
91+
);
92+
}
93+
});
94+
// [END v2Alerts]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "alerts-to-discord",
3+
"description": "Send a message to Discord when an alert is received from Firebase",
4+
"scripts": {
5+
"lint": "eslint .",
6+
"serve": "firebase emulators:start --only functions",
7+
"shell": "firebase functions:shell",
8+
"start": "npm run shell",
9+
"deploy": "firebase deploy --only functions",
10+
"logs": "firebase functions:log",
11+
"compile": "cp ../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json"
12+
},
13+
"engines": {
14+
"node": "16"
15+
},
16+
"main": "index.js",
17+
"dependencies": {
18+
"firebase-admin": "^10.1.0",
19+
"firebase-functions": "^3.20.1",
20+
"node-fetch": "^2.6.7"
21+
},
22+
"devDependencies": {
23+
"@types/node": "^17.0.31",
24+
"eslint": "^8.9.0",
25+
"eslint-config-google": "^0.14.0",
26+
"firebase-functions-test": "^0.2.0"
27+
},
28+
"private": true
29+
}

2nd-gen/callable-functions/.gitignore

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
firebase-debug.log*
8+
firebase-debug.*.log*
9+
10+
# Firebase cache
11+
.firebase/
12+
13+
# Firebase config
14+
15+
# Uncomment this if you'd like others to create their own Firebase project.
16+
# For a team working on the same Firebase project(s), it is recommended to leave
17+
# it commented so all members can deploy to the same project(s) in .firebaserc.
18+
# .firebaserc
19+
20+
# Runtime data
21+
pids
22+
*.pid
23+
*.seed
24+
*.pid.lock
25+
26+
# Directory for instrumented libs generated by jscoverage/JSCover
27+
lib-cov
28+
29+
# Coverage directory used by tools like istanbul
30+
coverage
31+
32+
# nyc test coverage
33+
.nyc_output
34+
35+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
36+
.grunt
37+
38+
# Bower dependency directory (https://bower.io/)
39+
bower_components
40+
41+
# node-waf configuration
42+
.lock-wscript
43+
44+
# Compiled binary addons (http://nodejs.org/api/addons.html)
45+
build/Release
46+
47+
# Dependency directories
48+
node_modules/
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Optional REPL history
57+
.node_repl_history
58+
59+
# Output of 'npm pack'
60+
*.tgz
61+
62+
# Yarn Integrity file
63+
.yarn-integrity
64+
65+
# dotenv environment variables file
66+
.env

0 commit comments

Comments
 (0)