-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
112 lines (84 loc) · 2.95 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from typing import Optional
from flask import Flask, request, Response
from flask import jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.dialects.postgresql import insert
from werkzeug.utils import import_string
from gobiko.apns import APNsClient
import os
import requests
app = Flask(__name__)
configuration = import_string(os.environ['APP_SETTINGS'])()
app.config.from_object(configuration)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
client = APNsClient(
team_id=os.environ['TEAM_ID'],
bundle_id=os.environ['BUNDLE_ID'],
auth_key_id=os.environ['APNS_KEY_ID'],
auth_key=os.environ['APNS_TOKEN'],
use_sandbox=configuration.SANDOX
)
from models import *
def check_challenge(key: str) -> Optional[Response]:
default = Response('Get up on out of here with my Eyeholes!', 401)
try:
verification = request.json[key]
if verification == os.environ['VERIFY_TOKEN']:
return None
else:
return default
except:
return default
@app.route('/webhook', methods=['GET'])
def challenge() -> Response:
challengeOutcome = check_challenge('hub.verify_token')
if challengeOutcome is not None:
return challengeOutcome
challenge = request.values.get('hub.challenge')
resp = jsonify({'hub.challenge': challenge})
resp.headers['Content-Type'] = 'application/json'
resp.status_code = 200
return resp
@app.route('/webhook', methods=['POST'])
def update() -> Response:
try:
print(request.json)
ownerId = request.json["owner_id"]
athlete = AthleteNotifications.query.filter_by(id=ownerId).first()
if athlete is None:
# Strava's API expects a 200
return Response('User not found', 200)
client.send_message(
athlete.token,
"Dash Notification",
content_available=True,
extra=request.json,
sound='default',
priority=5,
topic=os.environ['BUNDLE_ID']
)
print('Sent...')
return Response('Success sending Push', 200)
except Exception as error:
# Strava's API expects a 200
return Response('Bad request: {0}'.format(error), 200)
@app.route('/registerUser', methods=['POST'])
def registerUser() -> Response:
challengeOutcome = check_challenge("verify_token")
if challengeOutcome is not None:
return challengeOutcome
try:
athleteId = request.json["athlete_id"]
token = request.json["token"]
value = AthleteNotifications(id=athleteId, token=token)
db.session.merge(value)
db.session.commit()
return Response("Added", 201)
except SQLAlchemyError as error:
return Response('DB exception: {0}'.format(error), 500)
except Exception as error:
return Response('Exception: {0}'.format(error), 400)
if __name__ == '__main__':
# registerCallback()
app.run()