-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.js
89 lines (68 loc) · 2.13 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const twit = require('twit')
const config = require('./config.js')
const fs = require('fs')
const path = require('path')
// initializing the whole shabang
const Twitter = new twit(config)
app.use(express.static('public'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// reading from the data
const hashtags = fs.readFile(path.join(__dirname, '/hashtags.json'), "utf8", (err, data) => {
if (err) {
console.log(err)
} else {
init(data)
}
})
// entering the data
retweet = function(data) {
const theData = JSON.parse(data)
theData.hashtags.forEach(item => {
const theHashTag = item.tag
const params = {
q: theHashTag,
result_type: 'recent'
}
// retweet function (inside previous function)
Twitter.get('search/tweets', params, function(err, data) {
// if there no errors
if (!err) {
// grab ID of tweet to retweet
const retweetId = data.statuses[0].id_str
// Tell TWITTER to retweet
Twitter.post('statuses/retweet/:id', {
id: retweetId
}, function(err, response) {
// if there was an error while tweeting
if (err) {
console.log(`no new tweets with ${params.q} in the last minute`)
}
else {
console.log(`tweet with ${params.q} successful`)
}
});
}
// if unable to Search a tweet
else {
console.log('Error searching - try again')
}
})
})
}
// connect to server
app.listen(63217, function() {
console.log('app listening on post 63217')
})
// execute
const init = function(data) {
retweet(data)
setInterval(function(){retweet(data)
}, 70000)
}
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'))
})