Last active
November 20, 2016 03:06
-
-
Save marineb/1fef8562fce1bf85cb3a1740dafd53b8 to your computer and use it in GitHub Desktop.
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
module['exports'] = function feelio(hook) { | |
// hook.io has a range of node modules available - see | |
// https://hook.io/modules. | |
// We use request (https://www.npmjs.com/package/request) for an easy way to | |
// make the HTTP request. | |
var request = require('request'); | |
// The parameters passed in via the slash command POST request. | |
var params = hook.params; | |
// Get the data from Feelio API | |
// request.open('GET', 'http://www.feelio.cc/db_calls/feelio-api.php?format=json', true); | |
// var yo = hook.res.end(JSON.stringify(hook.env.feelio_api_json, true, 2)); | |
var url = JSON.stringify(hook.env.feelio_api_json); | |
var yo = hook.res.end(JSON.parse(url)); | |
// var weather = JSON.parse('{"feelio_api": {"currently": {"phrase": "Cold and nice out"}} }'); | |
var phrase = yo.feelio_api.currently.phrase; | |
// Check that the hook has been triggered from our slash command by | |
// matching the token against the one in our environment variables. | |
if(params.token === hook.env.newfeelio_token) { | |
// Set up the options for the HTTP request | |
var options = { | |
// Use the Webhook URL from the Slack Incoming Webhooks integration. | |
uri: hook.env.newfeelio_url, | |
method: 'POST', | |
// Slack expects a JSON payload with a "text" property. | |
json: { | |
// 'text': '@' + params.user_name + ' has a question about ' + params.text, | |
'text' : 'It\'s cold outside' + phrase, | |
// Request that Slack parse URLs, usernames, emoji, etc. in the | |
// incoming text. | |
'parse': 'full' | |
} // ends json hash | |
}; // ends option var definition | |
// Make the POST request to the Slack incoming webhook. | |
request(options, function (error, response, body) { | |
// Pass error back to client if request endpoint can't be reached. | |
if (error) { | |
hook.res.end(error.message); | |
} | |
// Finally, send the response. This will be displayed to the user after | |
// they submit the slash command. | |
hook.res.end('It worked.'); | |
}); // ends request | |
} else { | |
// If the token didn't match, send a response anyway for debugging. | |
hook.res.end('Incorrect token.'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment