-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
72 lines (57 loc) · 2.09 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
require('dotenv').config()
const cron = require('node-cron')
const { tweet } = require('./utils/twitter')
const { getActivity } = require('./utils/farcaster')
const FARCASTER_USERNAME = process.env.FARCASTER_USERNAME
let previousLastCast = new Date().getTime()
const devEnv = process.env.NODE_ENV === 'development'
const cronSchedule = devEnv ? '*/10 * * * * *' : '*/2 * * * *'
// Check activity every 2 mins (or 10 secs in dev mode)
cron.schedule(cronSchedule, async () => {
const casts = await getActivity(FARCASTER_USERNAME).then((res) => {
// Only return 10 recent casts to save bandwidth
return res.slice(0, 10)
})
// Get all parent casts (ignore replies, recasts, deletes)
const parentCasts = casts.filter((cast) => {
if (
cast.body.data.replyParentMerkleRoot ||
cast.body.data.text.startsWith('recast:') ||
cast.body.data.text.startsWith('delete:')
) {
return false
} else {
return true
}
})
const lastCast = parentCasts[0].body.publishedAt
const newCasts = parentCasts.filter((cast) => {
return cast.body.publishedAt > previousLastCast
})
if (newCasts.length === 0) return
const isOptIn = process.env.OPT_IN == 'true'
const optInMessage = process.env.OPT_IN_MESSAGE
const sentFromFc = process.env.SENT_FROM_FARCASTER == 'true'
// Handle new casts
newCasts.forEach((cast) => {
let text = cast.body.data.text
// Ignore @ mentions
if (text.match(/@[^\s]/g)) {
return console.log('New cast but it includes a mention -- ignoring')
}
// If opt-in is enabled, ignore casts that don't end with the message
if (isOptIn && !text.endsWith(optInMessage)) {
return console.log("New cast but it doesn't opt-in -- ignoring")
}
// If opt-out is enabled, ignore casts that end with the message
if (!isOptIn && text.endsWith(optInMessage)) {
return console.log('New cast but it opts-out -- ignoring')
}
if (sentFromFc && text.length < 250) {
text = `${text}\n\n - Sent from Farcaster`
}
tweet(text)
})
// Set the previous last cast date
previousLastCast = lastCast
})