Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
如何使用 JavaScript 在 Bluesky 上发布带有嵌入卡的链接_创想鸟

如何使用 JavaScript 在 Bluesky 上发布带有嵌入卡的链接

如何使用 javascript 在 bluesky 上发布带有嵌入卡的链接

随着 bluesky 的不断流行,更多的工具正在围绕它开发。最流行的应用程序之一是后期调度和自动化。

但是,bluesky 的 api 目前不提供直接发布 opengraph 卡片链接的方法。对于想要共享具有有吸引力预览的链接的用户来说,这可能是一个挑战。

在本教程中,我们将向您展示如何使用 javascript 在 bluesky 上发布带有嵌入卡的链接。此方法可以解决 api 限制,让您更有效地共享链接。

让我们开始吧!

立即学习“Java免费学习笔记(深入)”;

使用 javascript api 在 bluesky 上发帖

使用 bluesky api 非常简单。文档非常好。首先,我们需要从 npm 安装 @atproto/api 包:

npm install @atproto/api

接下来,我们创建 bluesky agent 的实例并使用您的 bluesky 凭据登录。

我建议为您的 bluesky 帐户创建一个新的应用程序密码,而不是使用您的主密码。这将使您在需要时更容易撤销访问权限并确保您的主帐户安全。另请确保在项目中设置 bluesky_username 和 bluesky_password 环境变量。

import { atpagent } from "@atproto/api"const getblueskyagent = async () => {  const agent = new atpagent({    service: "https://bsky.social",  })  await agent.login({    identifier: process.env.bluesky_username!,    password: process.env.bluesky_password!,  })  return agent}

一旦你有了代理,你就可以用它来发布到 bluesky,这非常简单。

/** * send a post to bluesky * @param text - the text of the post */export const sendblueskypost = async (text: string, url?: string) => {  const agent = await getblueskyagent()  await agent.post({ text })}

就这样,您刚刚向 bluesky 发送了一条帖子。不幸的是,即使您在帖子的文本中包含链接,它也不会自动转换为锚链接。我们很快就会解决这个问题。

自动检测 bluesky 上的分面链接

当您在 bluesky 上的帖子文本中包含链接时,它不会自动转换为锚链接。相反,它显示为纯文本。

要解决此问题,您需要检测链接并将其转换为分面链接。

虽然有手动方法可以实现这一点,但幸运的是,atproto 提供了一个 richtext 类,可以自动检测链接并将其转换为分面链接。

import { richtext } from "@atproto/api"/** * send a post to bluesky * @param text - the text of the post */export const sendblueskypost = async (text: string) => {  const agent = await getblueskyagent()  const rt = new richtext({ text })  await rt.detectfacets(agent)  await agent.post({    text: rt.text,    facets: rt.facets,  })}

那太好了,但我们仍然需要将嵌入卡添加到帖子中。接下来我们就开始吧。

在 bluesky 上创建嵌入卡

在帖子中包含链接固然很棒,但如果您可以添加嵌入卡就更好了。

为了实现这一点,我们需要使用 bluesky 的网站卡片嵌入功能。本质上,您向帖子添加一个嵌入密钥,其中至少包括 url、标题和描述。

有多种方法可以获得所需的数据。如果您在发布时知道它,则可以简单地对其进行硬编码。否则,您可以抓取 url 以收集标题、描述和图像。

但是,我发现最简单的方法是使用 dub.co metatags api 获取 url 元数据,然后从中创建嵌入卡。让我们看看它是如何工作的。

type metadata = {  title: string  description: string  image: string}/** * get the url metadata * @param url - the url to get the metadata for * @returns the metadata */const geturlmetadata = async (url: string) => {  const req = await fetch(`https://api.dub.co/metatags?url=${url}`)  const metadata: metadata = await req.json()  return metadata}

我们创建了一个简单的函数,用于获取 url 元数据,然后以清晰的格式返回数据。

接下来,让我们创建一个函数,使用元数据将图像上传到 bluesky,然后创建嵌入卡。

const getblueskyembedcard = async (url: string | undefined, agent: atpagent) => {  if (!url) return  try {    const metadata = await geturlmetadata(url)    const blob = await fetch(metadata.image).then(r => r.blob())    const { data } = await agent.uploadblob(blob, { encoding: "image/jpeg" })    return {      $type: "app.bsky.embed.external",      external: {        uri: url,        title: metadata.title,        description: metadata.description,        thumb: data.blob,      },    }  } catch (error) {    console.error("error fetching embed card:", error)    return  }}

一旦我们有了嵌入卡,我们就可以将其添加到帖子中。

export const sendblueskypost = async (text: string, url?: string) => {  const agent = await getblueskyagent()  const rt = new richtext({ text })  await rt.detectfacets(agent)  await agent.post({    text: rt.text,    facets: rt.facets,    embed: await getblueskyembedcard(url, agent),  })}

现在我们有一个功能,可以使用嵌入卡向 bluesky 发送帖子。

完整示例

希望,如果您已经按照说明进行操作,现在应该已经有了完整的代码。如果没有,这里是完整的代码,您可以将其复制并粘贴到您的项目中。它:

创建 bluesky 代理获取 url 元数据创建嵌入卡使用嵌入卡向 bluesky 发送帖子并自动检测多面链接

import { AtpAgent, RichText } from "@atproto/api"type Metadata = {  title: string  description: string  image: string}/** * Get the URL metadata * @param url - The URL to get the metadata for * @returns The metadata */const getUrlMetadata = async (url: string) => {  const req = await fetch(`https://api.dub.co/metatags?url=${url}`)  const metadata: Metadata = await req.json()  return metadata}/** * Get the Bluesky embed card * @param url - The URL to get the embed card for * @param agent - The Bluesky agent * @returns The embed card */const getBlueskyEmbedCard = async (url: string | undefined, agent: AtpAgent) => {  if (!url) return  try {    const metadata = await getUrlMetadata(url)    const blob = await fetch(metadata.image).then(r => r.blob())    const { data } = await agent.uploadBlob(blob, { encoding: "image/jpeg" })    return {      $type: "app.bsky.embed.external",      external: {        uri: url,        title: metadata.title,        description: metadata.description,        thumb: data.blob,      },    }  } catch (error) {    console.error("Error fetching embed card:", error)    return  }}/** * Get the Bluesky agent * @returns The Bluesky agent */const getBlueskyAgent = async () => {  const agent = new AtpAgent({    service: "https://bsky.social",  })  await agent.login({    identifier: process.env.BLUESKY_USERNAME!,    password: process.env.BLUESKY_PASSWORD!,  })  return agent}/** * Send a post to Bluesky * @param text - The text of the post * @param url - The URL to include in the post */export const sendBlueskyPost = async (text: string, url?: string) => {  const agent = await getBlueskyAgent()  const rt = new RichText({ text })  await rt.detectFacets(agent)  await agent.post({    text: rt.text,    facets: rt.facets,    embed: await getBlueskyEmbedCard(url, agent),  })}

我希望您发现本教程很有帮助,并且您会考虑在自己的项目中使用它。

发帖快乐!

以上就是如何使用 JavaScript 在 Bluesky 上发布带有嵌入卡的链接的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1499399.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
使用 VSCode 的基本 Javascript
上一篇 2025年12月19日 21:45:18
JavaScript:学习编码基础知识
下一篇 2025年12月19日 21:45:30

相关推荐

发表回复

登录后才能评论
关注微信