Ad
Pulling Data Off A Weather API And Tweeting It
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
Ad
Answer
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:
npm install request
npm install request-promise-native
To install, then:
const API_KEY = '7165..'; // Put your API key here
const Twit = require('twit');
const config = require('./config');
const rp = require('request-promise-native');
async function testWeatherTweet(location) {
const options = {
url: "http://api.apixu.com/v1/current.json",
qs: {
key: API_KEY,
q: location
},
json: true
};
let result = await rp(options);
let condition = result.current.condition.text;
let tweetText = `Conditions in ${location} are currently ${condition}, temperature is ${result.current.temp_c}°C.`;
console.log("Sending tweet: ", tweetText);
sendTweet(tweetText)
}
function sendTweet(text) {
const T = new Twit(config);
const tweet = {
status: text
}
T.post('statuses/update', tweet);
}
testWeatherTweet('Colombo');
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad