-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.js
77 lines (68 loc) · 2.43 KB
/
main.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
require('dotenv').config()
const Twit = require('twitter-v2')
const { Client } = require('discord.js');
const client = new Client({ intents: 2048 });
var T = new Twit({
// consumer_key: process.env.TWITTER_CONSUMER_KEY,
// consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
// access_token_key: process.env.TWITTER_ACCESS_TOKEN,
// access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
// timeout_ms: 60*1000, // optional HTTP request timeout to apply to all requests.
// strictSSL: true, // optional - requires SSL certificates to be valid.
bearer_token: process.env.BEARER_TOKEN
})
// //only show owner tweets
async function sendMessage (tweet, client){
console.log(tweet)
const url = "https://twitter.com/user/status/" + tweet.id;
try {
const channel = await client.channels.fetch(process.env.DISCORD_CHANNEL_ID)
channel.send(`${process.env.CHANNEL_MESSAGE} ${url}`)
} catch (error) {
console.error(error);
}
}
async function listenForever(streamFactory, dataConsumer) {
try {
for await (const { data } of streamFactory()) {
dataConsumer(data);
}
// The stream has been closed by Twitter. It is usually safe to reconnect.
console.log('Stream disconnected healthily. Reconnecting.');
listenForever(streamFactory, dataConsumer);
} catch (error) {
// An error occurred so we reconnect to the stream. Note that we should
// probably have retry logic here to prevent reconnection after a number of
// closely timed failures (may indicate a problem that is not downstream).
console.warn('Stream disconnected with error. Retrying.', error);
// listenForever(streamFactory, dataConsumer);
}
}
async function setup () {
const endpointParameters = {
'tweet.fields': [ 'author_id', 'conversation_id' ],
'expansions': [ 'author_id', 'referenced_tweets.id' ],
'media.fields': [ 'url' ]
}
try {
console.log('Setting up Twitter....')
const body = {
"add": [
{"value": "from:"+ process.env.TWITTER_USER_NAME, "tag": "from Me!!"}
]
}
const r = await T.post("tweets/search/stream/rules", body);
} catch (err) {
console.log(err)
}
listenForever(
() => T.stream('tweets/search/stream'),
(data) => sendMessage(data, client)
);
}
// Add above rule
client.login(process.env.DISCORD_TOKEN)
client.on('ready', () => {
console.log('Discord ready')
setup()
})