Permalink
Browse files

Completely changed the design

  • Loading branch information...
1 parent 11d7eca commit f84981c1e07aa95c1955c53bc51d0085f409ebf0 @ahollenbach ahollenbach committed with Jun 15, 2015
Showing with 369 additions and 107 deletions.
  1. +36 −0 default.json
  2. +29 −0 fetchChannelId.py
  3. +144 −54 slackbotExercise.py
  4. +160 −53 slackbotExercise.py.orig
View
@@ -0,0 +1,36 @@
+{
+ "teamDomain": "yourDomainHere",
+ "channelName": "general",
+ "channelId": "channelIdHere",
+ "timeBetweenCallouts": {
+ "minTime": 5,
+ "maxTime": 15,
+ "units": "minutes"
+ },
+ "exercises": [
+ {
+ "name": "pushups",
+ "minReps": 15,
+ "maxReps": 20,
+ "units": "reps"
+ },
+ {
+ "name": "planks",
+ "minReps": 40,
+ "maxReps": 60,
+ "units": "seconds"
+ },
+ {
+ "name": "wall sit",
+ "minReps": 30,
+ "maxReps": 50,
+ "units": "seconds"
+ },
+ {
+ "name": "chair dips",
+ "minReps": 15,
+ "maxReps": 30,
+ "units": "reps"
+ }
+ ]
+}
View
@@ -0,0 +1,29 @@
+'''
+A quick script to fetch the id of a channel you want to use.
+
+USAGE: python fetchChannelId.py <channel_name>
+'''
+
+import requests
+import sys
+import os
+import json
+
+# Environment variables must be set with your tokens
+USER_TOKEN_STRING = os.environ['SLACK_USER_TOKEN_STRING']
+URL_TOKEN_STRING = os.environ['SLACK_URL_TOKEN_STRING']
+
+HASH = "%23"
+
+channelName = sys.argv[1]
+
+params = {"token": USER_TOKEN_STRING }
+
+# Capture Response as JSON
+response = requests.get("https://slack.com/api/channels.list", params=params)
+channels = json.loads(response.text, encoding='utf-8')["channels"]
+
+for channel in channels:
+ if channel["name"] == channelName:
+ print channel["id"]
+ break
View
@@ -5,89 +5,179 @@
import csv
import os
-USERTOKENSTRING = os.environ['SLACK_USER_TOKEN_STRING']
-URLTOKENSTRING = os.environ['URL_TOKEN_STRING']
+# Environment variables must be set with your tokens
+USER_TOKEN_STRING = os.environ['SLACK_USER_TOKEN_STRING']
+URL_TOKEN_STRING = os.environ['SLACK_URL_TOKEN_STRING']
+HASH = "%23"
-MIN_COUNTDOWN = 5 # in minutes
-MAX_COUNTDOWN = 30 # in minutes
+DEBUG = True
-TEAM_DOMAIN = "YOUR_TEAM_HERE"
-HASH = "%23"
-CHANNEL_NAME = "YOUR_CHANNEL_HERE"
-FULL_URL = "https://" + TEAM_DOMAIN + ".slack.com/services/hooks/slackbot?token=" + URLTOKENSTRING + "&channel=" + HASH + CHANNEL_NAME
+# local cache of usernames
+# maps userIds to usernames
+user_cache = {}
+
+# Configuration values to be set in setConfiguration
+class Bot:
+ def __init__(self):
+ self.setConfiguration()
+
+ '''
+ Sets the configuration file.
+
+ Runs after every callout so that settings can be changed realtime
+ '''
+ def setConfiguration(self):
+ # Read variables fromt the configuration file
+ with open('config.json') as f:
+ settings = json.load(f)
+
+ self.team_domain = settings["teamDomain"]
+ self.channel_name = settings["channelName"]
+ self.min_countdown = settings["timeBetweenCallouts"]["minTime"]
+ self.max_countdown = settings["timeBetweenCallouts"]["maxTime"]
+ self.channel_id = settings["channelId"]
+ self.exercises = settings["exercises"]
+
+ self.post_URL = "https://" + self.team_domain + ".slack.com/services/hooks/slackbot?token=%23" + URL_TOKEN_STRING + "&channel=" + HASH + self.channel_name
-def extractSlackUsers(token):
- # Set token parameter of Slack API call
- tokenString = token
- params = {"token": tokenString}
+
+################################################################################
+
+
+'''
+Fetches a list of all slack users in the channel
+'''
+def fetchSlackUsers(bot):
+ params = {"token": USER_TOKEN_STRING, "channel": bot.channel_id}
# Capture Response as JSON
response = requests.get("https://slack.com/api/channels.info", params=params)
users = json.loads(response.text, encoding='utf-8')["channel"]["members"]
- def findUserNames(x):
- if getStats(x) is False:
- return None
- name = "@" + x["name"].encode('utf-8')
- return name.encode('utf-8')
+ return filter(None, list(map(fetchActiveUsers, users)))
- def getStats(x):
- params = {"token": tokenString, "user": x["id"]}
- 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)))
+'''
+Fetches all of the active users
+'''
+def fetchActiveUsers(userId):
+ if not isActive(userId):
+ return None
+ return getName(userId).encode('utf-8')
-# Selects Next Time Interval and Returns the Exercise
-def selectExerciseAndStartTime():
- # Exercise (2 Forms of Strings)
- exercises = [" PUSHUPS ", " PUSHUPS ", " SECOND PLANK ", " SITUPS ", " SECOND WALL SIT "]
- exerciseAnnouncements = ["PUSHUPS", "PUSHUPS", "PLANK", "SITUPS", "WALLSIT"]
+'''
+Fetches the username for a given id
+'''
+def getName(userId):
+ if userId in user_cache:
+ username = user_cache[userId]
+ else:
+ params = {"token": USER_TOKEN_STRING, "user": userId}
+ response = requests.get("https://slack.com/api/users.info",
+ params=params)
+ username = json.loads(response.text, encoding='utf-8')["user"]["name"]
+ user_cache[userId] = username
+ print "New user: " + username
- # Random Number generator for Reps/Seconds and Exercise
- nextTimeInterval = random.randrange(MIN_COUNTDOWN * 60, MAX_COUNTDOWN * 60)
- exerciseIndex = random.randrange(0, 5)
+ return ("@" + username).encode('utf-8')
- # Announcement String of next lottery time
- lotteryTimeString = "NEXT LOTTERY FOR " + str(exerciseAnnouncements[exerciseIndex]) + " IS IN " + str(nextTimeInterval/60) + " MINUTES"
- requests.post(FULL_URL, data=lotteryTimeString)
+'''
+Returns true if a user is currently "active", else false
+'''
+def isActive(userId):
+ params = {"token": USER_TOKEN_STRING, "user": userId}
+ response = requests.get("https://slack.com/api/users.getPresence",
+ params=params)
+ status = json.loads(response.text, encoding='utf-8')["presence"]
+
+ return status == "active"
+
- # Sleep until next lottery announcement
- time.sleep(nextTimeInterval)
+'''
+Selects a slack user from a list of slack users
+'''
+def selectSlackUser(bot):
+ slackUsers = fetchSlackUsers(bot)
- # Return exercise
- return str(exercises[exerciseIndex])
+ return slackUsers[random.randrange(0, len(slackUsers))]
-# Selects the exercise lottery winner
-def selectPerson(exercise):
+'''
+Selects the next exercise
+'''
+def selectExercise(bot):
+ idx = random.randrange(0, len(bot.exercises))
+ return bot.exercises[idx]
+'''
+Selects the next time interval
+'''
+def selectNextTimeInterval(bot):
+ return random.randrange(bot.min_countdown * 60, bot.max_countdown * 60)
+
+'''
+Selects an exercise and start time, and sleeps until the time
+period has past.
+'''
+def selectExerciseAndStartTime(bot):
+ next_time_interval = selectNextTimeInterval(bot)
+ exercise = selectExercise(bot)
+
+ # Announcement String of next lottery time
+ lottery_announcement = "NEXT LOTTERY FOR " + exercise["name"] + " IS IN " + str(next_time_interval/60) + " MINUTES"
+
+ # Announce the exercise to the thread
+ if not DEBUG:
+ requests.post(bot.post_URL, data=lottery_announcement)
+ print lottery_announcement
+
+ # Sleep the script until time is up
+ if not DEBUG:
+ time.sleep(next_time_interval)
+
+ return exercise
+
+
+'''
+Selects a person to do the already-selected exercise
+'''
+def assignExercise(bot, exercise):
# Select number of reps
- exerciseReps = random.randrange(25, 50)
+ exercise_reps = random.randrange(exercise["minReps"], exercise["maxReps"]+1)
+ winner = selectSlackUser(bot)
+
+ winner_announcement = str(exercise_reps) + " " + str(exercise["units"]) + " " + exercise["name"] + " RIGHT NOW " + str(winner)
- # Pull all users from API
- slackUsers = extractSlackUsers(USERTOKENSTRING)
+ if not DEBUG:
+ requests.post(bot.post_URL, data=winner_announcement)
+ print winner_announcement
- # Select index of team member from array of team members
- selection = random.randrange(0, len(slackUsers))
+ logExercise(winner,exercise["name"],exercise_reps,exercise["units"])
- # Select lottery winner
- lotteryWinnerString = str(exerciseReps) + str(exercise) + "RIGHT NOW " + slackUsers[selection]
- print lotteryWinnerString
- requests.post(FULL_URL, data=lotteryWinnerString)
- with open("results.csv", 'a') as f:
+def logExercise(user,exercise,reps,units):
+ with open("log.csv", 'a') as f:
writer = csv.writer(f)
- writer.writerow([slackUsers[selection], exerciseReps, exercise])
+ writer.writerow([user,exercise,reps,units])
+
+
+def main():
+ bot = Bot()
+
+ while True:
+ # Re-fetch config file if settings have changed
+ bot.setConfiguration()
+
+ # Get an exercise to do
+ exercise = selectExerciseAndStartTime(bot)
-for i in range(10000):
- exercise = selectExerciseAndStartTime()
- selectPerson(exercise)
+ # Assign the exercise to someone
+ assignExercise(bot, exercise)
+main()
Oops, something went wrong.

0 comments on commit f84981c

Please sign in to comment.