-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtweetStreamBearer.js
More file actions
57 lines (46 loc) · 1.38 KB
/
Copy pathtweetStreamBearer.js
File metadata and controls
57 lines (46 loc) · 1.38 KB
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
import { Client } from "twitter-api-sdk";
import axios from 'axios';
import * as dotenv from 'dotenv'
dotenv.config({ path: './config.env' })
/**
* This is a function that listents to tweets containing a keyword
* obtaining the bearer token for the authentication using the OAuth2 API.
*/
async function main() {
const bearerTokenCredentials =
Buffer.from(`${encodeURI(process.env.API_KEY)}:${encodeURI(process.env.API_SECRET_KEY)}`).toString('base64')
const result = await axios.post('https://api.twitter.com/oauth2/token',
'grant_type=client_credentials',
{
headers: {
'Authorization': 'Basic ' + bearerTokenCredentials,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
})
if (result.data.token_type !== 'bearer') {
throw 'BEARER ERROR'
}
const bearerToken = result.data.access_token;
const client = new Client(bearerToken);
await client.tweets.addOrDeleteRules(
{
add: [
{ value: "The Medium tin test", tag: "text_filtered_tag" }
],
// delete: {
// ids: [
// '',
// ]
// }
}
);
const rules = await client.tweets.getRules();
console.log('rules', rules);
const stream = client.tweets.searchStream({
"tweet.fields": ["author_id", "created_at"],
});
for await (const tweet of stream) {
console.log('🔷 TWEET', tweet);
}
}
main();