Skip to content

Commit c514d25

Browse files
Harshg999sreenaths
authored andcommitted
Refactor code
1 parent d1e1efb commit c514d25

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

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

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import logging
1919
import json
20+
from pprint import pprint
2021

2122
from django.shortcuts import render
2223
from django.http import HttpResponse
@@ -32,8 +33,17 @@
3233
SLACK_BOT_USER_TOKEN = getattr(settings, 'SLACK_BOT_USER_TOKEN', None)
3334

3435

35-
Client = WebClient(token=SLACK_BOT_USER_TOKEN)
36-
BOT_ID = Client.api_call('auth.test')['user_id']
36+
slack_client = WebClient(token=SLACK_BOT_USER_TOKEN)
37+
appname = "SQL Assistant"
38+
39+
def get_bot_id(botusername):
40+
response = slack_client.api_call("users.list")
41+
users = response["members"]
42+
for user in users:
43+
if 'name' in user and botusername in user.get('name') and not user.get('deleted'):
44+
return user.get('id')
45+
46+
BOT_ID = get_bot_id(appname)
3747

3848
@login_notrequired
3949
@csrf_exempt
@@ -43,32 +53,31 @@ def slack_events(request):
4353
if slack_message['token'] != SLACK_VERIFICATION_TOKEN:
4454
return HttpResponse(status=403)
4555

46-
# verification challenge
56+
# challenge verification
4757
if slack_message['type'] == 'url_verification':
4858
response_dict = {"challenge": slack_message.get('challenge')}
4959
return JsonResponse(response_dict, status=200)
5060

51-
52-
# Bot greeting when User says "hello hue"
5361
if 'event' in slack_message:
5462
event_message = slack_message['event']
55-
56-
user_id = event_message.get('user')
57-
58-
# ignore bot's own message
59-
if BOT_ID == user_id:
60-
return HttpResponse(status=200)
61-
62-
# process user's message
63-
text = event_message.get('text')
64-
channel = event_message.get('channel')
65-
66-
bot_text = 'Hi <@{}> :wave:'.format(user_id)
67-
if 'hello hue' in text.lower():
68-
Client.api_call(api_method='chat.postMessage', json={'channel': channel,'text': bot_text})
69-
return HttpResponse(status=200)
63+
parse_events(event_message)
7064

7165
return HttpResponse(status=200)
7266

67+
def parse_events(event_message):
68+
user_id = event_message.get('user')
69+
text = event_message.get('text')
70+
channel = event_message.get('channel')
7371

74-
72+
# ignore bot's own message
73+
if BOT_ID == user_id:
74+
return HttpResponse(status=200)
75+
76+
if 'hello hue' in text.lower():
77+
say_hi_user(channel, user_id)
78+
79+
def say_hi_user(channel, user_id):
80+
# App greets when user says "hello hue"
81+
bot_message = f'Hi <@{user_id}> :wave:'
82+
slack_client.api_call(api_method='chat.postMessage', json={'channel': channel, 'text': bot_message})
83+
return HttpResponse(status=200)

desktop/core/src/desktop/settings.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ def tracer():
826826
# Warning: Do not save your secrets here, instead export them as environment variables and read here
827827

828828
# use your keys
829-
SLACK_CLIENT_ID=''
830-
SLACK_CLIENT_SECRET=''
831-
SLACK_VERIFICATION_TOKEN=os.environ.get('SLACK_VERIFICATION_TOKEN')
832-
SLACK_BOT_USER_TOKEN=os.environ.get('SLACK_BOT_USER_TOKEN')
829+
SLACK_CLIENT_ID = ''
830+
SLACK_CLIENT_SECRET = ''
831+
SLACK_VERIFICATION_TOKEN = os.environ.get('SLACK_VERIFICATION_TOKEN')
832+
SLACK_BOT_USER_TOKEN = os.environ.get('SLACK_BOT_USER_TOKEN')

0 commit comments

Comments
 (0)