Last active
August 29, 2015 14:01
-
-
Save Hypnopompia/e2bb15414aef8b3b92fc to your computer and use it in GitHub Desktop.
Node app to listen for spark events and tweet the data
This file contains hidden or 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
// To run, first install the twit and eventsource packages: | |
// npm install twit eventsource | |
// then: | |
// node app.js | |
// From the spark core, call: | |
// Spark.publish("tweet", "Hello Twitter. (Test post, please ignore)", 60, PRIVATE); | |
var EventSource = require('eventsource'); | |
var Twit = require('twit'); | |
var twitter = new Twit({ // get your twitter credentials by making a twitter app at https://apps.twitter.com/ | |
consumer_key: '{{twitterConsumerKey}}', | |
consumer_secret: '{{twitterConsumerSecret}}', | |
access_token: '{{twitterAccessToken}}', | |
access_token_secret: '{{twitterAccessTokenSecret}}' | |
}); | |
var deviceId = "{{nameOfSparkCore}}"; | |
var eventName = "tweet"; | |
var sparkAccessToken = "{{sparkAccessToken}}"; | |
var appendHashTag = "#sparkcore"; | |
var url = "https://api.spark.io/v1/devices/" + deviceId + "/events" + "?access_token=" + sparkAccessToken; | |
var es = new EventSource(url); | |
es.addEventListener(eventName, function(event){ | |
var tweet = JSON.parse(event.data).data + " " + appendHashTag; | |
console.log(tweet); | |
twitter.post('statuses/update', { status: tweet }, function(err, data, response) { | |
// console.log(data); | |
}); | |
},false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment