Skip to content

Commit fa67691

Browse files
committed
Fixing Private Channels Bug
1 parent ce023a6 commit fa67691

4 files changed

Lines changed: 55 additions & 19 deletions

File tree

hackathon/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def view_hackathon(request, hackathon_id):
360360
paginator = Paginator(teams, 3)
361361
page = request.GET.get('page')
362362
paged_teams = paginator.get_page(page)
363-
create_private_channel = (settings.SLACK_ENABLED and settings.SLACK_BOT_TOKEN)
363+
create_private_channel = (settings.SLACK_ENABLED and settings.SLACK_ADMIN_TOKEN)
364364

365365
context = {
366366
'hackathon': hackathon,

main/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@
185185
SLACK_WORKSPACE = os.environ.get('SLACK_WORKSPACE')
186186
SLACK_TEAM_ID = os.environ.get('SLACK_TEAM_ID')
187187
SLACK_BOT_TOKEN = os.environ.get('SLACK_BOT_TOKEN')
188+
SLACK_BOT_ID = os.environ.get('SLACK_BOT_ID')
189+
SLACK_ADMIN_TOKEN = os.environ.get('SLACK_ADMIN_TOKEN')
188190
INSTALLED_APPS += ['custom_slack_provider']
189191
SOCIALACCOUNT_PROVIDERS = {
190192
'custom_slack_provider': {

teams/helpers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from copy import deepcopy
22
from datetime import datetime
33
import math
4+
import requests
45

56
import pytz
67

@@ -231,3 +232,23 @@ def calculate_timezone_offset(timezone, timezone_offset):
231232
tz = pytz.timezone(timezone)
232233
offset = (datetime.now(tz).utcoffset().total_seconds()/60/60)
233234
return offset - timezone_offset
235+
236+
237+
def invite_users_to_slack_channel(endpoint, headers, params):
238+
response = requests.post(endpoint, params=params, headers=headers)
239+
if not response.status_code == 200:
240+
return {
241+
'ok': False,
242+
'error': ('An unexpected error occurred creating the '
243+
'Private Slack Channel.')
244+
}
245+
246+
response = response.json()
247+
if not response.get('ok'):
248+
return {
249+
'ok': False,
250+
'error': ('An error occurred creating the Private Slack Channel. '
251+
f'Error code: {response.get("error")}')
252+
}
253+
254+
return {'ok': True, 'response': response}

teams/views.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
choose_team_levels, find_all_combinations,
2121
distribute_participants_to_teams,
2222
create_teams_in_view, update_team_participants,
23-
calculate_timezone_offset)
23+
calculate_timezone_offset, invite_users_to_slack_channel)
2424
from teams.forms import HackProjectForm, EditTeamName
2525

2626
SLACK_CHANNEL_ENDPOINT = 'https://slack.com/api/conversations.create'
@@ -135,7 +135,8 @@ def view_team(request, team_id):
135135
and not request.user.is_participant(team.hackathon))
136136
rename_team_form = EditTeamName(instance=team)
137137
create_private_channel = (settings.SLACK_ENABLED is not None
138-
and settings.SLACK_BOT_TOKEN is not None)
138+
and settings.SLACK_BOT_TOKEN is not None
139+
and settings.SLACK_ADMIN_TOKEN is not None)
139140

140141
mentor_profile = None
141142
if team.mentor and settings.SLACK_WORKSPACE:
@@ -239,8 +240,8 @@ def create_private_channel(request, team_id):
239240
'for this team.'))
240241
return redirect(reverse('view_team', kwargs={'team_id': team_id}))
241242

242-
if (not (settings.SLACK_ENABLED or settings.SLACK_BOT_TOKEN
243-
or settings.SLACK_WORKSPACE) or slack_site_settings.communication_channel_type != 'slack_private_channel'):
243+
if (not (settings.SLACK_ENABLED or settings.SLACK_BOT_TOKEN or settings.SLACK_ADMIN_TOKEN
244+
or settings.SLACK_WORKSPACE) or slack_site_settings.communication_channel_type != 'slack_private_channel'):
244245
messages.error(request, 'This feature is currently not enabled.')
245246
return redirect(reverse('view_team', kwargs={'team_id': team_id}))
246247

@@ -253,7 +254,9 @@ def create_private_channel(request, team_id):
253254
'name': channel_name,
254255
'is_private': True,
255256
}
256-
headers = {'Authorization': f'Bearer {settings.SLACK_BOT_TOKEN}'}
257+
# Cannot use Bot Token to create a channel if workspace settings
258+
# specify only Admins and Owners can create channels
259+
headers = {'Authorization': f'Bearer {settings.SLACK_ADMIN_TOKEN}'}
257260
create_response = requests.get(SLACK_CHANNEL_ENDPOINT, params=params,
258261
headers=headers)
259262
if not create_response.status_code == 200:
@@ -291,24 +294,34 @@ def create_private_channel(request, team_id):
291294
# Add admins to channel for administration purposes
292295
for admin in slack_site_settings.slack_admins.all():
293296
users.append(admin.username)
297+
298+
# First need to add Slack Bot to then add users to channel
299+
invite_bot_params = {
300+
'channel': channel,
301+
'users': settings.SLACK_BOT_ID,
302+
}
303+
response = invite_users_to_slack_channel(
304+
endpoint=SLACK_CHANNEL_INVITE_ENDPOINT,
305+
headers=headers,
306+
params=invite_bot_params)
307+
308+
if not response['ok']:
309+
messages.error(request, response['error'])
310+
return redirect(reverse('view_team', kwargs={'team_id': team_id}))
294311

295-
invite_params = {
312+
invite_team_params = {
296313
'channel': channel,
297314
'users': ','.join([user.split('_')[0]
298315
for user in users if pattern.match(user)]),
299316
}
300-
response = requests.post(SLACK_CHANNEL_INVITE_ENDPOINT,
301-
params=invite_params, headers=headers)
302-
303-
if not response.status_code == 200:
304-
messages.error(request, ('An unexpected error occurred creating the '
305-
'Private Slack Channel.'))
306-
return redirect(reverse('view_team', kwargs={'team_id': team_id}))
307-
308-
response = response.json()
309-
if not response.get('ok'):
310-
messages.error(request, (f'An error occurred creating the Private Slack Channel. '
311-
f'Error code: {response.get("error")}'))
317+
headers = {'Authorization': f'Bearer {settings.SLACK_BOT_TOKEN}'}
318+
response = invite_users_to_slack_channel(
319+
endpoint=SLACK_CHANNEL_INVITE_ENDPOINT,
320+
headers=headers,
321+
params=invite_team_params)
322+
323+
if not response['ok']:
324+
messages.error(request, response['error'])
312325
return redirect(reverse('view_team', kwargs={'team_id': team_id}))
313326

314327
if len(users) < len(team.participants.all()):

0 commit comments

Comments
 (0)