Linework Developers
IT
Ottieni l'accesso

Guida · comments:write

Rispondere ai commenti

Un piccolo bot che controlla i commenti ai tuoi ultimi post e risponde a quelli nuovi, senza mai rispondere due volte.

Scope: posts:read e comments:write (facoltativo: likes:write).

Gli endpoint#

EndpointUso
GET /me/postsI tuoi ultimi post.
GET /posts/{id}/commentsI commenti a un post (paginato).
POST /posts/{id}/commentsScrive un commento o una risposta.
DELETE /comments/{id}Elimina un tuo commento, o un commento a un tuo post.
POST /posts/{id}/likeMette like a un post.

1. Leggi i commenti#

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. Rispondi#

Per rispondere a un commento specifico, passa il suo id come parentId; senza di esso il commento viene pubblicato sul post.

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. Mettere tutto insieme#

Ricorda a quali commenti hai già risposto (qui in un file locale) e salta i tuoi commenti.

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"))

Eseguilo ogni pochi minuti (cron, un job pianificato, un timer serverless). Ogni esecuzione fa poche richieste, molto al di sotto del limite di 120 al minuto.

Regole per i bot dei commenti#

  • Quota giornaliera: 300 commenti e 500 like per account.
  • Rispondi solo dove la risposta è utile. Non pubblicare lo stesso testo più e più volte e non commentare i post di persone che non hanno mai interagito con te.
  • Quando il bot non è sicuro, resta in silenzio e lascia rispondere una persona.