1
+ from stravalib import Client
2
+ from authentication import *
3
+ from sendLINEMessage import *
4
+ from flask import Flask , request
5
+
6
+
7
+ app = Flask (__name__ )
8
+
9
+
10
+ # Validate webhook subscriptions
11
+ @app .get ('/webhook' )
12
+ def webhook_get ():
13
+ data = request .args
14
+ print (data )
15
+ # Your verify token (should be a random string)
16
+ VERIFY_TOKEN = "STRAVA"
17
+ # Parse the query string parameters
18
+ mode = data ['hub.mode' ]
19
+ verify_token = data ['hub.verify_token' ]
20
+ challenge = data ['hub.challenge' ]
21
+ if (mode != 'subscribe' ) or (verify_token != VERIFY_TOKEN ):
22
+ print ('WEBHOOK_NOT_VERIFIED' )
23
+ return ('INVALID_REQUEST' , 401 )
24
+ else :
25
+ print ('WEBHOOK_VERIFIED' )
26
+ return ({'hub.challenge' : challenge }, 200 )
27
+
28
+
29
+ # Receive webhook events
30
+ @app .post ('/webhook' )
31
+ def webhook_post ():
32
+ print ('EVENT_RECEIVED' )
33
+ data = request .json
34
+ print (data )
35
+ # You can do whatever you want upon receving a webhook event
36
+ # Here we send a LINE message with the Strava activity link
37
+ # when a non-indoor activity is created
38
+ if (data ["aspect_type" ] == "create" ):
39
+ client = Client (access_token = get_access_token ())
40
+ activity = client .get_activity (data ["object_id" ])
41
+ if (activity .trainer is True ): # indoor activity
42
+ print ("Activity " + str (activity .id ) + " is an indoor activity, please manually delete it via " +
43
+ "https://www.strava.com/activities/" + str (activity .id ))
44
+ return ("INDOOR_ACTIVITY_CREATED" , 200 )
45
+ else :
46
+ if sendLINEMessage ("https://www.strava.com/activities/" + str (activity .id )):
47
+ print ("LINE message sent!" )
48
+ return ("ACTIVITY_CREATED" , 200 )
49
+ else :
50
+ return ("SOMETHING_WAS_WRONG" , 404 )
51
+ elif (data ["aspect_type" ] == "update" ):
52
+ return ("ACTIVITY_UPDATED" , 200 )
53
+ elif (data ["aspect_type" ] == "delete" ):
54
+ return ("ACTIVITY_DELETED" , 200 )
55
+
56
+
57
+ if __name__ == "__main__" :
58
+ app .run (debug = True )
0 commit comments