A basic Twitter API for Node.js v4.0+.
Follow, unfollow, tweet, get infos about a twitter account and more ...
Getting started
When you have installed and include this twitter module, you have to create a new instance:
Requirements
Installation
npm install easy-twitter --save
#### Include the module
const TwitterClient = require('easy-twitter');
const twitter = new TwitterClient({consumer_key: "xxxx",
consumer_secret: "yyyy",
access_token_key: "zzzz",
access_token_secret : "aaaa"});
Usage
If you want to tweet a message you can use the method Twitter.tweet()
. This method returns a promise with the message
in parameter when succeed.
var message = 'Hello world !';
twitter.tweet(message)
.then(data => {
console.log('Posted the tweet:' + data.tweet);
})
.catch(err => {
console.error(err.error);
});
try {
const data = await twitter.tweet(message)
} catch (e) {
throw e
}
The first parameter is the message
you want to post and the second parameter is the path
to the image that you want to post.
const message = 'I like Twitter !';
const path = '/foo/bar/image.png';
twitter.uploadMedia(message, path)
.then(data => {
console.log('Tweeted the message: ' + data.message);
})
.catch(err => {
console.error(err.error);
});
try {
const data = await twitter.uploadMedia(message, path)
} catch (e) {
throw e
}
You can follow someone with the method follow()
with the name
of the twitter account in parameter. You can pass in parameter the name of the account with or without the '@'.
const twitterAccount = 'iAmAlphaZz';
const secondTwitterAccount = '@iAmAlphaZz';
twitter.follow(twitterAccount)
.then(data => {
console.log('You\'re now following :' + data.user);
})
.catch(err => {
console.error(err.error);
});
try {
const data = await twitter.follow(twitterAccount)
} catch (e) {
throw e
}
The unfollow()
method is used like the follow()
method: you just have to pass in parameter the name of the twitter account.
const twitterAccount = 'iAmAlphaZz';
const secondTwitterAccount = '@iAmAlphaZz';
twitter.unfollow(twitterAccount)
.then(data => {
console.log('You\'re now following :' + data.user);
})
.catch(err => {
console.error(err.error);
});
try {
const data = await twitter.unfollow(twitterAccount)
} catch (e) {
throw e
}
Get the count
(max: 200) followers list of twitterAccount
const twitterAccount = 'iAmAlphaZz';
const count = 12;
twitter.getFollowersList(twitterAccount, count)
.then(data => {
})
.catch(err => {
console.error(err.error);
});
try {
const data = await twitter.getFollowersList(twitterAccount, count)
} catch (e) {
throw e
}
The method getFollowersList()
return three datas:
followers
: An array with the account names of the followersfullInfos
: A JSON Object which contains a lot of informations. Here is a link showing the differents informations returned in this object ( look the 'Example Result' : https://dev.twitter.com/rest/reference/get/followers/list)user
: Is the user name of the twitter account you entered in parameter to the method.
Get the count
(max: 200) people from the friend list of twitterAccount
.
Actually, it returns only the count
lasts. The page
parameter will able to choose more precisely.
const twitterAccount = 'iAmAlphaZz';
const count = 50;
const page = 1;
twitter.getFriendsList(twitterAccount, count, page)
.then(data => {
})
.catch(err => {
console.error(err.error);
});
try {
const data = await twitter.getFriendsList(twitterAccount, count, page)
} catch (e) {
throw e
}
The method getFollowersList()
returns:
followers
: An array with the account names of the followersfullInfos
: A JSON Object which contains a lot of informations. Here is a link showing the differents informations returned in this object ( look the 'Example Result' : https://dev.twitter.com/rest/reference/get/friends/list)user
: Is the user name of the twitter account you entered in parameter to the method.page
: Is not yet implemented...
#### Get the followers count & friends count
twitter.getCounts('iAmAlphaZz')
.then(data => {
})
.catch(err => {
console.error(err.error);
})
try {
const data = await twitter.getCounts('iAmAlphaZz')
} catch (e) {
throw e
}