-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitterBot.js
64 lines (49 loc) · 1.73 KB
/
twitterBot.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
require('dotenv').config()
const request = require('request');
const twit = require('twit');
const config = {
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
}
const personal_token = process.env.personal_token
const Twitter = new twit(config);
const userID = "102773464" // @NYCASP
const stream = Twitter.stream('statuses/filter', {follow: [userID]})
stream.on('tweet', function (tweet) {
if(tweet.user.id_str === userID){
let tweetId = tweet.id_str
let tweetBody = tweet.truncated ? tweet.extended_tweet.full_text : tweet.text
tweetBody = tweetBody.replace("RT @NYCASP: ", "")
let suspended = tweetBody.includes("suspended")
sendStatus(tweetBody, suspended)
if(suspended) {
retweet(tweetId)
}
}
});
const retweet = function(id) {
Twitter.post('statuses/retweet/:id', {id: id}, function(err, res) {
if(res){
console.log(`Successfully Retweeted "${res.text}"`)
} else {
console.log(error.message)
}
})
}
const sendStatus = function(body, suspended) {
request.post(
"https://alternate-side-twilio.herokuapp.com/status",
{
'auth': {'bearer': personal_token},
'json': {'status': {'body': body, 'suspended': suspended}}
}, function(error, response, body) {
console.log("Error "+error)
console.log("Response "+response.statusCode)
console.log("Body "+body)
}
)
}
// Prevent Heroku from falling asleep
setInterval(() => console.log('Listening...'), 1740000)