如何在Bluesky创建线程?修正threadID获取失败导致重复发帖代码
你代码的核心问题是没有在回复记录中添加Bluesky要求的reply字段,这个字段是平台识别帖子所属线程的关键。同时你提供的代码中回复部分的replyRecord未完成,以下是完整的修正代码:
async function postToBluesky(url, text) { try { const summaryText = await summarizeText(text); const rt = new RichText({ text: summaryText }); // 分割文本为250字符以内的片段 const parts = []; while (text.length > 0) { parts.push(text.substring(0, 250)); text = text.substring(250); } // 发布第一篇帖子作为线程根帖 const postRt = new RichText({ text: parts[0] }); const postRecord = { $type: 'app.bsky.feed.post', text: postRt.text, facets: [ { name: 'url', value: url, index: { value: 0, byteStart: 0, byteEnd: postRt.text.length, }, features: [], }, ], createdAt: new Date().toISOString(), }; const rootPost = await agent.post(postRecord); let prevPost = rootPost; // 发布后续片段作为线程回复 for (let i = 1; i < parts.length; i++) { const replyRt = new RichText({ text: parts[i] }); const replyRecord = { $type: 'app.bsky.feed.post', text: replyRt.text, createdAt: new Date().toISOString(), // 关键:添加reply字段指定线程的根帖和父帖 reply: { root: { uri: rootPost.uri, cid: rootPost.cid }, parent: { uri: prevPost.uri, cid: prevPost.cid } } }; const currentReply = await agent.post(replyRecord); // 更新prevPost为当前回复,供下一层回复使用 prevPost = currentReply; } console.log('线程发布完成'); } catch (error) { console.error('发布线程失败:', error); } }
关键修正点说明:
- 添加
reply字段:每个回复帖子必须包含reply对象,其中root指定线程的第一篇帖子(根帖)的uri和cid,parent指定直接父帖子的uri和cid,这是Bluesky识别线程的核心依据。 - 使用正确的标识:Bluesky API返回的帖子对象中,
uri和cid才是用于关联回复的唯一标识,而非你之前使用的id。 - 循环更新父帖:每次发布回复后,将
prevPost更新为当前回复的帖子,确保下一条回复能正确关联到上一层内容。
内容的提问来源于stack exchange,提问作者hithatrail
相关产品推荐
相关产品推荐

