-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsky.ts
115 lines (96 loc) · 3.23 KB
/
bsky.ts
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
import { BskyAgent } from "@atproto/api";
import { createBskyThread } from "./lib/bsky-thread";
export const initializeBskyAgent = async () => {
const agent = new BskyAgent({
service: "https://bsky.social",
});
await agent.login({
identifier: process.env.BSKY_HANDLE ?? "",
password: process.env.BSKY_PASSWORD ?? "",
});
return agent;
};
export const sendBlueskyPost = async (content: string) => {
const agent = await initializeBskyAgent();
try {
const response = await agent.api.com.atproto.repo.createRecord({
collection: "app.bsky.feed.post",
repo: "duca.dev",
record: {
text: content,
createdAt: new Date().toISOString(),
},
});
console.log({ response });
if (response.success) {
console.log("Post successfully sent to Bluesky");
} else {
console.error("Failed to send post to Bluesky", response);
}
} catch (error) {
const errorMessage = (error as Error).message;
throw new Error(errorMessage);
}
};
export async function postBskyThread(text: string, bskyClient: BskyAgent) {
const threadParts = createBskyThread(text);
let rootPostUri: string | null = null;
let rootPostCid: string | null = null;
let previousPostUri: string | null = null;
let previousPostCid: string | null = null;
for (const [index, part] of threadParts.entries()) {
console.log(`Posting part ${index + 1}/${threadParts.length}: "${part}"`);
const record: {
text: string;
reply?: {
root: { uri: string; cid: string };
parent: { uri: string; cid: string };
};
} = {
text: part,
};
if (rootPostUri && rootPostCid && previousPostUri && previousPostCid) {
record.reply = {
root: { uri: rootPostUri, cid: rootPostCid },
parent: { uri: previousPostUri, cid: previousPostCid },
};
console.log(`Replying to parent post: ${previousPostUri}`);
} else if (rootPostUri && rootPostCid) {
// For the first reply, the parent is also the root
record.reply = {
root: { uri: rootPostUri, cid: rootPostCid },
parent: { uri: rootPostUri, cid: rootPostCid },
};
console.log(`Replying to root post: ${rootPostUri}`);
} else {
console.log("This is the first post in the thread.");
}
console.log("Record being sent:", JSON.stringify(record, null, 2));
const response = await bskyClient.api.com.atproto.repo.createRecord({
collection: "app.bsky.feed.post",
repo: "duca.dev",
record: {
text: record.text,
reply: record.reply,
createdAt: new Date().toISOString(),
},
});
console.log("Response received:", response);
const isResponseSuccessful = response.success && response.data;
if (isResponseSuccessful) {
const { uri, cid } = response.data;
console.log(`Post created successfully with URI: ${uri}`);
if (!rootPostUri || !rootPostCid) {
rootPostUri = uri;
rootPostCid = cid;
console.log(`Root post set to URI: ${rootPostUri}`);
}
previousPostUri = uri;
previousPostCid = cid;
} else {
console.error("Failed to create post in the thread", response);
break;
}
}
console.log("Thread posting complete.");
}