-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
186 lines (171 loc) · 5.71 KB
/
bot.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { createClient } from '@supabase/supabase-js'
import twit from 'twit'
import { VERIFY_TRUE, VERIFY_FALSE, VERIFY } from './constants.js'
// Create a single supabase client for interacting with your database
const supabase = createClient(
'https://xvtrvphgtmtrarhiicqb.supabase.co',
process.env.SUPABASE_KEY
)
const config = {
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
}
const T = new twit(config)
var stream = T.stream('statuses/filter', { track: ['@covid_src'] })
stream.on('tweet', tweetEvent)
function checkTweetMsgQuery(txt = '') {
if (
txt.includes('verify') &&
(txt.includes('true') || txt.includes('false'))
) {
if (txt.includes('true')) {
return VERIFY_TRUE
} else if (txt.includes('false')) {
return VERIFY_FALSE
}
} else if (txt.includes('verify')) {
return VERIFY
}
return null
}
async function tweetEvent(tweet) {
console.log('tweet', tweet.in_reply_to_status_id_str)
// const tweetId = tweet.id
const parentTweetId = tweet.in_reply_to_status_id_str
const displayText = tweet.text
.slice(tweet.display_text_range[0], tweet.display_text_range[1])
.toLowerCase()
let { data: tweetinfoRecord, error } = await supabase
.from('tweetinfo')
.select('*')
.eq('parent_tweet_id', parentTweetId)
if (tweetinfoRecord.length > 0) {
console.log('tweetinfoRecord', tweetinfoRecord)
// checks which query user has submitted
const cmd = checkTweetMsgQuery(displayText)
if (cmd === VERIFY_TRUE) {
verifyTrue(tweetinfoRecord, parentTweetId, tweet)
} else if (cmd === VERIFY_FALSE) {
verifyFalse(tweetinfoRecord, parentTweetId, tweet)
} else if (cmd === VERIFY) {
sendTweetResponseForVotes(tweet, tweetinfoRecord)
}
} else {
/* INSERTS new record with new data*/
const cmd = checkTweetMsgQuery(displayText)
const { data, error } = await supabase.from('tweetinfo').insert([
{
tweet_id: tweet.id_str,
parent_tweet_id: tweet.in_reply_to_status_id_str,
votes_true: cmd === VERIFY_TRUE ? 1 : 0,
votes_false: cmd === VERIFY_FALSE ? 1 : 0,
users: {
id: tweet.user.id,
name: tweet.user.name,
screen_name: tweet.user.screen_name,
},
},
])
console.log('data', data, cmd)
// send response based on condition
if (data && cmd !== null) {
// console.log('INSERTED NEW TWEET RECORD INTO DB')
sendTweetResponseForNewRecord(tweet)
} else {
console.log(
'Something went wrong: supabase insert operation',
error
)
}
}
}
async function verifyTrue(tweetinfoRecord, parentTweetId, tweet) {
// REGISTER VOTE FOR TRUE VERIFICATION
const { data, error } = await supabase
.from('tweetinfo')
.update({
votes_true: parseInt(
parseInt(tweetinfoRecord[0].votes_true) + parseInt(1)
),
})
.eq('parent_tweet_id', parentTweetId)
if (data) {
// console.log('VERIFY VOTE TO TRUE COUNT', data)
sendTweetResponseForVotes(tweet, data)
} else {
console.log('VERIFY TRUE VOTE ERROR', error)
}
}
async function verifyFalse(tweetinfoRecord, parentTweetId, tweet) {
// REGISTER VOTE FOR FALSE VERIFICATION
const { data, error } = await supabase
.from('tweetinfo')
.update({
votes_false: parseInt(
parseInt(tweetinfoRecord[0].votes_false) + parseInt(1)
),
})
.eq('parent_tweet_id', parentTweetId)
if (data) {
// console.log('VERIFY VOTE TO FALSE COUNT')
sendTweetResponseForVotes(tweet, data)
} else {
console.log('VERIFY FALSE VOTE ERROR', error)
}
}
function sendTweetResponseForVotes(tweet, data) {
let name = tweet.user.screen_name
let nameID = tweet.id_str
let diff = data[0].votes_true - data[0].votes_false
// Start a reply back to the sender
let reply =
'@' +
name +
' ' +
'Votes for this verified resource - YES: ' +
data[0].votes_true +
' NO: ' +
data[0].votes_false +
' \nOverall Verification result: ' +
diff
let params = {
status: reply,
in_reply_to_status_id: nameID,
}
T.post('statuses/update', params, function (err, data, response) {
if (err !== undefined) {
console.log('tweet reply error: ', err)
} else {
console.log('Tweeted: ' + params.status)
}
})
}
function sendTweetResponseForNewRecord(tweet) {
const name = tweet.user.screen_name
const nameID = tweet.id_str
const displayText = tweet.text.slice(
tweet.display_text_range[0],
tweet.display_text_range[1]
)
// Start a reply back to the sender
let reply =
'@' +
name +
' ' +
'No votes yet. You can vote now by replying in parent tweet with "verify true" or "verify false" '
let params = {
status: reply,
in_reply_to_status_id: nameID,
}
if (displayText.length < 25) {
T.post('statuses/update', params, function (err, data, response) {
if (err !== undefined) {
console.log('tweet reply error: ', err)
} else {
console.log('Tweeted: ' + params.status)
}
})
}
}