-
Notifications
You must be signed in to change notification settings - Fork 1
/
credil_bot.py
executable file
·143 lines (103 loc) · 3.72 KB
/
credil_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
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
#!/usr/bin/python2.7
import sys
import time
from datetime import datetime, timedelta
from config import username, password, chatroom, adminuser, ignoreUsers, xmppHandles, userConfig, conn_string, firstNames, REPL
# import testing_config
if(REPL):
import threading
# 'IPython' import to provide the REPL
try:
import IPython
except ImportError:
print >> sys.stderr, "ipython is not installed....\n\tsudo pip install IPython"
# create thread for ipython repl if set
def repl_start():
IPython.embed()
th = threading.Thread( target = repl_start )
# 'jabberbot' import
try:
from jabberbot import JabberBot, botcmd
except ImportError:
print >> sys.stderr, "you need to install jabberbot...\n\tsudo pip install jabberbot"
sys.exit(-1)
# 'schedule' import
try:
# beh maybe this is better? https://github.com/ahawker/crython
import schedule # https://github.com/dbader/schedule
except ImportError:
print >> sys.stderr, "you need to install 'schedule'...\n\tsudo pip install schedule OR https://github.com/dbader/schedule"
sys.exit(-1)
## other IMPORTS
import config
import redmine_stats
## END IMPORT STUFF ###################################################################################
def working_hours():
"""
return True if during the working week (8am-6pm)
False otherwise.
"""
# where monday == 0, sunday == 6
hour_start = 14
hour_end = 18
hour_report = [14, 16, 18]
minute_min = 0
minute_max = 10
now=datetime.now()
if(now.weekday() >= 0 and now.weekday() <= 4
and now.hour in hour_report
and now.minute >= minute_min and now.minute < minute_max):
return True
else:
return False
## scheduled jobs #####################################################################################
def hour_log(bot, room, skipHourCheck = False):
"""
pass in the instance of the bot
room is which room we are sending to. Idea being we might send to multiple rooms.
"""
if(working_hours() or skipHourCheck == True):
# message = "hello guys did you work today? %s" % datetime.now()
chatroom = room
message = redmine_stats.get_hours()
if(message != -1):
bot.send(chatroom, message, None, 'groupchat')
else:
print "getting stats returned error"
class CredilBot(JabberBot):
def __init__( self, jid, password, res = None):
DEBUG=config.DEBUG_LOG
super( CredilBot, self).__init__( jid, password, res,DEBUG)
self.users = []
self.message_queue = []
self.thread_killed = False
self.timers = []
try: # newer version of JabberBot uses muc_join_room
self.muc_join_room(config.chatroom, config.username.split('@')[0])
except Exception as e:
self.join_room(config.chatroom, config.username.split('@')[0])
# schedule jobs
schedule.every(10).minutes.do(hour_log, self, config.chatroom)
@botcmd
def remindme(self, mess, args):
"""
let the bot remind user of something at a later time.
(ex: `remindme in 20 minutes "go talk to Patty about Phoenix project"`
"""
print mess
print args
return "not implemented"
def idle_proc(self):
"""
super method, gets called periodically
"""
schedule.run_pending() # run scheduled tasks
## setup bot
print "Connecting....."
credilbot = CredilBot( config.username, config.password)
if __name__ == "__main__":
hour_log(credilbot, config.chatroom, True)
if(config.REPL):
credilbot.serve_forever(connect_callback = lambda: th.start())
else:
credilbot.serve_forever(connect_callback = None)