8000 Error checking on billing sample code for Slack (#4248) · lak2142/python-docs-samples@86b8103 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86b8103

Browse files
markmirchleahecole
andauthored
Error checking on billing sample code for Slack (GoogleCloudPlatform#4248)
* Error checking on billing sample code for Slack * Added clarifying comments * Reviewer cleanup on Slack billing function * Accidentally included a change from a different issue Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
1 parent 7a169f2 commit 86b8103

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

functions/billing/main.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
# [START functions_billing_limit]
1616
# [START functions_billing_limit_appengine]
1717
# [START functions_billing_stop]
18+
# [START functions_billing_slack]
1819
import base64
1920
import json
2021
import os
2122
# [END functions_billing_stop]
2223
# [END functions_billing_limit]
2324
# [END functions_billing_limit_appengine]
25+
# [END functions_billing_slack]
2426

2527
# [START functions_billing_limit]
2628
# [START functions_billing_limit_appengine]
@@ -32,6 +34,7 @@
3234

3335
# [START functions_billing_slack]
3436
import slack
37+
from slack.errors import SlackApiError
3538
# [END functions_billing_slack]
3639

3740
# [START functions_billing_limit]
@@ -42,26 +45,42 @@
4245
# [END functions_billing_limit]
4346

4447
# [START functions_billing_slack]
45-
4648
# See https://api.slack.com/docs/token-types#bot for more info
4749
BOT_ACCESS_TOKEN = 'xxxx-111111111111-abcdefghidklmnopq'
48-
49-
CHANNEL_ID = 'C0XXXXXX'
50+
CHANNEL = 'C0XXXXXX'
5051

5152
slack_client = slack.WebClient(token=BOT_ACCESS_TOKEN)
5253

5354

5455
def notify_slack(data, context):
5556
pubsub_message = data
5657

57-
notification_attrs = json.dumps(pubsub_message['attributes'])
58-
notification_data = base64.b64decode(data['data']).decode('utf-8')
59-
budget_notification_text = f'{notification_attrs}, {notification_data}'
58+
# For more information, see
59+
# https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification_format
60+
try:
61+
notification_attr = json.dumps(pubsub_message['attributes'])
62+
except KeyError:
63+
notification_attr = "No attributes passed in"
6064

61-
slack_client.api_call(
62-
'chat.postMessage',
63-
channel=CHANNEL_ID,
64-
text=budget_notification_text)
65+
try:
66+
notification_data = base64.b64decode(data['data']).decode('utf-8')
67+
except KeyError:
68+
notification_data = "No data passed in"
69+
70+
# This is just a quick dump of the budget data (or an empty string)
71+
# You can modify and format the message to meet your needs
72+
budget_notification_text = f'{notification_attr}, {notification_data}'
73+
74+
try:
75+
slack_client.api_call(
76+
'chat.postMessage',
77+
json={
78+
'channel': CHANNEL,
79+
'text' : budget_notification_text
80+
}
81+
)
82+
except SlackApiError:
83+
print('Error posting to Slack')
6584
# [END functions_billing_slack]
6685

6786

0 commit comments

Comments
 (0)
0