> ## Documentation Index
> Fetch the complete documentation index at: https://www.ayrshare.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Airtable

> 从 Airtable 管理你用户的社交媒体账户

## 概览

[Airtable](https://www.airtable.com) 是一个多功能云平台，将电子表格的结构与数据库的强大能力无缝融合。

通过将 Airtable 的自动化脚本（使用 JavaScript 编写）与 Ayrshare 的 API 集成，你可以直接在 Airtable 中简化社交媒体管理工作。此集成让你可以代表用户发布内容、分析绩效指标以及管理评论，所有这些都在一个集中位置完成。

在本指南中，我们将带你一步步完成集成搭建。

## 搭建

<Warning>
  运行 Airtable 自动化脚本需要 [付费](https://airtable.com/pricing) 的 Airtable 方案。
  请确认你的 Airtable 方案包含带脚本的自动化功能。
</Warning>

本指南将向你展示如何通过 [Ayrshare](https://www.ayrshare.com) 在 Airtable 中自动发布到关联的社交媒体账户。
你既可以发布到单个公司的社交账户，也可以发布到你管理的客户的社交账户。下面所有代码都可以在 [GitHub](https://github.com/ayrshare/airtable-post-social-media/blob/main/script.js) 上找到。

<Card horizontal icon="github" href="https://github.com/ayrshare/airtable-post-social-media/blob/main/script.js" title="GitHub：Airtable Post Social Media" />

## 获取你的 API Key

首先，在 [Ayrshare Dashboard](https://www.ayrshare.com) 获取你的免费或付费方案 API Key。此密钥将在下面的脚本中使用。

如果你使用 Ayrshare [Launch Plan](/multiple-users/business-launch-overview) 或 [Business Plan](https://www.ayrshare.com/business-plan-for-multiple-users/) 并希望代表客户发帖，可通过 /user 或 /create-profile 接口，或者在 Ayrshare Dashboard 中收集所有客户的 Profile Key。

<Tip>
  请确保你已经在 Ayrshare 中关联了一些社交账户。可在 [Ayrshare 社交账户
  关联页面](https://app.ayrshare.com/social-accounts) 中确认。
</Tip>

## 创建 Airtable Workspace

在 [Airtable](https://www.airtable.com) 中创建一个包含以下字段的新 workspace。请务必按下述方式命名列：

<ul class="custom-bullets">
  <li>`Post`：Long Text 类型</li>

  <li>
    `Platforms`：Multi Select 类型，选项为：facebook、instagram、twitter、linkedin、reddit 和
    telegram
  </li>

  <li>`Images`：Attachment 类型</li>
  <li>`Profile Keys`：Single Line Text 类型</li>
  <li>`Status`：Single Line Text 类型。</li>
  <li>`Schedule Date`：Date 类型，采用本地格式，包含时间字段，时间格式为 24 小时制</li>
</ul>

<img class="center" src="https://mintcdn.com/ayrshare-docs/6mPR4kmwNEz1kMbX/images/packages-guides/airtable-example.webp?fit=max&auto=format&n=6mPR4kmwNEz1kMbX&q=85&s=ac8b91e08f67f7c2432ddfbbf7e65aa0" alt="Airtable Table Example" width="1536" height="161" data-path="images/packages-guides/airtable-example.webp" />

这些字段将在我们即将构建的 Airtable 自动化脚本中使用。

查看一个[实时 Airtable 示例](https://airtable.com/shrCWY0oA1ghB42tI/tblpnTEiPwyuViqBo)。

<Card horizontal icon="book-open" href="https://airtable.com/shrCWY0oA1ghB42tI/tblpnTEiPwyuViqBo" title="Airtable 社交媒体示例">
  查看一个实时的 Airtable 示例
</Card>

## 填写测试发帖数据

我们需要一些示例数据来测试发帖。这里给出一份建议：

<ul class="custom-bullets">
  <li>`Post`：填入 *Happy New Year 2025*</li>

  <li>
    `Platforms`：选择一个或多个你已关联的网络。请务必使用小写名称。
  </li>

  <li>
    `Images`：附加一张图片。可以下载并附加下面这张：
    [https://img.ayrshare.com/012/gb.jpg](https://img.ayrshare.com/012/gb.jpg)
  </li>

  <li>
    `Profile Keys`：如果你使用 Business Plan 或 Launch Plan 并希望发布到客户的配置，请填入他们的
    Profile Key。*否则请留空。*
  </li>

  <li>
    `Status`：填入 *pending*。脚本只会抓取状态为 pending 的记录。请务必
    使用小写的 `pending`。
  </li>

  <li>
    `Schedule Date`：留空，因为现在我们只测试立即发帖。之后你可以
    选择一个未来时间来定时发帖。
  </li>
</ul>

## 使用脚本编辑器构建自动化脚本

现在我们将构建 Airtable 自动化脚本，用于从表中读取数据、创建帖子，并通过 Ayrshare 将其发送到社交网络。你会用到 Airtable 的脚本编辑器。

### 添加触发器

* 在 workspace 中，点击 *Automation*，然后点击 *+New automation.*

<img class="center" src="https://mintcdn.com/ayrshare-docs/6mPR4kmwNEz1kMbX/images/packages-guides/new%20automation.webp?fit=max&auto=format&n=6mPR4kmwNEz1kMbX&q=85&s=d2637323e3e540d6e46cf0d45e2f161a" alt="Go to the Automation section of Airtable" width="932" height="144" data-path="images/packages-guides/new automation.webp" />

<ul class="custom-bullets">
  <li>为自动化命名。</li>
  <li>点击 *Choose a Trigger*。</li>
  <li>选择 When a Record is Created。</li>
  <li>选择包含上述字段的表。</li>
  <li>点击 Done。</li>
</ul>

## 添加动作

<ul class="custom-bullets">
  <li>点击 *Add Action* 开始。</li>
  <li>选择 *Run Script*。你将被带到脚本编辑器。</li>
</ul>

**删除**下面这一行：

```javascript theme={"system"}
console.log(`Hello, ${base.name}!`);
```

然后**复制并粘贴**以下代码到脚本编辑器中：

```javascript theme={"system"}
const API_KEY = "Your API Key"; // 在 app.ayrshare.com 获取免费 key

console.log(`Starting Post ${base.name}!`);

const sendPost = async (data) => {
  const { post, platforms, imageUrls, profileKeys, scheduleDate, shortenLinks } = data;

  const body = Object.assign(
    {},
    post && { post },
    platforms && { platforms },
    profileKeys && { profileKeys: profileKeys.split(",") },
    Array.isArray(imageUrls) &&
      imageUrls.length > 0 && {
        mediaUrls: imageUrls.map((image) => image.url)
      },
    scheduleDate && { scheduleDate },
    shortenLinks !== undefined && shortenLinks !== null && { shortenLinks }
  );

  console.log("Posting JSON:", JSON.stringify(body, null, 2));

  if (profileKeys) {
    body.profileKeys = profileKeys.split(",");
  }

  const response = await fetch("https://api.ayrshare.com/api/post", {
    method: "POST",
    body: JSON.stringify(body),
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${API_KEY}`
    }
  }).then((res) => res.json());

  return response;
};

const table = base.getTable("Posts");
const query = await table.selectRecordsAsync();
const filteredRecords = query.records.filter((record) => {
  const status = record.getCellValue("Status");
  return status === "pending";
});

for (let record of filteredRecords) {
  const post = record.getCellValue("Post");
  const images = record.getCellValue("Images");
  const platforms = record.getCellValue("Platforms");
  const profileKeys = record.getCellValue("Profile Keys");
  const scheduleDate = record.getCellValue("Schedule Date");

  const shortenLinks = false;
  const response = await sendPost({
    post,
    platforms: platforms.map((x) => x.name),
    imageUrls: images,
    profileKeys,
    scheduleDate,
    shortenLinks
  });

  console.log(response);

  if (response) {
    let status;
    if (Array.isArray(response)) {
      status = response.map((x) => x.status).every((x) => x === "success") ? "success" : "error";
    } else {
      status = response.status;
    }

    await table.updateRecordAsync(record, {
      Status: status
    });
  }
}
```

这段代码会从你的表中读取数据并通过 Ayrshare 发帖。不过在此之前，你需要先填入自己的 API Key（在上文中获取）。

将 `Your API Key` 替换为你真实的 API key。

## 测试脚本

在脚本编辑器中，按下 *>Test*。

脚本将运行并输出 API 调用返回的响应。如果一切正常，你会看到一个成功消息，你记录中的 pending 字段会被更新为 success，同时你在所选社交网络上会看到该帖子。

之后每当你在表中新增一条记录，脚本就会运行并处理状态为 pending 的记录。

## Airtable 文档

如需了解更多信息，请参见 [Airtable 官方文档](https://www.airtable.com/developers)。
其中包含关于如何使用 Airtable 脚本编辑器的详细说明。

## 视频教程

以下是 Automate All The Things 团队制作的一段优质视频教程，视频演示了如何将 Ayrshare 集成到一个真实的 Airtable 项目中。

<div class="video-container">
  <iframe width="560" height="315" src="https://www.youtube.com/embed/U_jo6f5hsyY" title="Post To Social Media From Airtable" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" />
</div>

更多[将 Airtable 与社交媒体集成的示例](https://www.ayrshare.com/blog/automatically-post-to-social-media-from-airtable/)请参见此处。

## 问题

如有任何问题或反馈，请通过 [邮件](mailto:contact@ayrshare.com) 联系我们，或 <a href="javascript:void(0)" onClick={() => window.Intercom && window.Intercom('show')}>与我们在线沟通</a>。
