Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration and code to configure bot to post on only weekdays #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion default.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"officeHours": {
"on": false,
"begin": 9,
"end": 17
"end": 17,
"onlyWeekdays": true
},

"debug": false,
Expand Down
22 changes: 17 additions & 5 deletions slackbotExercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def setConfiguration(self):
self.office_hours_on = settings["officeHours"]["on"]
self.office_hours_begin = settings["officeHours"]["begin"]
self.office_hours_end = settings["officeHours"]["end"]
self.office_hours_weekdays = settings["officeHours"]["onlyWeekdays"]

self.debug = settings["debug"]

Expand Down Expand Up @@ -264,14 +265,25 @@ def saveUsers(bot):
with open('user_cache.save','wb') as f:
pickle.dump(bot.user_cache,f)

def isOfficeHours(bot):
def isOfficeHours(bot, date):
if not bot.office_hours_on:
if bot.debug:
print "not office hours"
return True
now = datetime.datetime.now()
now_time = now.time()
if now_time >= datetime.time(bot.office_hours_begin) and now_time <= datetime.time(bot.office_hours_end):

if bot.office_hours_weekdays:
if date.isoweekday() in range (1, 6):
if bot.debug:
print "is weekday"
return True
else:
if bot.debug:
print "is not weekday"
return False

time = date.time()

if time >= datetime.time(bot.office_hours_begin) and time <= datetime.time(bot.office_hours_end):
if bot.debug:
print "in office hours"
return True
Expand All @@ -285,7 +297,7 @@ def main():

try:
while True:
if isOfficeHours(bot):
if isOfficeHours(bot, datetime.datetime.now()):
# Re-fetch config file if settings have changed
bot.setConfiguration()

Expand Down