-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlackBot.py
More file actions
144 lines (111 loc) · 3.91 KB
/
SlackBot.py
File metadata and controls
144 lines (111 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import time
from slackclient import SlackClient
# testbot's ID as an environment variable
BOT_ID = os.environ.get("BOT_ID")
# constants
AT_BOT = "<@" + BOT_ID + ">"
# command requests
REQUEST_COMMAND = ['create case', 'escalate case', 'update case']
# instantiate Slack & Twilio clients
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
history = []
def append_history(t):
if len(history) > 20:
del history[:1]
history.append(t)
return True
def is_user_in_list(u):
length = len(history)
for i in range(length):
if history[i]['user'] == u:
return i, True
def del_user(u):
length = len(history)
for i in range(length):
if history[i]['user'] == u:
del history[i]
def get_user_name(user_id):
api_call = slack_client.api_call("users.list")
if api_call.get('ok'):
users = api_call.get('members')
for user in users:
if 'name' in user and user.get('id') == user_id:
return user['name']
else:
return user_id
def past_seconds(user, t):
"""
t value is time in seconds
"""
epoch = int(time.time())
if is_user_in_list(user):
index = is_user_in_list(user)
print(int(history[index[0]]['time']) + 300, epoch)
if int(history[index[0]]['time']) + t < epoch:
return True
def submit_api_sf():
pass
def send_message(response, channel):
slack_client.api_call("chat.postMessage", channel=channel, text=response, as_user=True)
def handle_help(command, channel):
if AT_BOT and 'help' in command:
send_message("Hi, I am a bot created by Joon, I can help you create a SF case\n"
"just type <create case> to proceed", channel)
def handle_response(command, channel, user):
"""
Receives commands directed at the bot and determines if they
are valid commands. If so, then acts on the commands. If not,
returns back what it needs for clarification.
"""
if REQUEST_COMMAND[0] in command and not is_user_in_list(user):
append_history({'time': ts[0:10], '__SESSION__': "initiated", 'user': user})
user_name = get_user_name(user)
send_message(user_name + " create case request received.\n" \
"Enter data\n" \
"Customer: <account name> <Alt+Enter>\n" \
"Description: <UI not working>\n"
"To cancel, type <cancel>", channel)
elif is_user_in_list(user) and "customer:" in command and "description:" in command:
send_message("""add your API code now""", channel)
del_user(user)
elif is_user_in_list(user) and 'cancel' in command:
del_user(user)
send_message("""Your create case request canceled""", channel)
elif is_user_in_list(user) and not past_seconds(user, 300):
user_name = get_user_name(user)
send_message("@" + user_name + ", still proceed with creating case?\n"
"Customer: <account name> <Alt+Enter>\n"
"Description: <UI not working>\nTo cancel, type <cancel>", channel)
def not_bot(output):
if 'bot_id' not in output:
return True
def parse_slack_output(slack_rtm_output):
"""
The Slack Real Time Messaging API is an events firehose.
this parsing function returns None unless a message is
directed at the Bot, based on its ID.
"""
output_list = slack_rtm_output
if output_list and len(output_list) > 0:
print(output_list)
for output in output_list:
if 'text' in output and not_bot(output):
# return text after the @ mention, whitespace removed
return output['text'].strip().lower(), output['channel'], output['ts'], output['user']
return None, None, None, None
if __name__ == "__main__":
READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
try:
if slack_client.rtm_connect():
print("TestBot connected and running!")
while True:
command, channel, ts, user = parse_slack_output(slack_client.rtm_read())
if command and channel and ts and user:
handle_help(command, channel)
handle_response(command, channel, user)
time.sleep(READ_WEBSOCKET_DELAY)
else:
print("Connection failed. Invalid Slack token or bot ID?")
except ConnectionResetError:
slack_client.rtm_connect()