指南 · comments:write
回复评论
一个小型机器人:检查您最新帖子下的评论并回复新评论,绝不重复回复。
权限范围:posts:read 和 comments:write(可选:likes:write)。
相关端点#
| 端点 | 用途 |
|---|---|
GET /me/posts | 您的最新帖子。 |
GET /posts/{id}/comments | 帖子下的评论(分页)。 |
POST /posts/{id}/comments | 发表评论或回复。 |
DELETE /comments/{id} | 删除您的评论,或您帖子下的评论。 |
POST /posts/{id}/like | 点赞帖子。 |
1. 读取评论#
curl "https://api.linework.app/open/v1/posts/1043/comments?limit=100" \
-H "Authorization: Bearer $LINEWORK_API_KEY"
{
"data": [
{
"id": "88121",
"text": "Where is this?",
"author": { "id": "5512", "username": "giulia" },
"createdAt": "2026-10-14T09:30:00.000Z"
}
],
"next_cursor": null
}
2. 回复#
要回复某条特定评论,请将其 ID 作为 parentId 传入;否则评论会直接发在帖子下。
curl https://api.linework.app/open/v1/posts/1043/comments \
-H "Authorization: Bearer $LINEWORK_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "text": "Lake Como, from Varenna 😊", "parentId": "88121" }'
3. 组合起来#
记录您已经回复过的评论(这里保存在本地文件中),并跳过您自己的评论。
import { readFile, writeFile } from "node:fs/promises";
const API = "https://api.linework.app/open/v1";
const headers = {
Authorization: `Bearer ${process.env.LINEWORK_API_KEY}`,
"Content-Type": "application/json",
};
const get = (p) => fetch(API + p, { headers }).then((r) => r.json());
const seen = new Set(JSON.parse(await readFile("seen.json", "utf8").catch(() => "[]")));
const me = await get("/me");
const posts = await get("/me/posts?limit=10");
for (const post of posts.data) {
const comments = await get(`/posts/${post.id}/comments?limit=100`);
for (const c of comments.data) {
if (seen.has(c.id) || c.author.id === me.id) continue;
const reply = answer(c.text);
if (reply) {
await fetch(`${API}/posts/${post.id}/comments`, {
method: "POST",
headers,
body: JSON.stringify({ text: reply, parentId: c.id }),
});
}
seen.add(c.id);
}
}
await writeFile("seen.json", JSON.stringify([...seen]));
function answer(text) {
if (/where|dove/i.test(text)) return "Lake Como, from Varenna 😊";
return null; // no canned answer: leave it to a human
}
import json, os, re, requests
API = "https://api.linework.app/open/v1"
s = requests.Session()
s.headers["Authorization"] = f"Bearer {os.environ['LINEWORK_API_KEY']}"
try:
seen = set(json.load(open("seen.json")))
except FileNotFoundError:
seen = set()
def answer(text):
if re.search(r"where|dove", text, re.I):
return "Lake Como, from Varenna 😊"
return None # no canned answer: leave it to a human
me = s.get(API + "/me", timeout=30).json()
posts = s.get(API + "/me/posts", params={"limit": 10}, timeout=30).json()
for post in posts["data"]:
comments = s.get(f"{API}/posts/{post['id']}/comments", params={"limit": 100}, timeout=30).json()
for c in comments["data"]:
if c["id"] in seen or c["author"]["id"] == me["id"]:
continue
reply = answer(c["text"])
if reply:
s.post(f"{API}/posts/{post['id']}/comments",
json={"text": reply, "parentId": c["id"]}, timeout=30)
seen.add(c["id"])
json.dump(sorted(seen), open("seen.json", "w"))
每隔几分钟运行一次(cron、定时任务、无服务器定时器)。每次运行只会发出少量请求,远低于每分钟 120 个的限制。
评论机器人规则#
- 每日配额:每个账号 300 条评论和 500 次点赞。
- 只在回复有用时才回复。不要反复发布相同的文字,也不要评论从未与您互动过的人的帖子。
- 当机器人不确定时,请保持沉默,交由真人回复。