-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
99 lines (88 loc) · 2.4 KB
/
app.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
var Twitter = require('twitter');
var cron = require('node-cron');
require('dotenv').config();
const consumer_key = process.env.consumer_key;
const consumer_secret = process.env.consumer_secret;
const access_token_key = process.env.access_token_key;
const access_token_secret = process.env.access_token_secret;
var client = new Twitter({
consumer_key,
consumer_secret,
access_token_key,
access_token_secret,
});
const prompt = '@whyweru methali';
function Mhenga(callback)
{
// Node Js spawn function used to run the Mhenga.swh script
const spawn = require("child_process").spawn;
let options = {shell: true};
const swahiliProcess = spawn('swahili',["./mhenga.swh"],options);
let output = '';
// Save the response to a variable
swahiliProcess.stdout.on('data', (chunk) => {
output += chunk.toString();
});
swahiliProcess.on('exit', () => {
callback(output);
});
}
//Function used to post the tweet
function tweeter(tweet_post)
{
client.post('statuses/update', {status: tweet_post}, function(error, tweet, response){
if(error){
console.log(tweet);
console.log("Error Posting Tweet");
}
else{
console.log("Tweet: " + tweet_post + "\n");
console.log(tweet);
console.log("Tweet Posted!");
}
})
}
// Listens for mentions of the twitter profile
function streamer()
{
var stream = client.stream('statuses/filter', {track: prompt});
stream.on('data', function(event) {
console.log(event && event.text);
// Get a methali from the mhenga function
Mhenga(function(output) {
client.post('statuses/update', {status: output, in_reply_to_status_id: event.id}, function(error, tweet, response){
if(error){
console.log(tweet);
console.log("Error Posting Tweet");
}
else{
console.log("Tweet: " + output + "\n");
console.log(tweet);
console.log("Tweet Posted!");
}
})
});
});
stream.on('error', function(error) {
throw error;
});
}
// Post a tweet every 6 hours
cron.schedule('0 0 */6 * * *', () => {
try {
// Function execution
Mhenga(function(output) {
tweeter(output)
console.log(output)
});
} catch (e) {
console.log("Trying different tweet")
// Sleep for a while to change the time
setTimeout(Mhenga(function(output) {
tweeter(output)
console.log(output)
}), 3000);
} finally {
console.log("Completed");
}
});