I’m setting up a twitter bot to tweet out the temperature of a city, any idea why my setup function isn’t working?
I tried changing to a different API but nothing seems to work.
console.log('starting twitter bot...')
var Twit = require('twit');
var config = require('./config');
var T = new Twit(config);
setup();
function setup() {
loadJSON("http://api.apixu.com/v1/current.json?key=###############&q=Colombo", gotData);
}
function gotData(data) {
console.log('Weather Data Retrieved...')
var r = data.current[2];
var tweet = {
status: 'here is ' + r + ' temperature test '
}
T.post('statuses/update', tweet);
}
I get this error:
ReferenceError: loadJSON is not defined
2
Answers
Are you using p5.js? You should note that p5 won’t run on node server side as it is dependent on accessing the window object. Hence loadJSON function is undefined.
you could use XMLHttpRequest for retrieving data.
I’d suggest using the request library to pull the weather conditions, in particular use the request-promise-native library, this makes it very easy to read API data:
Just do:
To install, then: