Skip to content

Commit 19c7bfa

Browse files
Harshg999sreenaths
authored andcommitted
[slack] load and instantiate sdk when config flag is enabled
1 parent b8b17a3 commit 19c7bfa

File tree

1 file changed

+19
-17
lines changed
  • desktop/core/src/desktop/lib/botserver

1 file changed

+19
-17
lines changed

desktop/core/src/desktop/lib/botserver/views.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,22 @@
2020
from pprint import pprint
2121

2222
from desktop import conf
23-
from django.shortcuts import render
2423
from django.http import HttpResponse
2524
from desktop.lib.django_util import login_notrequired, JsonResponse
2625
from desktop.lib.exceptions_renderable import PopupException
2726
from django.utils.translation import ugettext as _
2827
from django.views.decorators.csrf import csrf_exempt
2928

30-
from django.conf import settings
31-
from slack_sdk import WebClient
32-
3329
LOG = logging.getLogger(__name__)
3430

3531
SLACK_VERIFICATION_TOKEN = conf.SLACK.SLACK_VERIFICATION_TOKEN.get()
3632
SLACK_BOT_USER_TOKEN = conf.SLACK.SLACK_BOT_USER_TOKEN.get()
3733

38-
39-
slack_client = WebClient(token=SLACK_BOT_USER_TOKEN)
40-
appname = "hue_bot"
34+
slack_client, appname = None, None
35+
if conf.SLACK.IS_ENABLED.get():
36+
from slack_sdk import WebClient
37+
slack_client = WebClient(token=SLACK_BOT_USER_TOKEN)
38+
appname = "hue_bot"
4139

4240

4341
@login_notrequired
@@ -57,8 +55,8 @@ def slack_events(request):
5755
if 'event' in slack_message:
5856
event_message = slack_message['event']
5957
parse_events(event_message)
60-
except Exception as ex:
61-
raise PopupException(_("Response content is not valid JSON"), detail=ex)
58+
except ValueError as e:
59+
raise PopupException(_("Response content is not valid JSON"), detail=e)
6260

6361
return HttpResponse(status=200)
6462

@@ -67,18 +65,22 @@ def parse_events(event_message):
6765
user_id = event_message.get('user')
6866
text = event_message.get('text')
6967
channel = event_message.get('channel')
70-
BOT_ID = get_bot_id(appname)
68+
69+
BOT_ID = None
70+
if appname is not None:
71+
BOT_ID = get_bot_id(appname)
7172

7273
# ignore bot's own message
7374
if BOT_ID == user_id:
7475
return HttpResponse(status=200)
75-
76-
if 'hello hue' in text.lower():
77-
response = say_hi_user(channel, user_id)
78-
if response['ok']:
79-
return HttpResponse(status=200)
80-
else:
81-
raise PopupException(response["error"])
76+
77+
if slack_client is not None:
78+
if 'hello hue' in text.lower():
79+
response = say_hi_user(channel, user_id)
80+
if response['ok']:
81+
return HttpResponse(status=200)
82+
else:
83+
raise PopupException(response["error"])
8284

8385

8486
def say_hi_user(channel, user_id):

0 commit comments

Comments
 (0)