Skip to content

Commit 0d7916e

Browse files
authored
Add slack integration for job board (chicagopython#420)
* adds slack integration for job board * adds logging for failed job posting * removes print statment * updates template and adds weekdays parameter to command * updates to django 2.2.24 for security issue * adds @ray's feedback * updates template to shorten description and add url for each post * refactors slack webhook code into a utility * refactors slack webhook code into a utility Co-authored-by: Aaron Elmquist <[email protected]>
1 parent 2aabe8e commit 0d7916e

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

chipy_org/apps/job_board/management/commands/post_approved_jobs_to_slack.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import datetime
2-
import json
2+
3+
34
import logging
45

56
import requests
67
from django.conf import settings
7-
from django.core.management.base import BaseCommand, CommandError
8+
from django.core.management.base import BaseCommand
89
from django.template import loader
910

1011
from chipy_org.apps.job_board.models import JobPost
12+
from chipy_org.libs.slack_utils import post_message_to_slack
1113

1214
logger = logging.getLogger(__name__)
1315

@@ -39,18 +41,12 @@ def handle(self, *args, **options):
3941
context = {"posts": posts}
4042
template = loader.get_template("job_board/slack_template.txt")
4143
msg = template.render(context)
42-
4344
job_post_key = settings.JOB_POST_KEY
44-
webhook_url = f"https://hooks.slack.com/services/{job_post_key}"
45-
slack_data = {"text": msg}
46-
47-
response = requests.post(
48-
webhook_url,
49-
data=json.dumps(slack_data),
50-
headers={"Content-Type": "application/json"},
51-
)
52-
53-
if response.status_code != 200:
54-
raise CommandError(
55-
f"Failed to post to slack job channel - status code: {response.status_code}."
45+
46+
try:
47+
post_message_to_slack(
48+
channel_key=job_post_key, channel_name="job board", message=msg
5649
)
50+
except requests.HTTPError as error:
51+
logger.error(error)
52+
print(error)
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
There {{ posts.count|pluralize:"is,are" }} currently *{{ posts.count }}* job{{ posts.count|pluralize }} on the Chipy job board.
22

33
{% for post in posts %}
4-
*{{ post.company_name }}* posted a *{{ post.position }}* opportunity.
5-
6-
*Description*:
7-
{{ post.description }}
8-
4+
*Company*: {{ post.company_name }}
5+
*Position*: {{ post.position }}
6+
*Description*: {{ post.description | truncatewords:25 }}
7+
*Link*: https://www.chipy.org{% url 'job-post-detail' pk=post.pk %}
98
{% endfor %}
10-
119
See all the open job posts on https://www.chipy.org/job-board/

chipy_org/libs/slack_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
3+
import requests
4+
5+
6+
def post_message_to_slack(channel_key: str, channel_name: str, message: str):
7+
"""
8+
This function is used to post messages from chipy.org to the chipy slack
9+
channel_key: secret key for slack channel
10+
channel_name: human readable description of the slack channel
11+
message: string formatted text to post to the slack channel
12+
"""
13+
14+
webhook_url = f"https://hooks.slack.com/services/{channel_key}"
15+
slack_data = {"text": message}
16+
17+
response = requests.post(
18+
webhook_url,
19+
data=json.dumps(slack_data),
20+
headers={"Content-Type": "application/json"},
21+
allow_redirects=False,
22+
)
23+
24+
if response.status_code != 200:
25+
raise requests.HTTPError(
26+
f"Failed to post message '{message[:25]}...' to slack channel '{channel_name}'. \
27+
Status code {response.status_code} != 200."
28+
)

0 commit comments

Comments
 (0)