From e43174e892a1f9ad4781a435db683bfd9272e0fc Mon Sep 17 00:00:00 2001 From: spences10 Date: Wed, 17 May 2017 14:47:46 +0100 Subject: [PATCH] =?UTF-8?q?RT=20functionality=20=F0=9F=90=A4=F0=9F=90=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/api/retweet.js | 34 ++++++++++++++++++++++++++++++++++ src/bot.js | 15 +++++---------- src/config.js | 17 +++++++++++++---- 4 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 src/api/retweet.js diff --git a/README.md b/README.md index a613c71..1c52b02 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ TODO: - [ ] Follow the community hashtag and retweet tweets to it - [ ] Welcome new community members summarising their skills and who they'd like to pair with -- [ ] Retweet specific keywords "pair programming", "anyone want to pair with me" +- [x] Retweet specific keywords "pair programming", "anyone want to pair with me" - [ ] Reply to those specific keywords promoting PairHub diff --git a/src/api/retweet.js b/src/api/retweet.js new file mode 100644 index 0000000..53d8717 --- /dev/null +++ b/src/api/retweet.js @@ -0,0 +1,34 @@ +const Twit = require('twit') +const unique = require('unique-random-array') +const config = require('../config') + +const param = config.twitterConfig +const queryString = unique(param.queryString.split(',')) + +const bot = new Twit(config.twitterKeys) + +const retweet = () => { + let query = queryString() + bot.get('search/tweets', { + q: query, + result_type: param.resultType, + lang: param.language, + filter: 'safe' + }, (err, data) => { + // grab tweet ID to retweet + let retweetId = data.statuses[0].id_str + + if (err) console.log('ERRORDERP: Cannot Search Tweet!') + + bot.post('statuses/retweet/:id', { + id: retweetId + }, (err, response) => { + if (err) { + console.log('ERRORDERP: Retweet!') + } + console.log(data.statuses[0].text, 'SUCCESS: RT') + }) + }) +} + +module.exports = retweet diff --git a/src/bot.js b/src/bot.js index 81787f0..cfdb20e 100644 --- a/src/bot.js +++ b/src/bot.js @@ -1,14 +1,9 @@ const Twit = require('twit') const config = require('./config') -const bot = new Twit(config) +const bot = new Twit(config.twitterKeys) -bot.post('statuses/update', { - status: 'hello world!' -}, (err, data, response) => { - if (err) { - console.log(err) - } else { - console.log(`${data.text} tweeted!`) - } -}) +const retweet = require('./api/retweet') + +retweet() +setInterval(retweet, config.twitterConfig.retweet) diff --git a/src/config.js b/src/config.js index f687ce5..f0f95d8 100644 --- a/src/config.js +++ b/src/config.js @@ -1,8 +1,17 @@ require('dotenv').config() module.exports = { - consumer_key: process.env.CONSUMER_KEY, - consumer_secret: process.env.CONSUMER_SECRET, - access_token: process.env.ACCESS_TOKEN, - access_token_secret: process.env.ACCESS_TOKEN_SECRET, + twitterKeys: { + consumer_key: process.env.CONSUMER_KEY, + consumer_secret: process.env.CONSUMER_SECRET, + access_token: process.env.ACCESS_TOKEN, + access_token_secret: process.env.ACCESS_TOKEN_SECRET + }, + twitterConfig: { + queryString: process.env.QUERY_STRING, + resultType: process.env.RESULT_TYPE, + language: process.env.LANG, + username: process.env.TWITTER_USERNAME, + retweet: process.env.TWITTER_RETWEET_RATE * 1000 * 60 + } }