|
| 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] |
0 commit comments