forked from malfynnction/AltText-Tweeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-reply-text.js
82 lines (70 loc) · 2.25 KB
/
get-reply-text.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
const getTweet = require('./get-tweet')
const read = require('./read-alt-text')
module.exports = (tweet) => {
const mentioning_id = tweet.id_str
const mentioning_user = tweet.user.screen_name
let content = `@${mentioning_user} `
return new Promise(async (resolve) => {
try {
if (!tweet) {
console.log('🚨This should not happen🚨')
throw 'No tweet found'
}
// do not reply to retweets
if (typeof tweet.retweeted_status !== 'undefined') {
resolve()
}
let original_id = tweet.in_reply_to_status_id_str
let original_user = tweet.in_reply_to_screen_name
//ppl posting the pic and triggering the bot within the same tweet
if (original_id == null) {
original_id = mentioning_id
original_user = mentioning_user
}
//pls no loops of death!
if (mentioning_user == 'get_altText') {
resolve()
}
const original_tweet = await getTweet(original_id)
//media in the original tweet
if (
original_tweet.extended_entities &&
original_tweet.extended_entities['media']
) {
content += read(original_tweet, original_user)
resolve(content)
}
//media in the triggering tweet
if (tweet.extended_entities && tweet.extended_entities['media']) {
content += read(tweet, mentioning_user)
resolve(content)
}
//in the quoted tweet?
if (tweet.is_quote_status) {
const quoted_tweet = await getTweet(tweet.quoted_status_id_str)
//media in the quoted tweet
if (
quoted_tweet.extended_entities &&
quoted_tweet.extended_entities['media']
) {
content += read(quoted_tweet, quoted_tweet.user.screen_name)
resolve(content)
} else {
//no media in quoted tweet => no tweet
resolve()
}
}
resolve()
} catch (err) {
console.log(err)
if (err.length === 1) {
resolve(content + err[0].message.replace('you are', 'I am'))
} else {
resolve(
content +
'There has been an error while trying to read the alt text, please try again later – @malfynnction, you should probably look into this!'
)
}
}
})
}