├── .gitignore ├── config.default.js ├── .gitattributes ├── package.json ├── app.js └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | config.js 3 | -------------------------------------------------------------------------------- /config.default.js: -------------------------------------------------------------------------------- 1 | // in your terminal, write > cp config.default.js config.js and change the following values 2 | // get these keys in https://apps.twitter.com/app/new 3 | 4 | module.exports = { 5 | consumer_key: '', 6 | consumer_secret: '', 7 | access_token_key: '', 8 | access_token_secret: '' 9 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "twitter-bot", 3 | "version": "1.0.0", 4 | "description": "Nodejs Twitter Bot", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node app.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/bmorelli25/Twitter-Bot.git" 13 | }, 14 | "author": "Brandon Morelli", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/bmorelli25/Twitter-Bot/issues" 18 | }, 19 | "homepage": "https://github.com/bmorelli25/Twitter-Bot#readme", 20 | "dependencies": { 21 | "twitter": "^1.7.0" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const Twitter = require('twitter'); 2 | const config = require('./config.js'); 3 | const T = new Twitter(config); 4 | 5 | // Set up your search parameters 6 | const params = { 7 | q: '#nodejs', 8 | count: 10, 9 | result_type: 'recent', 10 | lang: 'en' 11 | } 12 | 13 | // Initiate your search using the above paramaters 14 | T.get('search/tweets', params, (err, data, response) => { 15 | // If there is no error, proceed 16 | if(err){ 17 | return console.log(err); 18 | } 19 | 20 | // Loop through the returned tweets 21 | const tweetsId = data.statuses 22 | .map(tweet => ({ id: tweet.id_str })); 23 | 24 | tweetsId.map(tweetId => { 25 | T.post('favorites/create', tweetId, (err, response) => { 26 | if(err){ 27 | return console.log(err[0].message); 28 | } 29 | 30 | const username = response.user.screen_name; 31 | const favoritedTweetId = response.id_str; 32 | console.log(`Favorited: https://twitter.com/${username}/status/${favoritedTweetId}`); 33 | 34 | }); 35 | }); 36 | }) 37 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Twitter Favorite Bot 2 | 3 | Read all about this project and follow the tutorial on Medium: [Build a simple Twitter Bot with Node.js in just 38 lines of code](https://codeburst.io/build-a-simple-twitter-bot-with-node-js-in-just-38-lines-of-code-ed92db9eb078) 4 | 5 | This bot returns 10 tweets for a specified search query then favorites each of the returned tweets. 6 | 7 | --- 8 | 9 | ### How to use 10 | * Star and download the repository 11 | * run `npm install` to install the needed dependencies 12 | * Copy the file `config.default.js`, rename it to `config.js` and then change its values - this file will store the configuration details for the Twitter API. 13 | 14 | The structure should be the following: 15 | ```js 16 | module.exports = { 17 | consumer_key: '', 18 | consumer_secret: '', 19 | access_token_key: '', 20 | access_token_secret: '' 21 | } 22 | ``` 23 | 24 | * Visit the [Twitter API](https://apps.twitter.com/app/new) and fill out the form. When done, click on the _Keys and Access Tokens_ tab to view your consumer key/secret and access token key/secret. Copy these keys/secrets into your `config.js` file. 25 | * In `app.js` you can edit the `params` variable to determine what to search for: 26 | ``` 27 | var params = { 28 | q: 'SEARCH_QUERY_HERE', //search query 29 | count: 10, //number of tweets to return 30 | result_type: 'recent', //shows recent tweets 31 | lang: 'en' //language English 32 | } 33 | ``` 34 | 35 | * Now we're ready! Open up the command prompt and type `npm start` to run the application. 36 | --------------------------------------------------------------------------------