Permalink
Browse files

created config.json file to save "environmental variables" into

  • Loading branch information...
1 parent 2387f7a commit 3b8f85b804ab7abe430ca8b4ae43ddaa8f4203e0 @brandonshin committed Jun 14, 2015
Showing with 37 additions and 8 deletions.
  1. +5 −0 config.json
  2. +9 −0 results.csv
  3. +23 −8 slackbotExercise.py
View
@@ -0,0 +1,5 @@
+{
+ "USERTOKENSTRING" : "YOUR USER TOKEN STRING",
+ "URLTOKENSTRING" : "YOUR URL TOKEN STRING",
+ "TEAMNAMESTRING" : "YOUR SLACK TEAM NAME"
+}
View
@@ -0,0 +1,9 @@
+@bshin,26, SECOND PLANK
+@shahan,45, PUSHUPS
+@shahan,42, SECOND WALL SIT
+@bshin,26, PUSHUPS
+@bshin,35, PUSHUPS
+@bshin,28, PUSHUPS
+@shahan,40, SECOND PLANK
+@brandon,48, PUSHUPS
+@bshin,36, SECOND WALL SIT
View
@@ -4,10 +4,15 @@
import json
import csv
-USERTOKENSTRING = # YOUR (SLACKBOT API) USER AUTH TOKEN
-URLTOKENSTRING = # SLACKBOT REMOTE CONTROL URL TOKEN
-TEAMNAMESTRING = # YOUR SLACKTEAM NAME
+# Set your config variables from the config.json file
+with open('config.json') as f:
+ settings = json.load(f)
+ USERTOKENSTRING = settings['USERTOKENSTRING']
+ URLTOKENSTRING = settings["URLTOKENSTRING"]
+ TEAMNAMESTRING = settings["TEAMNAMESTRING"]
+
+# Extracts online users from Slack API
def extractSlackUsers(token):
# Set token parameter of Slack API call
tokenString = token
@@ -18,19 +23,21 @@ def extractSlackUsers(token):
users = json.loads(response.text, encoding='utf-8')["members"]
def findUserNames(x):
- if getStats(x) == False:
+ if getStats(x) is False:
return None
name = "@" + x["name"].encode('utf-8')
return name.encode('utf-8')
+
def getStats(x):
params = {"token": tokenString, "user": x["id"]}
- response = requests.get("https://slack.com/api/users.getPresence",
- params=params)
+ response = requests.get("https://slack.com/api/users.getPresence", params=params)
status = json.loads(response.text, encoding='utf-8')["presence"]
return status == "active"
return filter(None, list(map(findUserNames, users)))
+
+# Selects Next Time Interval and Returns the Exercise
def selectExerciseAndStartTime():
# Exercise (2 Forms of Strings)
@@ -44,13 +51,17 @@ def selectExerciseAndStartTime():
# Announcement String of next lottery time
lotteryTimeString = "NEXT LOTTERY FOR " + str(exerciseAnnouncements[exerciseIndex]) + " IS IN " + str(nextTimeInterval/60) + " MINUTES"
- requests.post("https://"+ TEAMNAMESTRING +".slack.com/services/hooks/slackbot?token="+URLTOKENSTRING+"&channel=%23general", data=lotteryTimeString)
+ # POST next lottery announcement to Slack
+ requests.post("https://" + TEAMNAMESTRING + ".slack.com/services/hooks/slackbot?token="+URLTOKENSTRING+"&channel=%23general", data=lotteryTimeString)
+ # Sleep until next lottery announcement
time.sleep(nextTimeInterval)
+ # Return exercise
return str(exercises[exerciseIndex])
+# Selects the exercise lottery winner
def selectPerson(exercise):
# Select number of reps
@@ -62,10 +73,14 @@ def selectPerson(exercise):
# Select index of team member from array of team members
selection = random.randrange(0, len(slackUsers))
+ # Select lottery winner
lotteryWinnerString = str(exerciseReps) + str(exercise) + "RIGHT NOW " + slackUsers[selection]
print lotteryWinnerString
- requests.post("https://"+ TEAMNAMESTRING +".slack.com/services/hooks/slackbot?token="+URLTOKENSTRING+"&channel=%23general", data=lotteryWinnerString)
+ # POST to Slack
+ requests.post("https://" + TEAMNAMESTRING + ".slack.com/services/hooks/slackbot?token="+URLTOKENSTRING+"&channel=%23general", data=lotteryWinnerString)
+
+ # Record exercise entry in csv
with open("results.csv", 'a') as f:
writer = csv.writer(f)
writer.writerow([slackUsers[selection], exerciseReps, exercise])

0 comments on commit 3b8f85b

Please sign in to comment.