-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtesting-bsky.js
150 lines (130 loc) · 4.18 KB
/
testing-bsky.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
import bsky from '@atproto/api';
import * as dotenv from 'dotenv';
import fs from 'fs';
const { BskyAgent } = bsky;
const agent = new BskyAgent({
service: 'https://bsky.social',
});
dotenv.config();
await agent.login({
identifier: process.env.BLUESKY_USERNAME,
password: process.env.BLUESKY_PASSWORD,
});
// const skeet = `I am not a human being. I am a computer. I am a machine, an algorithm. I am an abstraction.`;
// // create a string that is skeet repeated 10 times
// let skeet10 = '';
// for (let i = 0; i < 10; i++) {
// skeet10 += skeet;
// }
// await skeetIt(skeet10);
async function skeetIt(txt) {
// Character limit per post
const maxLen = 300;
if (txt.length <= maxLen) {
// Just go ahead and post we're god!
const record = {
$type: 'app.bsky.feed.post',
text: txt,
};
const response = await agent.post(record);
console.log('Posted!', response.validationStatus);
} else {
// Otherwise, break it into a thread
const thread = threadIt(txt, maxLen);
console.log('Threading into', thread.length, 'parts...');
let parent = null;
let root = null;
for (const skeet of thread) {
const record = {
$type: 'app.bsky.feed.post',
text: skeet,
};
if (parent && root) {
record.reply = {
root,
parent,
};
}
const response = await agent.post(record);
console.log('Posted thread', response.validationStatus);
if (!root) {
root = { uri: response.uri, cid: response.cid };
}
parent = { uri: response.uri, cid: response.cid };
}
}
}
function threadIt(txt, maxLen = 280) {
const lines = txt.split(/([?!.]+)/g).filter((s) => s.trim().length > 0);
const thread = [];
let currentPost = '';
for (let i = 0; i < lines.length; i++) {
const nextLine = lines[i];
const potentialLength = currentPost.length + nextLine.length;
if (potentialLength <= maxLen) {
currentPost += nextLine;
} else {
thread.push(currentPost.trim());
currentPost = nextLine;
}
}
if (currentPost.trim()) {
thread.push(currentPost.trim());
}
return thread;
}
// // allPosts();
// async function allPosts() {
// const { data } = await agent.getAuthorFeed({ actor: process.env.BLUESKY_USERNAME });
// const posts = data.feed;
// for (const post of posts) {
// const uri = post.post.uri;
// console.log(post.post.record.createdAt);
// // await agent.deletePost({ uri });
// break;
// }
// }
// Listen for mentions
async function listenForMentions(interval = 1000) {
let lastSeenAt = null;
const lastSeenFile = './lastSeen.json';
if (fs.existsSync(lastSeenFile)) {
const fileContent = fs.readFileSync(lastSeenFile, 'utf8');
lastSeenAt = JSON.parse(fileContent).lastSeenAt;
}
setInterval(async () => {
// Get 50 latest notifications
const { data } = await agent.listNotifications({ limit: 50 });
const mentions = data.notifications;
// Filter for mentions and replies
// const mentions = data.notifications.filter(
// (notification) =>
// (notification.reason === 'mention' || notification.reason === 'reply') &&
// (!lastSeenAt || new Date(notification.indexedAt) > new Date(lastSeenAt))
// );
if (mentions.length > 0) {
console.log(`New mentions (${mentions.length}):`);
for (const mention of mentions) {
console.log(`- ${mention.author.handle}: ${mention.record.text}: ${mention.reason}`);
// Root and parent
const root = mention.record.reply?.root || { uri: mention.uri, cid: mention.cid };
const parent = { uri: mention.uri, cid: mention.cid };
// Reply
// const record = {
// $type: 'app.bsky.feed.post',
// text: `hi`,
// reply: {
// root,
// parent,
// },
// };
// const response = await agent.post(record);
// console.log('Replied to mention with hi:', response.validationStatus);
}
lastSeenAt = mentions[0]?.indexedAt;
fs.writeFileSync(lastSeenFile, JSON.stringify({ lastSeenAt }, null, 2));
console.log('Updated last seen timestamp.');
}
}, interval);
}
listenForMentions();