Created
February 24, 2019 15:14
-
-
Save youminkim/3cd4f2a15fa2028b8b5658c76c719a98 to your computer and use it in GitHub Desktop.
G Suite Directory's Birthday alarms to Slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "import datetime, json\nfrom IPython.display import display, HTML\nimport pandas as pd\nimport numpy as np\n\nfrom googleapiclient.discovery import build\nfrom google.oauth2 import service_account\ncredentials = service_account.Credentials.from_service_account_file(\n '...',\n scopes=[\n 'https://www.googleapis.com/auth/admin.directory.user',\n ],\n)\ndelegated_credentials = credentials.with_subject('[email protected]')\nservice = build('admin', 'directory_v1', credentials=delegated_credentials)\n\nresults = service.users().list(domain=\"ksisters.sg\", projection=\"full\").execute()\nusers = results.get('users', [])", | |
| "execution_count": 3, | |
| "outputs": [] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true | |
| }, | |
| "cell_type": "code", | |
| "source": "import datetime\nimport requests\n\ndef is_match(d1, d2):\n return d1.month == d2.month and d1.date == d2.date\n\ndef send_slack(msg, webhook): \n message = {\n \"text\": msg\n }\n\n result = requests.post(\n webhook, \n data=json.dumps(message), headers={\"Content-Type\": \"application/json\"} \n )\n return result\n\nfor u in users:\n info = u['customSchemas']['Employee_Information']\n start_date = datetime.datetime.strptime(info['Start_date'], '%Y-%m-%d') if 'Start_date' in info else None\n birthday = datetime.datetime.strptime(info['Birthday'], '%Y-%m-%d')if 'Birthday' in info else None\n \n today = datetime.datetime.now()\n week_later_sd = today + datetime.timedelta(days=7)\n if start_date and (is_match(today, start_date)):\n message = \"π It's {0}'s Ksistersary. Her starting date was {1}\".format(\n u['name']['givenName'],\n start_date.strftime(\"%Y %b %d\"),\n )\n send_slack(message, webhook_general) \n elif start_date and is_match(today, week_later_sd):\n message = \"π{0}'s Ksistersary is coming. Her starting date was {1}\".format(\n u['name']['givenName'],\n start_date.strftime(\"%Y %b %d\"),\n )\n send_slack(message, webhook_lead)\n \n week_later_bd = today + datetime.timedelta(days=7)\n if birthday and (is_match(today, birthday) or is_match(today, week_later_bd)):\n message = \"π It's {0}'s birthday! Her birthday is {1}\".format(\n u['name']['givenName'],\n birthday.strftime(\"%b %d\"),\n )\n send_slack(message, webhook_general)\n elif birthday and is_match(today, week_later_bd):\n message = \"π {0}'s birthday is coming. Her birthday was {1}\".format(\n u['name']['givenName'],\n birthday.strftime(\"%Y %b %d\"),\n )\n send_slack(message, webhook_lead)", | |
| "execution_count": 5, | |
| "outputs": [] | |
| } | |
| ], | |
| "metadata": { | |
| "gist": { | |
| "id": "8b65a93e4614f2c76560dc0a1ca7a14b", | |
| "data": { | |
| "description": "G Suite Directory's Birthday alarms to Slack", | |
| "public": true | |
| } | |
| }, | |
| "hide_input": false, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3", | |
| "language": "python" | |
| }, | |
| "language_info": { | |
| "name": "python", | |
| "version": "3.6.8", | |
| "mimetype": "text/x-python", | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "pygments_lexer": "ipython3", | |
| "nbconvert_exporter": "python", | |
| "file_extension": ".py" | |
| }, | |
| "_draft": { | |
| "nbviewer_url": "https://gist.github.com/8b65a93e4614f2c76560dc0a1ca7a14b" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment