def main(): # make Slack API connection slack = Slacker(SLACK_AUTH_TOKEN) # get all students and subjects they do student_details, all_subjects = get_student_data(STUDENT_FILE) # get users from Slack like {email: (id, username, real name)} slack_user_details = get_slack_users(slack) # get channels like {channel name: (id, [members])} channel_details = get_slack_channels(slack) # PP.pprint(channel_details) # optionally, clear out non-enrolled students if REMOVE_OLD_STUDENTS: print("Removing all students from subject channels") remove_students(slack, channel_details, slack_user_details, student_details, STAFF_FILE) # return substitutions = create_substitutions() missing_students = set() missing_channels = set() invited_count = 0 # now we can find students not in their subject channels for email, subjects in student_details.items(): try: slack_id = slack_user_details[email][0] except KeyError: missing_students.add(email) continue for subject in subjects: channel_name = subject_to_channel(subject, substitutions) try: # print(email, slack_id, channel_name) # PP.pprint(channel_details[channel_name]) if slack_id not in channel_details[channel_name][1]: print("inviting", email, "to", channel_name) invited_count += 1 try: slack.channels.invite(channel_details[channel_name][0], slack_id) except Exception as error: print("ERROR inviting ({})\n".format(error)) missing_channels.add(channel_name) except Exception as error: print("ERROR with {} lookup ({})\n".format(channel_name, error)) print("Invited people {} times".format(invited_count)) print("\n{} people not in Slack:\n{}".format(len(missing_students), "\n".join(missing_students))) # output text file with missing students # in form ready for bulk Slack invite (comma separated) with open(NONSLACKERS_FILE, "w") as f: f.write(", ".join(missing_students)) if missing_channels: print("\nProblem (probably missing) channels: {}\n".format( "\n".join(missing_channels)))
def main(): slack = Slacker(SLACK_AUTH_TOKEN) pp = PrettyPrinter(indent=4) # get all students and subjects they do groups_students = get_group_lists(STUDENT_FILE) pp.pprint(groups_students) return # get all users from Slack slack_user_details = get_slack_users(slack, pp) # get groups like {'name': (group ID, [member IDs])} group_details = get_slack_groups(slack) # pp.pprint(group_details) # return missing = [] invited_count = 0 for group, students in groups_students.items(): print("Group: {}".format(group)) for email in students: # get slack ID or if user is missing, keep track of them separately try: slack_id = slack_user_details[email][0] # print(group, slack_id, email) except KeyError: missing.append(email) continue # invite students to their groups try: if slack_id not in group_details[group][1]: print("Inviting {} to {}".format(email, group)) invited_count += 1 try: slack.groups.invite(group_details[group][0], slack_id) except: print("ERROR with Slack call, probably missing group for {}\n".format(group)) except: print("ERROR with lookup, probably missing group for {}\n".format(group)) print("Invited people {} times".format(invited_count)) print("\n{} people not in Slack:\n{}".format(len(missing), "\n".join(missing))) # output text file with missing students in form ready for bulk Slack invite (comma separated) with open("output/nonslackers.txt", "w") as f: f.write(", ".join(missing))
def main(): slack = Slacker(SLACK_AUTH_TOKEN) pp = PrettyPrinter(indent=4) # get all students and subjects they do student_details, all_subjects = get_student_data(STUDENT_FILE) # pp.pprint(student_details) # get users from Slack slack_user_details = get_slack_users(slack, pp) # get channels (names and ids) channel_details = get_slack_channels(slack) # pp.pprint(channel_details) missing = [] invited_count = 0 # now we can find students not in their subject channels for email, subjects in student_details.items(): try: slack_id = slack_user_details[email][0] # print(slack_id, email) except KeyError: missing.append(email) continue for subject in subjects: channel = subject_to_channel(subject) try: if slack_id not in channel_details[channel][1]: print("inviting", email, "to", channel) invited_count += 1 try: slack.channels.invite(channel_details[channel][0], slack_id) except: print("ERROR with Slack call, probably missing channel for {}\n".format(channel)) except: print("ERROR with lookup, probably missing channel for {}\n".format(channel)) print("Invited people {} times".format(invited_count)) print("\n{} people not in Slack:\n{}".format(len(missing), "\n".join(missing))) # output text file with missing students in form ready for bulk Slack invite (comma separated) with open("nonslackers.txt", "w") as f: f.write(", ".join(missing))
def main(): slack = Slacker(SLACK_AUTH_TOKEN) # pp = PrettyPrinter(indent=4) # get all students and their groups groups_students = get_group_lists(STUDENT_FILE) # pp.pprint(groups_students) # get all users from Slack slack_user_details = get_slack_users(slack) # get all groups like {'name': (group ID, [member IDs])} group_details = get_slack_groups(slack) # pp.pprint(group_details) missing = [] invited_count = 0 for group_name, students in groups_students.items(): print("Group: {}".format(group_name)) # make group if it doesn't exist try: group_id = group_details[group_name][0] except KeyError: print("No {} group. Adding it now.".format(group_name)) response = slack.groups.create(group_name) try: # add new group details to current groups dictionary in same format (id, [members]) group_id = response.body['group']['id'] group_details[group_name] = (group_id, []) except: print("Creating group {} failed. Exiting.".format(group_name)) return False # add students to group for email in students: # get slack ID or if user is missing, keep track of them separately try: slack_id = slack_user_details[email][0] # print(group, slack_id, email) except KeyError: missing.append(email) continue # invite students to their groups try: if slack_id not in group_details[group_name][1]: print("Inviting {} to {}".format(email, group_name)) invited_count += 1 try: slack.groups.invite(group_id, slack_id) except: print( "ERROR with Slack call. Maybe missing group {} or user {}\n" .format(group_name, email)) except: print("ERROR with lookup, probably missing group for {}\n". format(group_name)) print("Invited people {} times".format(invited_count)) print("\n{} people not in Slack:\n{}".format(len(missing), "\n".join(missing))) # output text file with missing students in form ready for bulk Slack invite (comma separated) with open("output/nonslacker_groups.txt", "w") as f: f.write(", ".join(missing))