-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexhortationserver.js
executable file
·131 lines (116 loc) · 3.69 KB
/
exhortationserver.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
#!/usr/bin/env node
/* global process __dirname */
var Twit = require('twit');
var config = require('./config');
var createExhorter = require('./exhorter');
var tributeDemander = require('./tributedemander');
var Chronicler = require('basicset-chronicler').createChronicler;
var behavior = require('./behaviorsettings');
var canIChimeIn = require('can-i-chime-in')({
extraWordsToAvoid: [
'fascist',
'fascism',
'internment',
'nazi',
'nazis',
'autistic',
'palsy'
]
});
var createNounfinder = require('nounfinder');
var figurePicker = require('./figurepicker');
var prepPhrasePicker = require('./prepphrasepicker');
var probable = require('probable');
var log = require('./logger').info;
var dryRun = process.argv[2] === '--dry';
if (dryRun) {
require('longjohn');
}
log('The exhortation server is running.');
var chronicler = Chronicler({
dbLocation: __dirname + '/data/tributes.db'
});
var nounfinder = createNounfinder({
wordnikAPIKey: config.wordnikAPIKey
});
var twit = new Twit(config.twitter);
var stream = twit.stream('user');
var maxCommonnessForTopic =
behavior.maxCommonnessForReplyTopic[0] +
probable.roll(
behavior.maxCommonnessForReplyTopic[1] -
behavior.maxCommonnessForReplyTopic[0]
);
var maxCommonnessForImageTopic =
behavior.maxCommonnessForImageTopic[0] +
probable.roll(
behavior.maxCommonnessForImageTopic[1] -
behavior.maxCommonnessForImageTopic[0]
);
var exhorterOpts = {
chronicler: chronicler,
behavior: behavior,
canIChimeIn: canIChimeIn,
nounfinder: nounfinder,
tributeDemander: tributeDemander,
prepPhrasePicker: prepPhrasePicker,
figurePicker: figurePicker,
decorateWithEmojiOpts: tributeDemander.decorateWithEmojiOpts,
maxCommonnessForTopic: maxCommonnessForTopic,
maxCommonnessForImageTopic: maxCommonnessForImageTopic,
// TODO: Make this a function that does a coin flip to decide if this should
// be 1 or 2 per instance.
nounCountThreshold: 1,
w2vNeighborChance: behavior.w2vNeighborChance
};
log('maxCommonnessForTopic:', maxCommonnessForTopic);
log('maxCommonnessForImageTopic', maxCommonnessForImageTopic);
var exhorter = createExhorter(exhorterOpts);
stream.on('tweet', function respondToTweet(tweet) {
exhorter.getExhortationForTweet(tweet, tweetExhortation);
});
function tweetExhortation(error, tweet, exhortation, topics) {
log('exhortation:', exhortation);
if (error) {
log('Error from getExhortationForTweet:', error);
} else if (!exhortation || exhortation.length < 1) {
log('No error, but got nothing from getExhortationForTweet. Tweet:', tweet);
} else {
setTimeout(
function doPost() {
if (dryRun) {
console.log('\n');
console.log('TWEET', tweet.text);
console.log('RESPONSE', exhortation);
console.log('\n');
} else {
twit.post(
'statuses/update',
{
status: exhortation,
in_reply_to_status_id: tweet.id_str
},
function recordTweetResult(error) {
if (error) {
log(error);
} else {
recordReplyDetails(tweet, topics);
log('Replied to status', tweet.text, 'with :', exhortation);
}
}
);
}
},
// Vary the delay until the response from 0 to 30 seconds.
probable.roll(6) * 5 * 1000
);
}
}
function recordReplyDetails(targetStatus, topics) {
var userId = targetStatus.user.id_str;
chronicler.recordThatTweetWasRepliedTo(targetStatus.id_str);
chronicler.recordThatUserWasRepliedTo(userId);
topics.forEach(function recordTopic(topic) {
chronicler.recordThatTopicWasUsedInReplyToUser(topic, userId);
});
}