20
20
from pprint import pprint
21
21
22
22
from desktop import conf
23
- from django .shortcuts import render
24
23
from django .http import HttpResponse
25
24
from desktop .lib .django_util import login_notrequired , JsonResponse
26
25
from desktop .lib .exceptions_renderable import PopupException
27
26
from django .utils .translation import ugettext as _
28
27
from django .views .decorators .csrf import csrf_exempt
29
28
30
- from django .conf import settings
31
- from slack_sdk import WebClient
32
-
33
29
LOG = logging .getLogger (__name__ )
34
30
35
31
SLACK_VERIFICATION_TOKEN = conf .SLACK .SLACK_VERIFICATION_TOKEN .get ()
36
32
SLACK_BOT_USER_TOKEN = conf .SLACK .SLACK_BOT_USER_TOKEN .get ()
37
33
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"
41
39
42
40
43
41
@login_notrequired
@@ -57,8 +55,8 @@ def slack_events(request):
57
55
if 'event' in slack_message :
58
56
event_message = slack_message ['event' ]
59
57
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 )
62
60
63
61
return HttpResponse (status = 200 )
64
62
@@ -67,18 +65,22 @@ def parse_events(event_message):
67
65
user_id = event_message .get ('user' )
68
66
text = event_message .get ('text' )
69
67
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 )
71
72
72
73
# ignore bot's own message
73
74
if BOT_ID == user_id :
74
75
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" ])
82
84
83
85
84
86
def say_hi_user (channel , user_id ):
0 commit comments