Skip to content

Instantly share code, notes, and snippets.

@dacort
Created September 7, 2018 17:20
Show Gist options
  • Save dacort/75ef8323940f9a65fae437185857ecec to your computer and use it in GitHub Desktop.
Save dacort/75ef8323940f9a65fae437185857ecec to your computer and use it in GitHub Desktop.
import sys
import boto3
from slacker import Slacker
import ConfigParser
import csv
from datetime import datetime
reload(sys)
sys.setdefaultencoding('utf8')
config = ConfigParser.RawConfigParser()
config.read('slackbot.cfg')
SLACK = Slacker(config.get('slack', 'token'))
S3 = boto3.resource('s3',
region_name='us-west-2',
aws_access_key_id=config.get('aws', 'access_key_id'),
aws_secret_access_key=config.get('aws', 'secret_access_key')
)
BUKET = config.get('aws', 'bucket')
def lambda_handler(event, context):
response = SLACK.users.list(presence=1)
users = response.body['members']
# Create a file timestamped with the current minute
timestamp = datetime.utcnow()
timestamp_str = timestamp.strftime("%Y-%m-%dT%H:%M")
filename = 'users_%s.csv' % timestamp_str
outfile = open("/tmp/%s" % filename, 'w')
# Create a csv, that'll be easier for us to import elsewhere
headers = ['timestamp', 'id', 'username', 'is_bot', 'deleted', 'presence', 'timezone', 'full_name', 'email']
writer = csv.DictWriter(outfile, headers)
writer.writeheader()
# Write all user presence status to the csv
for user in users:
record = {
'timestamp': timestamp.isoformat(),
'id': user['id'],
'username': user['name'],
'is_bot': user['is_bot'],
'deleted': user['deleted'],
'presence': user.get('presence', None),
'timezone': user.get('tz', None),
'full_name': user['profile']['real_name_normalized'],
'email': user['profile'].get('email', None)
}
writer.writerow(record)
outfile.close()
# Now upload the local file to s3
S3.meta.client.upload_file("/tmp/%s" % filename, BUKET, filename)
if __name__ == "__main__":
lambda_handler(None, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment