-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
executable file
·116 lines (103 loc) · 3.1 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// need to use this OTHER twitter lib to post photos, sigh
var conf = require('./config.js');
var Twitter = require('node-twitter');
var twitterRestClient = new Twitter.RestClient(
conf.consumer_key,
conf.consumer_secret,
conf.access_token,
conf.access_token_secret
);
var Twit = require('twit')
var T = new Twit({
consumer_key: conf.consumer_key,
consumer_secret: conf.consumer_secret,
access_token: conf.access_token,
access_token_secret: conf.access_token_secret
})
// Helper function
function cinvirtString(str) {
return str.replace(/[AEOU]/g, 'I')
.replace(/[aeou]/g, 'i')
.replace(/[áéóú]/g, 'í')
.replace(/[àèò]/g, 'ì');
}
// function postSarcasm(tweet) {
// console.log(tweet);
// if (tweet.in_reply_to_user_id_str) {
// // Get the text from the parent tweet
// var text = getParentTweet(tweet.in_reply_to_user_id_str);
// text = cinvirtString(text);
// console.log(text);
// }
// }
// Get the tweet by Id using node-twitter to get the original tweet from 'in_reply_to_status_id_str'
function postSarcasm(tweet) {
// Get parent tweet
// var Tid = tweet.id;
// console.log(tweet);
T.get('statuses/show/:id', { id: tweet.in_reply_to_status_id_str },
function (err, data, response) {
if (!err) {
console.log('Got parent data OK!');
// console.log(data);
// console.log(cinvirtString(data.text));
var text = cinvirtString(data.text);
var url = 'https://twitter.com/' + data.user.screen_name + '/status/' + data.id_str;
// console.log(text);
// console.log(url);
// Post text via response
T.post('statuses/update',
{
status: text + ' ' + url
// status: '@' + data.user.screen_name + ' ' + text ,
// in_reply_to_status_id: data.id_str
}, function(err, data, response) {
console.log('Tweet posted');
// console.log(data);
});
} else {
console.log('Error');
console.log(err);
}
}
);
}
// Main function to offer the service to people
function listenToMasses() {
var twitterStreamClient = new Twitter.StreamClient(
conf.consumer_key,
conf.consumer_secret,
conf.access_token,
conf.access_token_secret
);
if (twitterStreamClient.isRunning())
{
twitterStreamClient.stop();
}
twitterStreamClient.start(['@istipidi']);
twitterStreamClient.on('tweet', function(tweet) {
console.log('A new request is on the way');
// console.log(tweet);
// console.log(tweet);
postSarcasm(tweet);
});
twitterStreamClient.on('close', function() {
console.log('Connection closed.');
});
twitterStreamClient.on('end', function() {
console.log('End of Line.');
});
twitterStreamClient.on('error', function(error) {
console.log('Error: ' + (error.code ? error.code + ' ' + error.message : error.message));
});
}
// Start listening to masses to offer our Sarcasm for free
listenToMasses();
// Challenge 2
// var Tid = '1035490030284881920';
// getParentTweet(Tid);
// Challenge 1
// var strTest = 'Hello Twitter! #myfirstTweet';
// console.log(strTest);
// console.log('---');
// console.log(cinvirtString(strTest));