Skip to content

Commit 87650c7

Browse files
committed
update: 후원사 슬랙 알림 추가
1 parent 9b62a13 commit 87650c7

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

pyconkr/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@
147147
"",
148148
"Slack 알림 전송에 사용할 Secret",
149149
),
150-
"SPONSOR_NOTI_CHANNEL": (
150+
"SLACK_SPONSOR_NOTI_WEBHOOK_URL": (
151151
"",
152-
"후원사 변동사항에 대한 알림을 보낼 채널",
152+
"후원사 관련 사항 알림을 위한 SLACK WEBHOOK URL"
153153
),
154154
}
155155

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ Pillow==9.4.0
1515
django-constance==2.9.1
1616
django-picklefield==3.1
1717
drf-spectacular==0.25.1
18-
django-cors-headers==3.14.0
18+
django-cors-headers==3.14.0
19+
requests==2.28.2

sponsor/slack.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import requests
2+
from requests import RequestException
3+
from constance import config
4+
5+
6+
def send_new_sponsor_notification(id: int, sponsor_name: str):
7+
block = _get_base_block()
8+
block.extend(_get_sponsor_sections(id, sponsor_name))
9+
10+
try:
11+
response = requests.post(
12+
url=config.SLACK_SPONSOR_NOTI_WEBHOOK_URL,
13+
headers={"Content-Type": "application/json"},
14+
json={"blocks": block},
15+
)
16+
except RequestException as e:
17+
raise RuntimeError("Slack 호출 중 오류 발생")
18+
19+
if response.status_code != 200:
20+
raise RuntimeError("Slack에서 오류 응답: {}".format(response.content))
21+
22+
23+
def _get_base_block() -> list:
24+
# create block var
25+
block = list()
26+
27+
# add Title
28+
block.append(
29+
{"type": "section", "text": {"type": "mrkdwn", "text": "새로운 알림이 있습니다."}}
30+
)
31+
32+
# add title
33+
block.append(
34+
{
35+
"type": "header",
36+
"text": {"type": "plain_text", "text": "새로운 후원사 신청이 있습니다. :-)"},
37+
}
38+
)
39+
40+
return block
41+
42+
43+
def _get_sponsor_sections(id: int, sponsor_name: str) -> list:
44+
section_blocks = list()
45+
46+
# add sponsor section
47+
section_blocks.append(
48+
{
49+
"type": "section",
50+
"text": {
51+
"type": "mrkdwn",
52+
"text": "{}에서 후원사를 신청했습니다.".format(sponsor_name),
53+
},
54+
}
55+
)
56+
57+
# add hyperlink button
58+
section_blocks.append(
59+
{
60+
"type": "actions",
61+
"elements": [
62+
{
63+
"type": "button",
64+
"text": {"type": "plain_text", "text": "내용 보기", "emoji": True},
65+
"value": "click_me",
66+
"url": "https://api.pycon.kr/admin/sponsor/sponsor/{}/change/".format(
67+
id
68+
),
69+
}
70+
],
71+
}
72+
)
73+
74+
return section_blocks

sponsor/viewsets.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
SponsorRemainingAccountSerializer,
1414
SponsorSerializer,
1515
)
16+
from sponsor.slack import send_new_sponsor_notification
1617
from sponsor.validators import SponsorValidater
1718

1819

@@ -38,6 +39,12 @@ def create(self, request, *args, **kwargs):
3839

3940
new_sponsor = serializer.save()
4041

42+
# slack 알림을 실패하더라도 transaction 전체를 롤백하지는 않아야 함
43+
try:
44+
send_new_sponsor_notification(new_sponsor.id, new_sponsor.name)
45+
except RuntimeError as e:
46+
print(e)
47+
4148
return Response(serializer.data)
4249

4350
def retrieve(self, request, *args, **kwargs):

0 commit comments

Comments
 (0)