-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (81 loc) · 2.91 KB
/
index.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
// v1.1.1 - 180117
const botr = require('telegram-api/build');
const Twitter = require('twitter');
const parser = require('rss-url-parser')
const fs = require('fs');
const request = require('request');
// Set the headers
const headers = {
'User-Agent': 'Bot/1.0.0',
'Content-Type': 'application/x-www-form-urlencoded'
}
console.log('Started...');
// Configuration
// NODE_ENV=test node ./app.js
const env = process.env.NODE_ENV || 'development' // or 'production'
, configurationFilename = './config.' + env + '.json'
, configuration = require(configurationFilename)
, globalConfiguration = require('./config.global.json')
console.log('. Current:' + env);
configuration.global = globalConfiguration;
//------------------------------------------------------------------------------
const bot = new botr({"token" : configuration.global.token});
ScanNews(); // scan rss at start
bot.start();
if (!configuration.scanTime) {
configuration.scanTime = 60000; // 1 minute
}
setInterval(ScanNews, configuration.scanTime);
var last_title = configuration.global.lastTitle;
//------------------------------------------------------------------------------
function ScanNews() { // Scaning RSS for new records
parser(configuration.global.RSS).then((data) => {
if (last_title !== data[0].title) {
console.log('New record:' + data[0].title);
var poststring = data[0].title + ' ' + data[0].link;
if (data[0].description) {
poststring += ' ' + data[0].description;
}
// Posting to twitter
const client = new Twitter({
consumer_key: configuration.global.twitter.consumer_key,
consumer_secret: configuration.global.twitter.consumer_secret,
access_token_key: configuration.global.twitter.access_token_key,
access_token_secret: configuration.global.twitter.access_token_secret
});
client.post('statuses/update', {status: poststring}, function(error, tweet, response) {
if (!error) {
console.log(tweet);
}
});
// Posting to Telegram channel
/// Remove all html tags
const poststring1 = poststring.replace(/<\/?[^>]+(>|$)/g, "");
/// Configure the request
const options = {
url: 'https://api.telegram.org/bot' + configuration.global.token + '/sendMessage',
method: 'GET',
headers: headers,
qs: {
'chat_id': configuration.global.chatId,
'text': poststring
}
};
/// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.info('body:', body);
last_title = data[0].title;
saveConfig(last_title);
} else {
console.error(error);
}
})
};
})
};
//------------------------------------------------------------------------------
function saveConfig(last_title) { // Saving global config file with new 'last record'
configuration.global.lastTitle = last_title;
fs.writeFileSync('./config.global.json', JSON.stringify(configuration.global, null, '\t'));
}