Created
December 10, 2019 09:24
-
-
Save jairovadillo/06e62325f6b0cf17a468b235ea14d840 to your computer and use it in GitHub Desktop.
This file contains 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
from datetime import datetime | |
import os | |
from slacker import Slacker | |
import requests | |
SLACK_TOKEN = os.environ['SLACK_TOKEN'] | |
SLACK_CHANNEL = os.environ['SLACK_CHANNEL'] | |
BITLY_API = "https://api-ssl.bitly.com/v3/user/link_history" | |
BITLY_ACCOUNTS = [ | |
{ | |
"name": '<account 1>', | |
"token": "<token 1>", | |
"limit": 3000 | |
}, { | |
"name": '<account 2>', | |
"token": "<token 2>", | |
"limit": 1500 | |
} | |
] | |
slack = Slacker(SLACK_TOKEN) | |
def lambda_handler(event, context): | |
now = datetime.now() | |
timestamp_fist_of_month = int(datetime(now.year,now.month,1,0,0).timestamp()) | |
report = "*Bitly accounts report:*\n\n" | |
for account in BITLY_ACCOUNTS: | |
querystring = { | |
"access_token": account['token'], | |
"created_after": "{}".format(timestamp_fist_of_month) | |
} | |
response = requests.request("GET", BITLY_API, params=querystring) | |
n_links = response.json()['data']['result_count'] | |
report += "- Account {}: {}/{} links used\n".format(account['name'], | |
n_links, | |
account['limit']) | |
slack.chat.post_message(SLACK_CHANNEL, report, as_user=True, link_names=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment