To use the twitter package, you'll need to:
Add the dependency in the file package.json
, the easiest way is: (inside the project folder)
npm --save install twitter
Then you must import the module to your application and configure it with the keys you have. By the way, that's what require
is for, to import another module to the current module.
var Twitter = require('twitter');
var twclient = new Twitter({
consumer_key: '', // debes poner los datos correctos en estos campos
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
All this as you know is in the documentation, now:
To get the latest Twit
You must obtain the route statuses/user_timeline
. Look at the documentation as there are other options for the consultation, here prepare a very simple:
// preparamos los datos del usuario a consultar
var usuario = {
// en screen_name, pones el nombre de usuario sin la arroba.
screen_name: 'pedromiguelpimienta',
// la cantidad de mensajes a obtener
count: 1,
// solo mensajes propios (no re-tweets)
include_rts: false
};
twclient.get('statuses/user_timeline', usuario, function(error, tweets, response){
if(error) throw error; // se produjo un error, manejar aquí
if (tweets.length) {
console.log(tweets[0].text); // el texto del ultimo twit, si hay alguno
}
});
By the way this is a very popular library, only today received more than 1000 downloads.
EDITION:
In this repository, I leave you a functional example with the instructions so you can try it.
link