16
16
17
17
# [START v2import]
18
18
from 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
25
20
# [END v2import]
26
21
27
22
import requests
28
23
29
24
DISCORD_WEBHOOK_URL = params .SecretParam ("DISCORD_WEBHOOK_URL" )
30
25
31
26
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 :
35
29
"""Posts a message to Discord with Discord's Webhook API.
36
30
37
31
Params:
@@ -42,28 +36,24 @@ def post_message_to_discord(
42
36
raise EnvironmentError (
43
37
"No webhook URL found. Set the Discord Webhook URL before deploying. "
44
38
"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" )
47
40
48
41
return requests .post (
49
42
url = webhook_url ,
50
43
json = {
51
44
# Here's what the Discord API supports in the payload:
52
45
# https://discord.com/developers/docs/resources/webhook#execute-webhook-jsonform-params
53
46
"username" : bot_name ,
54
- "content" : message_body ,
55
- },
56
- )
47
+ "content" : message_body
48
+ })
57
49
58
50
59
51
# [START v2Alerts]
60
52
# [START v2CrashlyticsAlertTrigger]
61
53
@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 :
65
55
"""Publishes a message to Discord whenever a new Crashlytics fatal issue occurs."""
66
- # [END v2CrashlyticsAlertTrigger]
56
+ # [END v2CrashlyticsAlertTrigger]
67
57
# [START v2CrashlyticsEventPayload]
68
58
# Construct a helpful message to send to Discord.
69
59
app_id = event .app_id
@@ -81,33 +71,22 @@ def post_fatal_issue_to_discord(
81
71
82
72
try :
83
73
# [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 )
87
75
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." )
91
77
pprint .pp (event .data .payload )
92
78
else :
93
79
response .raise_for_status ()
94
80
# [END v2SendToDiscord]
95
81
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 )
100
83
101
84
102
85
# [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 :
109
88
"""Publishes a message to Discord whenever someone registers a new iOS test device."""
110
- # [END v2AppDistributionAlertTrigger]
89
+ # [END v2AppDistributionAlertTrigger]
111
90
# [START v2AppDistributionEventPayload]
112
91
# Construct a helpful message to send to Discord.
113
92
app_id = event .app_id
@@ -121,31 +100,24 @@ def post_new_udid_to_discord(
121
100
122
101
try :
123
102
# [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 )
127
104
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." )
131
106
pprint .pp (event .data .payload )
132
107
else :
133
108
response .raise_for_status ()
134
109
# [END v2SendNewTesterIosDeviceToDiscord]
135
110
except (EnvironmentError , requests .HTTPError ) as error :
136
111
print (
137
112
f"Unable to post iOS device registration alert for { app_dist .tester_email } to Discord." ,
138
- error ,
139
- )
113
+ error )
140
114
141
115
142
116
# [START v2PerformanceAlertTrigger]
143
117
@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 :
147
119
"""Publishes a message to Discord whenever a performance threshold alert is fired."""
148
- # [END v2PerformanceAlertTrigger]
120
+ # [END v2PerformanceAlertTrigger]
149
121
# [START v2PerformanceEventPayload]
150
122
# Construct a helpful message to send to Discord.
151
123
app_id = event .app_id
@@ -167,22 +139,14 @@ def post_performance_alert_to_discord(
167
139
168
140
try :
169
141
# [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 )
173
144
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." )
177
146
pprint .pp (event .data .payload )
178
147
else :
179
148
response .raise_for_status ()
180
149
# [END v2SendPerformanceAlertToDiscord]
181
150
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 )
188
152
# [END v2Alerts]
0 commit comments