This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
forked from govau/australia-gov-au-static
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.py
43 lines (37 loc) · 1.34 KB
/
slack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import json
import requests
import argparse
import os
# TODO consider https://github.com/10mohi6/slack-webhook-python for more advanced webhooks
def _send_slack_data(slack_data):
print("Sending to slack", slack_data)
webhook_url = os.environ.get('SLACK_WEBHOOK')
response = requests.post(
webhook_url, data=json.dumps(slack_data),
headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--test', action='store_true')
parser.add_argument('--new_develop_deploy', nargs=1, type=str)
parser.add_argument('--new_preview', nargs=1, type=str)
args = parser.parse_args()
if args.test:
slack_data = {'text': "abcd"}
_send_slack_data(slack_data)
elif args.new_develop_deploy:
slack_data = {
'channel': args.new_develop_deploy[0],
'text': "A content update of australia.gov.au is now being tested - {}".format(os.environ.get('CIRCLE_PULL_REQUEST',''))}
_send_slack_data(slack_data)
elif args.new_preview:
slack_data = {
'text': "There's a new preview environment deployed at https://{}.apps.y.cld.gov.au/".format(args.new_preview[0])}
_send_slack_data(slack_data)
else:
print("No slack message chosen")