From 5b005a244cf01b822e7b865aa68cf8326e807b84 Mon Sep 17 00:00:00 2001 From: Oleg Melnichuk Date: Wed, 1 Mar 2017 10:17:35 +0200 Subject: [PATCH 1/6] add changes for working with private channels --- default.json | 1 + fetchChannelId.py | 9 +++++++++ slackbotExercise.py | 9 +++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/default.json b/default.json index 2775ae2..3454afd 100644 --- a/default.json +++ b/default.json @@ -2,6 +2,7 @@ "teamDomain": "yourDomainHere", "channelName": "general", "channelId": "channelIdHere", + "isPrivate": "false", "officeHours": { "on": false, diff --git a/fetchChannelId.py b/fetchChannelId.py index 9764317..667e8a7 100644 --- a/fetchChannelId.py +++ b/fetchChannelId.py @@ -27,3 +27,12 @@ if channel["name"] == channelName: print channel["id"] break + +#if it's private channel +response = requests.get("https://slack.com/api/groups.list", params=params) +channels = json.loads(response.text, encoding='utf-8')["groups"] + +for channel in channels: + if channel["name"] == channelName: + print channel["id"] + break \ No newline at end of file diff --git a/slackbotExercise.py b/slackbotExercise.py index d66d712..f5b99cf 100644 --- a/slackbotExercise.py +++ b/slackbotExercise.py @@ -55,6 +55,7 @@ def setConfiguration(self): self.team_domain = settings["teamDomain"] self.channel_name = settings["channelName"] + self.is_private = settings["isPrivate"] self.min_countdown = settings["callouts"]["timeBetween"]["minTime"] self.max_countdown = settings["callouts"]["timeBetween"]["maxTime"] self.num_people_per_callout = settings["callouts"]["numPeople"] @@ -122,8 +123,12 @@ def selectUser(bot, exercise): def fetchActiveUsers(bot): # Check for new members params = {"token": USER_TOKEN_STRING, "channel": bot.channel_id} - response = requests.get("https://slack.com/api/channels.info", params=params) - user_ids = json.loads(response.text, encoding='utf-8')["channel"]["members"] + if self.is_private: + urlSegment = "channel"; + else: + urlSegment = "group"; + response = requests.get("https://slack.com/api/" + urlSegment + "s.info", params=params) + user_ids = json.loads(response.text, encoding='utf-8')[urlSegment]["members"] active_users = [] From 385709c8fe5c6b9e2c151c1ef01cd1c538f8da69 Mon Sep 17 00:00:00 2001 From: Oleg Melnichuk Date: Wed, 1 Mar 2017 11:30:19 +0200 Subject: [PATCH 2/6] configs typo fix --- default.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/default.json b/default.json index 3454afd..40d717c 100644 --- a/default.json +++ b/default.json @@ -2,7 +2,7 @@ "teamDomain": "yourDomainHere", "channelName": "general", "channelId": "channelIdHere", - "isPrivate": "false", + "isPrivate": false, "officeHours": { "on": false, From d30259819796a4ab42aee02de367637d9119c6cc Mon Sep 17 00:00:00 2001 From: Oleg Melnichuk Date: Wed, 1 Mar 2017 12:46:19 +0200 Subject: [PATCH 3/6] fix typo issues :) --- slackbotExercise.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slackbotExercise.py b/slackbotExercise.py index f5b99cf..2f5e78f 100644 --- a/slackbotExercise.py +++ b/slackbotExercise.py @@ -123,7 +123,7 @@ def selectUser(bot, exercise): def fetchActiveUsers(bot): # Check for new members params = {"token": USER_TOKEN_STRING, "channel": bot.channel_id} - if self.is_private: + if bot.is_private: urlSegment = "channel"; else: urlSegment = "group"; From a7a6020dda2eb782279f5aab12fc9bc02df27eb6 Mon Sep 17 00:00:00 2001 From: Oleg Melnichuk Date: Wed, 1 Mar 2017 12:54:25 +0200 Subject: [PATCH 4/6] small fix --- slackbotExercise.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/slackbotExercise.py b/slackbotExercise.py index 2f5e78f..f57f2bb 100644 --- a/slackbotExercise.py +++ b/slackbotExercise.py @@ -124,10 +124,10 @@ def fetchActiveUsers(bot): # Check for new members params = {"token": USER_TOKEN_STRING, "channel": bot.channel_id} if bot.is_private: - urlSegment = "channel"; + urlSegment = "groups"; else: - urlSegment = "group"; - response = requests.get("https://slack.com/api/" + urlSegment + "s.info", params=params) + urlSegment = "channels"; + response = requests.get("https://slack.com/api/" + urlSegment + ".info", params=params) user_ids = json.loads(response.text, encoding='utf-8')[urlSegment]["members"] active_users = [] From d22588a2a91607a65e1acada166b66efafc44e33 Mon Sep 17 00:00:00 2001 From: Oleg Melnichuk Date: Wed, 1 Mar 2017 16:19:17 +0200 Subject: [PATCH 5/6] checking --- slackbotExercise.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/slackbotExercise.py b/slackbotExercise.py index f57f2bb..1827043 100644 --- a/slackbotExercise.py +++ b/slackbotExercise.py @@ -124,10 +124,10 @@ def fetchActiveUsers(bot): # Check for new members params = {"token": USER_TOKEN_STRING, "channel": bot.channel_id} if bot.is_private: - urlSegment = "groups"; + urlSegment = "group"; else: - urlSegment = "channels"; - response = requests.get("https://slack.com/api/" + urlSegment + ".info", params=params) + urlSegment = "channel"; + response = requests.get("https://slack.com/api/" + urlSegment + "s.info", params=params) user_ids = json.loads(response.text, encoding='utf-8')[urlSegment]["members"] active_users = [] From 8101835daceb48cfd508cbd91d3bb02a323e6374 Mon Sep 17 00:00:00 2001 From: Oleg Melnichuk Date: Tue, 14 Mar 2017 16:34:21 +0200 Subject: [PATCH 6/6] add one more instruction field for exercise --- default.json | 5 +++++ slackbotExercise.py | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/default.json b/default.json index 40d717c..79c8af5 100644 --- a/default.json +++ b/default.json @@ -27,6 +27,7 @@ { "id": 0, "name": "pushups", + "instruction": "", "minReps": 15, "maxReps": 20, "units": "rep" @@ -34,6 +35,7 @@ { "id": 1, "name": "planks", + "instruction": "", "minReps": 40, "maxReps": 60, "units": "second" @@ -41,6 +43,7 @@ { "id": 2, "name": "wall sit", + "instruction": "", "minReps": 40, "maxReps": 50, "units": "second" @@ -48,6 +51,7 @@ { "id": 3, "name": "chair dips", + "instruction": "", "minReps": 15, "maxReps": 30, "units": "rep" @@ -55,6 +59,7 @@ { "id": 4, "name": "calf raises", + "instruction": "", "minReps": 20, "maxReps": 30, "units": "rep" diff --git a/slackbotExercise.py b/slackbotExercise.py index 1827043..46fcd6c 100644 --- a/slackbotExercise.py +++ b/slackbotExercise.py @@ -79,6 +79,7 @@ def setConfiguration(self): def selectUser(bot, exercise): active_users = fetchActiveUsers(bot) + #print len(active_users) # Add all active users not already in the user queue # Shuffles to randomly add new active users shuffle(active_users) @@ -197,7 +198,7 @@ def assignExercise(bot, exercise): # Select number of reps exercise_reps = random.randrange(exercise["minReps"], exercise["maxReps"]+1) - winner_announcement = str(exercise_reps) + " " + str(exercise["units"]) + " " + exercise["name"] + " RIGHT NOW " + winner_announcement = str(exercise_reps) + " " + str(exercise["units"]) + " " + exercise["name"] + " (" + exercise["instruction"] + ") RIGHT NOW " # EVERYBODY if random.random() < bot.group_callout_chance: @@ -210,6 +211,10 @@ def assignExercise(bot, exercise): logExercise(bot,"@channel",exercise["name"],exercise_reps,exercise["units"]) else: + amount_active_users = len(fetchActiveUsers(bot)) + if bot.num_people_per_callout > amount_active_users: + bot.num_people_per_callout = amount_active_users + winners = [selectUser(bot, exercise) for i in range(bot.num_people_per_callout)] for i in range(bot.num_people_per_callout):