AI agents can use the SimplePost documentation index at /llms.txt to find related guides.
TypeScript SDK
当你掌控 TypeScript 运行时,并希望你的应用或智能体与各平台发布器之间的抽象层尽可能少时,请使用该 SDK。REST 服务器、Scheduler 应用、CLI 和 MCP 服务器都构建在这个包之上。
安装
该 SDK 已公开发布在 npm 上,不需要 GitHub 令牌或自定义包注册表。使用你常用的包管理器安装即可:
npm install @simple-post/sdk
# or
yarn add @simple-post/sdk
# or
pnpm add @simple-post/sdk
快速开始
将 TELEGRAM_BOT_TOKEN 设置为你的机器人令牌,将 TELEGRAM_CHAT_ID 设置为频道用户名(例如 @mychannel)或数字聊天 ID。请将机器人添加到该聊天中,并授予其发帖权限。示例中显式传入了聊天 ID;SDK 不会自动加载它。
import { post } from "@simple-post/sdk";
const botToken = process.env.TELEGRAM_BOT_TOKEN;
const chatId = process.env.TELEGRAM_CHAT_ID;
if (!botToken || !chatId) throw new Error("Set TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID");
const results = await post({
content: { text: "Hello from SimplePost" },
platforms: ["telegram"],
options: { telegram: { chatId, credentials: { botToken } } },
});
const result = results.get("telegram");
console.log(result);
Telegram 发布成功的结果包含消息 ID 和 error: "NO_ERROR"。post() 会返回一个以所请求平台为键的 Map;请检查每一个结果,因为多平台发布可能出现部分失败。有关创建机器人和权限设置,请参阅 Telegram 设置。
该快速开始已针对已发布的 SDK 1.3.3 进行检查。各平台指南会区分该版本与较新的托管/核心行为;请参阅发布范围。
发布模型
SDK 使用 Post 载荷。其他接口会将各自的输入转换为此模型;REST 账户 ID 和 MCP 的 message 字段并不是 SDK 字段。在为 X、YouTube 和 Instagram 配置好凭据,并设置好公共媒体存储之后:
await post({
content: {
text: "Check out this video",
media: [
{
type: "video",
path: "./video.mp4",
title: "Launch demo",
},
],
},
platforms: ["x", "youtube", "instagram"],
options: {
common: { logLevel: "info", strictMode: false },
youtube: { privacyStatus: "unlisted" },
},
});
媒体可以是运行时能够读取文件的本地 path,也可以是公开的 url。某些平台(尤其是 Instagram 和 Threads)在底层要求使用公开的媒体 URL。配置存储后,SimplePost 可以上传临时的公共媒体。
提供商设置
SDK 用户通常需要自行管理提供商凭据。你可以通过两种方式提供凭据:
- 环境变量,适用于本地工具和单账户服务。
options.<platform>.credentials,更适合应用为每个用户存储各自 OAuth 令牌的情形。
请从你想要发帖的每个提供商对应的平台指南开始:
| 平台 | 键 | 指南 |
|---|---|---|
| X | x | X 设置 |
| Telegram | telegram | Telegram 设置 |
instagram | Instagram 设置 | |
facebook | Facebook 设置 | |
| Threads | threads | Threads 设置 |
| TikTok | tiktok | TikTok 设置 |
| YouTube | youtube | YouTube 设置 |
pinterest | Pinterest 设置 | |
linkedin | LinkedIn 设置 | |
| Bluesky | bluesky | Bluesky 设置 |
常见用法
文本帖子
await post({
content: { text: "Hey, this is a simple post" },
platforms: ["x", "facebook"],
});
媒体帖子
await post({
content: {
text: "Here are some photos",
media: [
{ type: "image", path: "./image1.jpg" },
{ type: "image", path: "./image2.jpg" },
],
},
platforms: ["x", "instagram"],
});
X 回复或串文片段
await post({
content: { text: "Great point." },
platforms: ["x"],
options: {
x: { replyToId: "1234567890" },
},
});
Telegram 频道
Telegram 的每次发帖都需要 chatId 选项——它不会从任何环境变量中读取。机器人令牌从 TELEGRAM_BOT_TOKEN 中读取,或通过 options.telegram.credentials.botToken 提供。
await post({
content: { text: "Hello Telegram" },
platforms: ["telegram"],
options: {
telegram: { chatId: "@mychannel", parseMode: "HTML" },
},
});
严格模式
默认情况下,SimplePost 会在可能时根据平台限制调整内容。例如,图片数量上限较低的平台收到的图片可能比其他平台更少。启用严格模式后,将改为直接失败:
await post({
content: { text: "Cross-platform post" },
platforms: ["x", "bluesky"],
options: {
common: { strictMode: true },
},
});
令牌刷新
某些 OAuth 提供商会轮换刷新令牌。X 是最重要的例子:每次成功刷新 X 令牌都会返回新的刷新令牌,并使旧令牌失效。SDK 不会为你持久化刷新后的凭据。如果结果中包含 extraData.refreshedCredentials,请在下一次发帖之前存储这些值。
const refreshed = results.get("x")?.extraData?.refreshedCredentials;
if (refreshed) {
await saveUserTokens(refreshed);
}
示例
代码仓库在 core/examples 下提供了示例,包括:
examples/x/postThread.tsexamples/instagram/postCarousel.tsexamples/tiktok/postVideo.tsexamples/youtube/postVideoFull.tsexamples/all/postVideo.ts
在设置好相应的提供商凭据后,请在 core/examples 工作区中运行示例。
若要发布视频,请安装 FFmpeg,使 ffprobe 位于 PATH 中,或者设置一个绝对路径的 FFPROBE_PATH。SDK 会在上传前测量文件;检查失败会返回验证错误。请参阅验证覆盖范围。