This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (139 loc) · 5.21 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var aws = require('aws-sdk');
var s3 = new aws.S3({apiVersion: '2006-03-01'});
var async = require('async');
var Twit = require('twit');
var request = require('request');
var settings = require('./settings');
//////////////////////////
// Environment mode
//////////////////////////
var env = 'dev';
//////////////////////////
function processHashtagRules(text) {
for (i = 0; i < settings.rules.hashtags.length; i++) {
for (c = 0; c < settings.rules.hashtags[i].patterns.length; c++) {
if (settings.rules.hashtags[i].patterns[c].exec(text)) {
return settings.rules.hashtags[i].tags.join(" ");
}
}
}
}
var Worker = {};
Worker.s3get = function (bucket, path, callback) {
s3.getObject({
Bucket: bucket,
Key: path
}, function (error, data) {
if (!error) {
callback(null, data.Body);
}
else {
callback(error);
}
});
};
Worker.s3put = function (bucket, path, obj, callback) {
s3.putObject({
Bucket: bucket,
Key: path,
ContentType: 'text/plain',
Body: JSON.stringify(obj)
}, function (error, data) {
if (!error) {
callback(null);
}
else {
callback(error);
}
});
};
Worker.getState = function (callback) {
Worker.s3get(settings[env].s3.bucket, settings[env].s3.path, function (error, data) {
if (!error) callback(null, JSON.parse(data));
else callback(null, settings[env].default_state)
});
};
Worker.askCymon = function (ip, callback) {
request("https://cymon.io/api/nexus/v1/ip/" + ip + "/events", null, function (error, response) {
if (response.statusCode == 200) {
var data = JSON.parse(response.body);
callback(null, "I have " + data.count + " events for https://cymon.io/" + ip);
}
else callback(null, "I do not have any reports for " + ip);
});
};
Worker.respondToTweet = function (tweet, callback) {
var question = tweet.text.toLowerCase().replace("@" + settings[env].twitter.handle, "").trim();
var rx = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/;
var ip = rx.exec(question);
if (ip) {
Worker.askCymon(ip, function (error, answer) {
if (error) callback(error);
else {
// modify text using defined rules
var text = "@" + tweet.user.screen_name + " " + answer;
var mod = processHashtagRules(text);
if (mod && mod.length > 0) text += " " + mod;
callback(null, text);
}
});
}
else callback(null, false);
};
exports.handler = function (event, context) {
// init twitter
var Bot = new Twit(settings[env].twitter.auth);
// get last tweet ID from persistent storage
Worker.getState(function (error, bot_state) {
var num_responses_sent = 0;
var options = {count: 25};
if (bot_state.since_id !== 0) options.since_id = bot_state.since_id;
// get all recent mentions since last tweet ID
Bot.get('statuses/mentions_timeline', options, function (error, tweets, response) {
if (!error) {
tweets.reverse();
async.each(tweets, function (tweet, cb) {
Worker.respondToTweet(tweet, function (error, res) {
if (!error) {
if (res) {
console.log('[NEW TWEET] ' + res);
num_responses_sent++;
// tweet back with response
Bot.post('statuses/update', {status: res, in_reply_to_status_id: tweet.id_str}, cb);
//cb(null, true); //dev
}
else cb(null);
}
else {
cb(error);
}
});
},
function (error) {
if (tweets.length) {
// update state file
bot_state.since_id = tweets[tweets.length - 1].id_str;
Worker.s3put(settings[env].s3.bucket, settings[env].s3.path, bot_state, function (error) {
if (!error) {
// done
context.succeed('Replied to ' + num_responses_sent + ' tweets (out of ' + tweets.length + ')');
}
else {
// failed
context.fail("Failed to save bot state: " + error);
}
});
}
else {
// done
context.succeed('No new tweets found.');
}
});
}
else {
// failed
context.fail("Twitter API call failed: " + error);
}
})
});
};