Skip to content

Instantly share code, notes, and snippets.

@alexisrobert
Created July 24, 2017 14:26
Show Gist options
  • Save alexisrobert/af462aec95c233ff965df05a81337163 to your computer and use it in GitHub Desktop.
Save alexisrobert/af462aec95c233ff965df05a81337163 to your computer and use it in GitHub Desktop.
Automatic registration of Calendly guests in StationF's hal
import os
import dateparser
import datetime
import pytz
import requests
from flask import Flask, request
app = Flask(__name__)
CALENDLY_EVENT_SLUGS = os.getenv("CALENDLY_EVENT_SLUGS")
STATIONF_LOGIN = os.getenv("STATIONF_LOGIN")
STATIONF_PASSWORD = os.getenv("STATIONF_PASSWORD")
HOOK_PWD = os.getenv("HOOK_PWD")
print("-> Calendars : {0}".format(CALENDLY_EVENT_SLUGS.split(',')))
@app.route("/hook/<pwd>", methods=['POST'])
def hook(pwd):
if pwd != HOOK_PWD: return "KO"
content = request.get_json()
print(content)
if content["event"] != "invitee.created": return "SKIP"
if content["payload"]["event_type"]["slug"] not in CALENDLY_EVENT_SLUGS.split(','): return "SKIP"
# Convert the date to the right format
event_date = dateparser.parse(content["payload"]["event"]["start_time"])
event_date_utc = event_date.astimezone(pytz.utc)
event_date_utc = event_date_utc - datetime.timedelta(days=1) + datetime.timedelta(hours=1)
event_date_str = event_date_utc.isoformat().replace('+00:00','.000Z')
stationf_data = {
"date": event_date_str.split('T')[0]+'T22:00:00.000Z',
"duration": "60",
"email": content["payload"]["invitee"]["email"],
"firstName": content["payload"]["invitee"]["first_name"],
"lastName": content["payload"]["invitee"]["last_name"],
"time": "1970-01-01T"+event_date_str.split('T')[1],
"type": "share"
}
print(stationf_data)
print("-> Login StationF ...")
s = requests.Session()
x = s.post("https://hal.stationf.co/login",
json={"login": STATIONF_LOGIN, "password": STATIONF_PASSWORD})
print(x.text)
print("-> Push event ...")
x = s.post("https://hal.stationf.co/api/guests", json=stationf_data)
print(x.text)
return "OK"
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment