Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Dependencies =========================
var
twit = require('twit'),
config = require('./config');
var Twitter = new twit(config);
// RETWEET BOT ==========================
// find latest tweet according the query 'q' in params
var retweet = function() {
var params = {
q: '#nodejs, #Nodejs', // REQUIRED
result_type: 'recent',
lang: 'en'
}
Twitter.get('search/tweets', params, function(err, data) {
// if there no errors
if (!err) {
// grab ID of tweet to retweet
var retweetId = data.statuses[0].id_str;
// Tell TWITTER to retweet
Twitter.post('statuses/retweet/:id', {
id: retweetId
}, function(err, response) {
if (response) {
console.log('Retweeted!!!');
}
// if there was an error while tweeting
if (err) {
console.log('Something went wrong while RETWEETING... Duplication maybe...');
}
});
}
// if unable to Search a tweet
else {
console.log('Something went wrong while SEARCHING...');
}
});
}
// grab & retweet as soon as program is running...
retweet();
// retweet in every 50 minutes
setInterval(retweet, 3000000);
// FAVORITE BOT====================
// find a random tweet and 'favorite' it
var favoriteTweet = function(){
var params = {
q: '#nodejs, #Nodejs', // REQUIRED
result_type: 'recent',
lang: 'en'
}
// find the tweet
Twitter.get('search/tweets', params, function(err,data){
// find tweets
var tweet = data.statuses;
var randomTweet = ranDom(tweet); // pick a random tweet
// if random tweet exists
if(typeof randomTweet != 'undefined'){
// Tell TWITTER to 'favorite'
Twitter.post('favorites/create', {id: randomTweet.id_str}, function(err, response){
// if there was an error while 'favorite'
if(err){
console.log('CANNOT BE FAVORITE... Error');
}
else{
console.log('FAVORITED... Success!!!');
}
});
}
});
}
// grab & 'favorite' as soon as program is running...
favoriteTweet();
// 'favorite' a tweet in every 60 minutes
setInterval(favoriteTweet, 3600000);
// function to generate a random tweet tweet
function ranDom (arr) {
var index = Math.floor(Math.random()*arr.length);
return arr[index];
};
@santosh

This comment has been minimized.

Copy link

commented Jan 7, 2017

Hello @amandeepmittal,

I was developing a twitter bot myself. At one moment I was hung. I was thinking that what would I do if I want two separate things to do with same bot.

If I give an example of this code, what can I do to get a favoritePython() to do same thing for #python and #django?

How can I run both functions simultaneously with setInterval()?

@amandeepmittal

This comment has been minimized.

Copy link
Owner Author

commented Jan 8, 2017

@santosh JS is single threaded. You cannot execute multiple functions at the same time using same setInterval().

The above bot is doing separate things (is finding tweets to retweet & finding tweets to favorite them) but there are two setInterval() functions are used.

@FujiZer0

This comment has been minimized.

Copy link

commented Jul 14, 2017

i am making a bot and how to make hes Commands and Command PREFIXES?

@FujiZer0

This comment has been minimized.

Copy link

commented Jul 14, 2017

how to make my bot online
dude

@hakan526

This comment has been minimized.

Copy link

commented Jun 28, 2019

EYVALAH BİLADER

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.