-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
189 lines (164 loc) · 6.88 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
187
188
189
import { TwitterApi, EDirectMessageEventTypeV1 } from 'twitter-api-v2';
import { TwitterApiRateLimitPlugin } from '@twitter-api-v2/plugin-rate-limit'
import dotenv from 'dotenv';
dotenv.config();
const API_KEY = process.env.API_KEY;
const API_KEY_SECRET = process.env.API_KEY_SECRET;
const BEARER_TOKEN = process.env.BEARER_TOKEN;
const ACCESS_TOKEN = process.env.ACCESS_TOKEN;
const ACCESS_TOKEN_SECRET = process.env.ACCESS_TOKEN_SECRET;
// Instanciate with desired auth type (here's Bearer v2 auth)
const rateLimitPlugin = new TwitterApiRateLimitPlugin()
const twitterClient = new TwitterApi({
appKey: API_KEY,
appSecret: API_KEY_SECRET,
accessToken: ACCESS_TOKEN, // oauth token from previous step (link generation)
accessSecret: ACCESS_TOKEN_SECRET, // oauth token secret from previous step (link generation)
}, {plugins: [rateLimitPlugin]});
// Tell typescript it's a readonly app
// const roClient = twitterClient.readWrite;
const rwClient = twitterClient.readWrite
let myUserId;
let getId = async () => {
if(myUserId) {
return myUserId;
}
let thisUser = await twitterClient.v2.me();
myUserId = thisUser.data.id;
}
let setWelcomeDM = async () => {
console.log('setting welcome DM');
const welcomeDm = await twitterClient.v1.newWelcomeDm('BirdBuds welcome message', { text: 'Thanks for signing up for BirdBuds! Expect new matches in your inbox soon <3' });
// This will handle all the boilerplate for you:
await twitterClient.v1.setWelcomeDm(welcomeDm[EDirectMessageEventTypeV1.WelcomeCreate].id);
};
let doTheThing = async () => {
console.log('------------------------')
console.log('Checking again at ' + new Date());
// GOAL: Get bot to follow me when I follow it
await getId();
// get the list of people following my account
console.log('Getting list of people following me...');
let pplFollowingMe = await twitterClient.v2.followers(myUserId, {
max_results: 200,
asPaginator: true
});
const allPplFollowingMe = await pplFollowingMe.fetchLast(400);
// paginate through the list of people following me
let followingMe = [];
// go through itereator
for await (const page of pplFollowingMe) {
// add each page to the followingMe array
followingMe = followingMe.concat(page);
}
pplFollowingMe = {data: followingMe};
console.log('Got list of people following me.');
console.log(`${pplFollowingMe.data.length} people following me.`);
console.log('Getting list of people I follow...');
let pplIFollow = await twitterClient.v2.following(myUserId, {
max_results: 200,
asPaginator: true
});
const allpplIFollow = await pplIFollow.fetchLast(400);
// paginate through the list of people I follow
let iFollow = [];
// go through itereator
for await (const page of pplIFollow) {
// add each page to the followingMe array
// console.log(page);
iFollow = iFollow.concat(page);
}
pplIFollow = {data: iFollow};
console.log('Got list of people I follow.');
console.log(`${pplIFollow.data.length} people I follow.`);
// console.log(pplIFollow.data);
// console.log(pplFollowingMe.data);
let userIDs = pplFollowingMe.data.map(person => person.id);
let userIDsIFollow = pplIFollow.data.map(person => person.id);
if(userIDs.length === 200 || userIDs.length === 0) {
console.log('WARNING: 200 or 0 users following me. This may not be all of them.');
return;
}
else if (userIDsIFollow.length === 200 || userIDsIFollow.length === 0) {
console.log('WARNING: 200 or 0 users I follow. This may not be all of them.');
return;
}
// find all userIDs I don't follow that are following me
let newFollowersIDs = userIDs.filter(id => !userIDsIFollow.includes(id));
// find all userIDs I follow that are not following me
let churnedUsers = userIDsIFollow.filter(id => !userIDs.includes(id));
console.log('Churned Users: ', churnedUsers);
// follow all of them
for (let userToFollowId of newFollowersIDs) {
console.log(`Following user ${userToFollowId}`);
const IGNORED = ["1931065754"];
if(IGNORED.includes(userToFollowId)) {
console.log('Ignoring user');
continue;
}
//TODO: account for private accounts
let followingResponse = await twitterClient.v2.follow(myUserId, userToFollowId);
// send intro DM:
if(followingResponse.data.following) {
await twitterClient.v1.sendDm({
event: EDirectMessageEventTypeV1.DirectMessageEvents,
recipient_id: userToFollowId,
text: "Thanks for joining BirdBuds! If you''re seeing this for the second time, it means we need you to log back in :) Please complete signup here:",
ctas: [{
type: 'web_url',
url: `https://app.birdbuds.com/v2/login?id=${userToFollowId}`,
label: 'Complete Signup',
}]
});
//get rate limit
let rateLimit = await rateLimitPlugin.v2.getRateLimit('users/:id/following');
console.log(`Rate limit: ${rateLimit.limit ? rateLimit.remaining + '/' + rateLimit.limit : 'None.'}`);
console.log(followingResponse);
if(followingResponse.data.errors) {
console.log('Error following user');
console.log(followingResponse.data.errors);
}
else {
console.log(followingResponse?.data?.following ? 'Successfully followed user' : 'Error following user');
}
}
}
// unfollow all of them
for (let userToUnfollowId of churnedUsers) {
console.log(`Unfollowing user ${userToUnfollowId}`);
let unfollowingResponse = await twitterClient.v2.unfollow(myUserId, userToUnfollowId);
//get rate limit
let rateLimit = await rateLimitPlugin.v2.getRateLimit('users/:id/following');
console.log(`Rate limit: ${rateLimit.limit ? rateLimit.remaining + '/' + rateLimit.limit : 'None.'}`);
console.log(unfollowingResponse);
if(unfollowingResponse.data.errors) {
console.log('Error unfollowing user');
console.log(unfollowingResponse.data.errors);
}
else {
console.log(unfollowingResponse?.data?.following ? 'Error unfollowing user' : 'Successfully unfollowed user');
}
}
if(newFollowersIDs.length == 0) {
console.log('No new followers.');
}
}
try {
//do the thing every 1 mins
let doTheThingSafely = () => {
try{
doTheThing().catch((e) => {
console.log(e);
console.log(e?.data?.errors);
});
} catch (e) {
console.log(e);
}
};
setInterval(doTheThingSafely, 1000 * 60);
// doTheThingSafely();
// setWelcomeDM();
}
catch (e) {
console.log(e);
}