-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
86 lines (73 loc) · 2.57 KB
/
bot.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
import slacker
import websocket
import _thread
import schedule
import time
import json
import settings
import events
import utils.log
import utils.api
class Bot(object):
def __init__(self):
self.connected = False
self.websocket = None
self.api = slacker.Slacker(settings.API_TOKEN)
self.webapi = utils.api.WebAPI()
self.event = events.EventHandler()
self.console = utils.log.console()
self.log = utils.log.file()
self.console.info('Start.')
def connect(self):
if self.connected:
return
while True:
try:
reply = self.api.rtm.start().body
self.get_websocket(reply["url"])
self.console.info("Connected to Slack.")
self.connected = True
time.sleep(1)
return
except Exception as e:
self.console.exception("Failed to connect: %s", e)
if str(e) == "not_authed":
self.console.exception("Wrong API Token.")
return
time.sleep(5)
def get_websocket(self, url):
self.websocket = websocket.WebSocketApp(url, on_message=self.receive, on_error=self.error, on_close=self.close, on_open=self.send)
def start(self):
self.websocket.run_forever()
def error(self, ws, e):
if str(e):
self.log.warning(e)
self.console.warning(e)
self.webapi.post_debug_info(e)
def close(self, ws):
self.console.info("Connection Closed.")
def receive(self, ws, message):
self.console.debug(message)
event = json.loads(message)
if self.validate(event):
if event["type"] == "message" and "subtype" not in event:
self.event.on_message(ws, event["user"], event["text"])
if event["type"] == "member_left_channel":
self.event.on_left(ws, event["user"])
if event["type"] == "member_joined_channel":
self.event.on_joined(ws, event["user"])
def send(self, ws):
schedule.every(settings.TIMER_INTERVAL).seconds.do(self.event.on_timer, ws)
def timer():
while True:
schedule.run_pending()
time.sleep(1)
_thread.start_new_thread(timer,())
def validate(self, event):
if "type" not in event:
return False
if "bot_id" in event:
return False
if event.get("user") == settings.USER_SELF:
return False
return True