-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.py
92 lines (84 loc) · 3.63 KB
/
event.py
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
###
# Written by Warren Brown
# Fresh-Pots Bot
# Version 1.0
##
# My Imports
import command
# Create class
class Event:
# Initialize object values
def __init__(self, bot):
# Set bot data into local data
self.bot = bot
self.command = command.Command(self)
# Define function to wait for event data
def wait_for_event(self):
# Listen to Slack Event API
events = self.bot.slack_client.rtm_read()
# If event has data
if events and len(events) > 0:
# For each event, call parse_event function
for event in events:
self.parse_event(event)
# Define parse event funtion
def parse_event(self, event):
# Check if event is text type
if event and 'text' in event:
# Get events channel to make suer its in datapool
chanevent = event['channel']
# Check for channel in datapool, if not then add channel to datapool
if chanevent not in self.bot.datapool:
self.channel_create(chanevent)
# Check for command flags
if '!fp' in event['text'] or '!freshpots' in event['text']:
if '!fp' in event['text'] and '!freshpots' in event['text']:
request='help me'
command='help'
string="help"
else:
# Check for what tag is in use
if '!fp' in event['text']:
fphandle='!fp'
if '!freshpots' in event['text']:
fphandle='!freshpots'
# Check if command is passed after flag
try:
event['text'].split(fphandle)[1]
except IndexError:
request='help me'
else:
request=event['text'].split(fphandle)[1].strip()
try:
command=request.split()[0]
except IndexError:
command='help'
else:
command=request.split()[0]
try:
string=request.split(command)[1].strip()
except IndexError:
string="help"
else:
string=request.split(command)[1].strip()
# Print command and string to terminal for testing
# print("Command: " + command + " | String : " + string)
# Call event handler
self.handle_event(event['user'], command.lower(), event['channel'],string)
# Define function to handle events
def handle_event(self, user, command, channel, request):
# Make sure we have command and channel
if command and channel:
# Get response from the command handler
response = self.command.handle_command(user,command,channel,request)
# Pass response to the Slack API
self.bot.slack_client.api_call("chat.postMessage", channel=channel, text=response, as_user=True)
# Define function to create channel and subsets in datapool
def channel_create(self, channel):
self.bot.datapool[channel]={}
self.bot.datapool[channel]['coffee']=[]
self.bot.datapool[channel]['user']=[]
self.bot.datapool[channel]['newpot']={'type':'','time':''}
self.bot.datapool[channel]['prevpot1']={'type':'','time':''}
self.bot.datapool[channel]['prevpot2']={'type':'','time':''}
self.bot.datapool[channel]['prevpot3']={'type':'','time':''}