-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackbot.py
69 lines (59 loc) · 2.12 KB
/
slackbot.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
import win32serviceutil
import win32service
import win32event
import servicemanager
import logging
import socket
import time
import emailhandler
import mysecrets
from logging.handlers import RotatingFileHandler
# https://stackoverflow.com/questions/13466053/all-python-windows-service-can-not-starterror-1053
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "SlackNotifyBot"
_svc_display_name_ = "Slack Bot"
def __init__(self, args):
self.eh = emailhandler.emailhandler()
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.is_running = True
socket.setdefaulttimeout(60)
# can take 60 seconds to kill
def SvcStop(self):
self.is_running = False
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.is_running = False
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.is_running = True
self.main()
def main(self):
# set up logging
rfh = logging.handlers.RotatingFileHandler(
filename=mysecrets.log_file_location,
mode='a',
maxBytes=10 * 1024 * 1024,
backupCount=3
)
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(message)s',
handlers=[rfh]
)
logger = logging.getLogger()
logger.debug('slackbot.py:: Service start')
# email account is only for this bot
try:
self.eh.read_all_emails()
except:
logger.error("slackbot.py:: read_all_emails was unsuccessful")
while self.is_running:
# We'll leave this excessive logging on, for now
time.sleep(60)
emails = self.eh.get_emails()
self.eh.process_emails(emails)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)