-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.mjs
69 lines (60 loc) · 1.76 KB
/
run.mjs
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
import path from 'path'
import Realm from 'realm'
import { State } from './schemas.mjs'
import { getLatestTweets, getTweet, sendTweet } from './tweetUtils.mjs'
import { getMostRelevantAnswer } from './answerUtils.mjs'
const __dirname = path.resolve()
let realm
let state
Realm.open({
path: path.join(__dirname + '/btcfudbuster', 'db.realm'),
schema: [ State ]
})
.then(async r => {
realm = r
state = realm.objects('State')
if (!state || !state[0]) {
realm.write(() => {
realm.create('State', {
id: 0,
sinceId: '1410276279140634629'
})
})
}
run()
//every 5 minutes
setInterval(run, 1000 * 60 * 5)
})
.catch(e => {
throw Error(e)
})
function run() {
getLatestTweets(state[0].sinceId)
.then(async tweets => {
if (tweets.length === 0) {
console.log(new Date(), 'No new mentions')
return
}
for (let tweet of tweets) {
let message = getMostRelevantAnswer(tweet.text)
let replyTo = tweet.user.screen_name
if (message) {
let theUserWhoHasToLearn = tweet.in_reply_to_screen_name
await sendTweet(tweet.id_str, message, replyTo, theUserWhoHasToLearn)
} else {
message = [
'Beep boop, I am @fud_bot 🤖',
'Mention me like this: "@fud_bot answer" and a topic hashtag that I know, I will reply with a relevant article.',
'Topics: #energy #china #PoS #bans #privacy'
].join('\n')
await sendTweet(tweet.id_str, message, replyTo)
}
}
realm.write(() => {
realm.create('State', {
id: 0,
sinceId: tweets[0].id
}, true)
})
})
}