Browse Source

Botserver update with Hue App greeting

Harshg999 4 years ago
parent
commit
d17be81a36

+ 1 - 1
desktop/core/src/desktop/conf.py

@@ -718,7 +718,7 @@ SLACK = ConfigSection(
     IS_ENABLED=Config(
       key='is_enabled',
       help=_('Turns on Slack application API endpoints'),
-      default=False,
+      default=True,
       type=coerce_bool),
   )
 )

+ 1 - 1
desktop/core/src/desktop/lib/botserver/urls.py

@@ -19,5 +19,5 @@ from django.conf.urls import url
 from desktop.lib.botserver import views
 
 urlpatterns = [
-  url(r'^events/', views.home, name='home')
+  url(r'^events/', views.slack_events, name='slack_events')
 ]

+ 52 - 3
desktop/core/src/desktop/lib/botserver/views.py

@@ -15,11 +15,60 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import logging
+import json
+
 from django.shortcuts import render
 from django.http import HttpResponse
-from desktop.lib.django_util import login_notrequired
+from desktop.lib.django_util import login_notrequired, JsonResponse
+from django.views.decorators.csrf import csrf_exempt
+
+from django.conf import settings
+from slack_sdk import WebClient
+
+LOG = logging.getLogger(__name__)
+
+SLACK_VERIFICATION_TOKEN = getattr(settings, 'SLACK_VERIFICATION_TOKEN', None)
+SLACK_BOT_USER_TOKEN = getattr(settings, 'SLACK_BOT_USER_TOKEN', None)
 
 
+Client = WebClient(token=SLACK_BOT_USER_TOKEN)
+BOT_ID = Client.api_call('auth.test')['user_id']
+
 @login_notrequired
-def home(request):
-  return HttpResponse("Hello World")
+@csrf_exempt
+def slack_events(request):
+  slack_message = json.loads(request.body.decode('utf-8'))
+
+  if slack_message['token'] != SLACK_VERIFICATION_TOKEN:
+    return HttpResponse(status=403)
+
+  # verification challenge
+  if slack_message['type'] == 'url_verification':
+    response_dict = {"challenge": slack_message.get('challenge')}
+    return JsonResponse(response_dict, status=200)
+  
+
+  # Bot greeting when User says "hello hue"
+  if 'event' in slack_message:
+    event_message = slack_message['event']
+   
+    user_id = event_message.get('user')
+
+    # ignore bot's own message
+    if BOT_ID == user_id:
+      return HttpResponse(status=200) 
+            
+    # process user's message              
+    text = event_message.get('text')                     
+    channel = event_message.get('channel')
+
+    bot_text = 'Hi <@{}> :wave:'.format(user_id)
+    if 'hello hue' in text.lower():
+      Client.api_call(api_method='chat.postMessage', json={'channel': channel,'text': bot_text})
+      return HttpResponse(status=200)
+
+  return HttpResponse(status=200)
+
+
+  

+ 11 - 0
desktop/core/src/desktop/settings.py

@@ -819,3 +819,14 @@ try:
 except ValueError:
   from desktop.monkey_patches import monkey_patch_md5
   monkey_patch_md5(MODULES_TO_PATCH)
+
+################################################################
+# SLACK API Configurations
+################################################################
+# Warning: Do not save your secrets here, instead export them as environment variables and read here
+
+# use your keys 
+SLACK_CLIENT_ID=''
+SLACK_CLIENT_SECRET=''
+SLACK_VERIFICATION_TOKEN=os.environ.get('SLACK_VERIFICATION_TOKEN')
+SLACK_BOT_USER_TOKEN=os.environ.get('SLACK_BOT_USER_TOKEN')