brendmanager/src/linkedin.js
Peppa 35e974c510
All checks were successful
peppa/lint lint: success за 1с
peppa/test test: success за 2с
ci / checks (pull_request) Successful in 11s
deploy / deploy (pull_request) Successful in 20s
BRM-78: объяснение, почему отправка в Telegram и LinkedIn выключена
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 18:34:58 +05:00

57 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// LinkedIn: публикация текстового поста в личный профиль (UGC Posts API).
// Токен, автор и статус одобрения приложения — только из env.
// LINKEDIN_APP_STATUS: waiting (по умолчанию) | received | denied.
const WEEK = 7 * 24 * 3600 * 1000;
const LIMITS = [{ limit: 1, periodMs: 24 * 3600 * 1000 }, { limit: 5, periodMs: WEEK }];
export function createLinkedIn({
token = process.env.LINKEDIN_ACCESS_TOKEN,
author = process.env.LINKEDIN_AUTHOR_URN, // urn:li:person:...
status = process.env.LINKEDIN_APP_STATUS ?? "waiting",
fetchImpl = fetch,
} = {}) {
return {
channel: "linkedin",
secrets: () => [token].filter(Boolean),
approval: () => status,
defaultLimits: () => LIMITS,
connected: () => Boolean(token && author),
missing: () => (!token ? "key" : !author ? "author" : null),
async send(text) {
if (!token || !author) throw new Error("нет доступов к LinkedIn");
const r = await fetchImpl("https://api.linkedin.com/v2/ugcPosts", {
method: "POST",
headers: { authorization: `Bearer ${token}`, "content-type": "application/json", "x-restli-protocol-version": "2.0.0" },
body: JSON.stringify({
author, lifecycleState: "PUBLISHED",
specificContent: { "com.linkedin.ugc.ShareContent": { shareCommentary: { text }, shareMediaCategory: "NONE" } },
visibility: { "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC" },
}),
});
const j = await r.json().catch(() => ({}));
if (!r.ok || !j.id) throw new Error(j.message ?? `ошибка LinkedIn ${r.status}`);
return { id: j.id, url: `https://www.linkedin.com/feed/update/${j.id}/` };
},
};
}
// Заглушка с контрактом UGC Posts API до одобрения приложения LinkedIn.
export function mockLinkedIn({ fail, status = "received" } = {}) {
const sent = [];
return {
channel: "linkedin",
sent,
secrets: () => ["MOCK_LI_TOKEN"],
approval: () => status,
defaultLimits: () => LIMITS,
connected: () => true,
async send(text) {
if (fail) throw new Error(fail);
const id = `urn:li:share:${1000 + sent.length}`;
const m = { id, text, url: `https://www.linkedin.com/feed/update/${id}/` };
sent.push(m);
return m;
},
};
}