Created
January 2, 2018 11:35
-
-
Save naamancampbell/163308a2343260541ceba999d54c734d to your computer and use it in GitHub Desktop.
Strava API Subscription Webhook on Azure Functions
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
{ | |
"bindings": [ | |
{ | |
"type": "httpTrigger", | |
"direction": "in", | |
"name": "req", | |
"authLevel": "anonymous", | |
"methods": [ | |
"get", | |
"post" | |
] | |
}, | |
{ | |
"type": "http", | |
"direction": "out", | |
"name": "res" | |
} | |
], | |
"disabled": false | |
} |
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
const fetch = require('node-fetch'); | |
module.exports = function (context) { | |
context.log('Webhook was triggered!'); | |
if(context.req.method == 'POST') { | |
if('owner_id' in context.req.body && 'object_id' in context.req.body) { | |
// environment variables: Application settings -> Application settings | |
const url = process.env['API_URL'] + | |
'?athlete_id=' + context.req.body.owner_id + | |
'&activity_id=' + context.req.body.object_id; | |
// post new activity to your API | |
fetch(url) | |
.then(function(response) { | |
context.log(response.text()); | |
}).then(function(body) { | |
context.res = body.json(); | |
context.log = body.json(); | |
}) | |
.catch(function(error) { | |
context.res = { | |
status: 502, | |
body: error.json() }; | |
context.log('API returned error: ', error); | |
}); | |
} | |
} else { // [GET] | |
if('hub.mode' in context.req.query && | |
context.req.query['hub.verify_token'] == 'STRAVA') { | |
context.res = { | |
'hub.challenge': context.req.query['hub.challenge'] }; | |
context.log( | |
'hub.challenge received: ', context.req.query['hub.challenge']); | |
} else { | |
context.res = { | |
status: 400, | |
body: { error: 'Invalid request: ' + context.req.query.toString() }}; | |
context.log( | |
'Subscribe verification error: ', context.req.query.toString()); | |
} | |
} | |
context.done(); | |
} |
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
{ | |
"name": "<your app>", | |
"version": "0.3.1", | |
"description": "Provides the Strava Webhook Subscription functionality for the <your app> app.", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Naaman Campbell", | |
"license": "MIT", | |
"dependencies": { | |
"node-fetch": "^1.7.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment