Last active
December 23, 2019 03:40
-
-
Save clubapplets-server/194f2bab29ec342beb11 to your computer and use it in GitHub Desktop.
Script to automatically send the content of a gsheet to your slack via webhook
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
# -*- coding: utf8 -*- | |
# !/usr/bin/env python3 | |
""" | |
ApplETS Script Stages Slack automatically sends a reminder on Slack about | |
internships available for ApplETS scientific club. Crontab it if you | |
want to make it every a definite period of time. | |
Copyright 2015 ApplETS applets@ens.etsmtl.ca | |
ApplETS Script Stages Slack is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
ApplETS Script Stages Slack is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
""" | |
import sys | |
import os | |
import argparse | |
import logging | |
import urllib | |
import json | |
import urllib.parse | |
import requests | |
import gspread | |
from oauth2client.client import SignedJwtAssertionCredentials | |
from oauth2client import tools | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() | |
CLIENT_SECRET_FILE = 'client_secret.json' | |
URL_HOOK_SLACK = 'https://hooks.slack.com/services/xxxxxxxx' | |
USERNAME = "ApplETS" | |
ICON_EMOJI = ":logoapplets:" | |
GSHEET_ID = "xxxxxxxxx" | |
CHANNEL = "xxxxxxxxx" | |
def main(): | |
""" | |
Loads a gsheet document describing companies for internships and displays it on Slack | |
""" | |
dir = os.path.join(os.path.dirname(__file__)) | |
json_key = json.load(open(dir + "/" + CLIENT_SECRET_FILE)) | |
scope = ['https://spreadsheets.google.com/feeds'] | |
credentials = SignedJwtAssertionCredentials(json_key['client_email'], bytes(json_key['private_key'], 'utf-8'), | |
scope) | |
gc = gspread.authorize(credentials) | |
worksheet = gc.open_by_key(GSHEET_ID).sheet1 | |
list_rows = worksheet.get_all_values() | |
message = "" | |
tabSpace = 0 | |
sizeTab = 0 | |
for row in list_rows[1:]: | |
if len(row) > 1 and len(row[0]) > tabSpace: | |
tabSpace = len(row[0]) | |
if sizeTab < len(row[1]): | |
sizeTab = len(row[1]) | |
tabSpace += 4 | |
sizeTab += tabSpace | |
message += list_rows[0][0] + "\n\n" | |
message += "```" | |
message += list_rows[1][0] + " " * (tabSpace - len(list_rows[1][0])) + list_rows[1][1] + "\n" | |
message += "-" * sizeTab + "\n" | |
for row in list_rows[2:]: | |
message += row[0] + " " * (tabSpace - len(row[0])) + row[1] + "\n" | |
message += "```" | |
payload = {"channel": CHANNEL, "username": USERNAME, "text": message, "icon_emoji": ICON_EMOJI} | |
data = "payload=" + urllib.parse.quote_plus(json.dumps(payload)) | |
headers = {'content-type': 'application/x-www-form-urlencoded'} | |
response = requests.request("POST", URL_HOOK_SLACK, data=data, headers=headers) | |
print(response.text) | |
sys.exit(0) | |
if __name__ == '__main__': | |
main() | |
__author__ = 'gnut3ll4' | |
__date__ = '2015-08-21' | |
__copyright__ = "Copyright 2015, ApplETS Script Stages Slack" | |
__credits__ = ["Thibaut Tauveron"] | |
__license__ = "GPL" | |
__version__ = "1.0.1" | |
__email__ = "applets@ens.etsmtl.ca" | |
__status__ = "Production" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment