1616
1717# [START v2import]
1818from firebase_functions import params
19- from firebase_functions .alerts import (
20- app_distribution_fn ,
21- crashlytics_fn ,
22- performance_fn ,
23- )
24-
19+ from firebase_functions .alerts import app_distribution_fn , crashlytics_fn , performance_fn
2520# [END v2import]
2621
2722import requests
2823
2924DISCORD_WEBHOOK_URL = params .SecretParam ("DISCORD_WEBHOOK_URL" )
3025
3126
32- def post_message_to_discord (
33- bot_name : str , message_body : str , webhook_url : str
34- ) -> requests .Response :
27+ def post_message_to_discord (bot_name : str , message_body : str ,
28+ webhook_url : str ) -> requests .Response :
3529 """Posts a message to Discord with Discord's Webhook API.
3630
3731 Params:
@@ -42,28 +36,24 @@ def post_message_to_discord(
4236 raise EnvironmentError (
4337 "No webhook URL found. Set the Discord Webhook URL before deploying. "
4438 "Learn more about Discord webhooks here: "
45- "https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks"
46- )
39+ "https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks" )
4740
4841 return requests .post (
4942 url = webhook_url ,
5043 json = {
5144 # Here's what the Discord API supports in the payload:
5245 # https://discord.com/developers/docs/resources/webhook#execute-webhook-jsonform-params
5346 "username" : bot_name ,
54- "content" : message_body ,
55- },
56- )
47+ "content" : message_body
48+ })
5749
5850
5951# [START v2Alerts]
6052# [START v2CrashlyticsAlertTrigger]
6153@crashlytics_fn .on_new_fatal_issue_published (secrets = ["DISCORD_WEBHOOK_URL" ])
62- def post_fatal_issue_to_discord (
63- event : crashlytics_fn .CrashlyticsNewFatalIssueEvent ,
64- ) -> None :
54+ def post_fatal_issue_to_discord (event : crashlytics_fn .CrashlyticsNewFatalIssueEvent ) -> None :
6555 """Publishes a message to Discord whenever a new Crashlytics fatal issue occurs."""
66- # [END v2CrashlyticsAlertTrigger]
56+ # [END v2CrashlyticsAlertTrigger]
6757 # [START v2CrashlyticsEventPayload]
6858 # Construct a helpful message to send to Discord.
6959 app_id = event .app_id
@@ -81,33 +71,22 @@ def post_fatal_issue_to_discord(
8171
8272 try :
8373 # [START v2SendToDiscord]
84- response = post_message_to_discord (
85- "Crashlytics Bot" , message , DISCORD_WEBHOOK_URL .value
86- )
74+ response = post_message_to_discord ("Crashlytics Bot" , message , DISCORD_WEBHOOK_URL .value )
8775 if response .ok :
88- print (
89- f"Posted fatal Crashlytics alert { issue .id } for { app_id } to Discord."
90- )
76+ print (f"Posted fatal Crashlytics alert { issue .id } for { app_id } to Discord." )
9177 pprint .pp (event .data .payload )
9278 else :
9379 response .raise_for_status ()
9480 # [END v2SendToDiscord]
9581 except (EnvironmentError , requests .HTTPError ) as error :
96- print (
97- f"Unable to post fatal Crashlytics alert { issue .id } for { app_id } to Discord." ,
98- error ,
99- )
82+ print (f"Unable to post fatal Crashlytics alert { issue .id } for { app_id } to Discord." , error )
10083
10184
10285# [START v2AppDistributionAlertTrigger]
103- @app_distribution_fn .on_new_tester_ios_device_published (
104- secrets = ["DISCORD_WEBHOOK_URL" ]
105- )
106- def post_new_udid_to_discord (
107- event : app_distribution_fn .NewTesterDeviceEvent ,
108- ) -> None :
86+ @app_distribution_fn .on_new_tester_ios_device_published (secrets = ["DISCORD_WEBHOOK_URL" ])
87+ def post_new_udid_to_discord (event : app_distribution_fn .NewTesterDeviceEvent ) -> None :
10988 """Publishes a message to Discord whenever someone registers a new iOS test device."""
110- # [END v2AppDistributionAlertTrigger]
89+ # [END v2AppDistributionAlertTrigger]
11190 # [START v2AppDistributionEventPayload]
11291 # Construct a helpful message to send to Discord.
11392 app_id = event .app_id
@@ -121,31 +100,24 @@ def post_new_udid_to_discord(
121100
122101 try :
123102 # [START v2SendNewTesterIosDeviceToDiscord]
124- response = post_message_to_discord (
125- "App Distro Bot" , message , DISCORD_WEBHOOK_URL .value
126- )
103+ response = post_message_to_discord ("App Distro Bot" , message , DISCORD_WEBHOOK_URL .value )
127104 if response .ok :
128- print (
129- f"Posted iOS device registration alert for { app_dist .tester_email } to Discord."
130- )
105+ print (f"Posted iOS device registration alert for { app_dist .tester_email } to Discord." )
131106 pprint .pp (event .data .payload )
132107 else :
133108 response .raise_for_status ()
134109 # [END v2SendNewTesterIosDeviceToDiscord]
135110 except (EnvironmentError , requests .HTTPError ) as error :
136111 print (
137112 f"Unable to post iOS device registration alert for { app_dist .tester_email } to Discord." ,
138- error ,
139- )
113+ error )
140114
141115
142116# [START v2PerformanceAlertTrigger]
143117@performance_fn .on_threshold_alert_published (secrets = ["DISCORD_WEBHOOK_URL" ])
144- def post_performance_alert_to_discord (
145- event : performance_fn .PerformanceThresholdAlertEvent ,
146- ) -> None :
118+ def post_performance_alert_to_discord (event : performance_fn .PerformanceThresholdAlertEvent ) -> None :
147119 """Publishes a message to Discord whenever a performance threshold alert is fired."""
148- # [END v2PerformanceAlertTrigger]
120+ # [END v2PerformanceAlertTrigger]
149121 # [START v2PerformanceEventPayload]
150122 # Construct a helpful message to send to Discord.
151123 app_id = event .app_id
@@ -167,22 +139,14 @@ def post_performance_alert_to_discord(
167139
168140 try :
169141 # [START v2SendPerformanceAlertToDiscord]
170- response = post_message_to_discord (
171- "App Performance Bot" , message , DISCORD_WEBHOOK_URL .value
172- )
142+ response = post_message_to_discord ("App Performance Bot" , message ,
143+ DISCORD_WEBHOOK_URL .value )
173144 if response .ok :
174- print (
175- f"Posted Firebase Performance alert { perf .event_name } to Discord."
176- )
145+ print (f"Posted Firebase Performance alert { perf .event_name } to Discord." )
177146 pprint .pp (event .data .payload )
178147 else :
179148 response .raise_for_status ()
180149 # [END v2SendPerformanceAlertToDiscord]
181150 except (EnvironmentError , requests .HTTPError ) as error :
182- print (
183- f"Unable to post Firebase Performance alert { perf .event_name } to Discord." ,
184- error ,
185- )
186-
187-
151+ print (f"Unable to post Firebase Performance alert { perf .event_name } to Discord." , error )
188152# [END v2Alerts]
0 commit comments