-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxquik-twitter.ts
More file actions
184 lines (157 loc) · 5.21 KB
/
Copy pathxquik-twitter.ts
File metadata and controls
184 lines (157 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type { Variant, PublishResult, ChannelHints } from "../models.js";
import type { Profile } from "../backends/base.js";
import type { ChannelAdapter } from "./index.js";
const DEFAULT_XQUIK_BASE_URL = "https://xquik.com";
const XQUIK_TWEET_PATH = "/api/v1/x/tweets";
const X_POST_LIMIT = 280;
const REQUEST_TIMEOUT_MS = 30_000;
interface XquikPostResponse {
data?: {
id?: unknown;
tweetId?: unknown;
url?: unknown;
};
tweet?: {
id?: unknown;
url?: unknown;
};
id?: unknown;
tweetId?: unknown;
url?: unknown;
}
interface XquikConfig {
account: string;
apiKey: string;
baseUrl: string;
}
function credential(profile: Profile, key: string): string {
const profileValue = profile.credentials[key]?.trim();
if (profileValue) return profileValue;
return (process.env[key] ?? "").trim();
}
function trimTrailingSlash(value: string): string {
return value.replace(/\/+$/, "");
}
export function getXquikConfig(profile: Profile, variant: Variant): XquikConfig {
const apiKey = credential(profile, "XQUIK_API_KEY")
|| credential(profile, "HERMES_TWEET_API_KEY");
const baseUrl = trimTrailingSlash(
credential(profile, "XQUIK_BASE_URL") || DEFAULT_XQUIK_BASE_URL,
);
const accountFromChannel = variant.channel.split(":")[1] ?? "";
const accountFromExtras = typeof variant.extras.account === "string"
? variant.extras.account
: "";
const account = accountFromExtras.trim()
|| credential(profile, "XQUIK_ACCOUNT")
|| credential(profile, "HERMES_TWEET_ACCOUNT")
|| accountFromChannel.trim();
return { account, apiKey, baseUrl };
}
export function buildXquikUrl(baseUrl: string): string {
return new URL(`${trimTrailingSlash(baseUrl)}${XQUIK_TWEET_PATH}`).toString();
}
export function buildXquikHeaders(apiKey: string): Record<string, string> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
Accept: "application/json",
"User-Agent": "content-distribution-mcp/xquik-twitter",
};
if (apiKey.startsWith("xq_")) {
headers["x-api-key"] = apiKey;
} else {
headers.Authorization = `Bearer ${apiKey}`;
}
return headers;
}
function stringValue(value: unknown): string {
return typeof value === "string" ? value : "";
}
function extractTweetId(payload: XquikPostResponse): string {
return stringValue(payload.data?.id)
|| stringValue(payload.data?.tweetId)
|| stringValue(payload.tweet?.id)
|| stringValue(payload.id)
|| stringValue(payload.tweetId);
}
function extractTweetUrl(payload: XquikPostResponse, account: string): string | undefined {
const explicitUrl = stringValue(payload.data?.url)
|| stringValue(payload.tweet?.url)
|| stringValue(payload.url);
if (explicitUrl) return explicitUrl;
const id = extractTweetId(payload);
if (!id || !account) return undefined;
return `https://x.com/${account.replace(/^@/, "")}/status/${id}`;
}
async function readJson(response: Response): Promise<XquikPostResponse> {
const text = await response.text();
if (!text) return {};
try {
return JSON.parse(text) as XquikPostResponse;
} catch {
return { data: { id: "" } };
}
}
async function postWithTimeout(url: string, init: RequestInit): Promise<Response> {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
try {
return await fetch(url, { ...init, signal: controller.signal });
} finally {
clearTimeout(timer);
}
}
export class XquikTwitterAdapter implements ChannelAdapter {
constructor(private readonly fallback: ChannelAdapter) {}
hints(): ChannelHints {
return {
max_length: X_POST_LIMIT,
supported_md_features: ["links"],
cta_placement: "none",
canonical_url_supported: false,
browser_only: false,
};
}
async publish(variant: Variant, profile: Profile): Promise<PublishResult> {
const config = getXquikConfig(profile, variant);
if (!config.apiKey) {
return this.fallback.publish(variant, profile);
}
if (!config.account) {
return {
channel: variant.channel,
state: "failed",
error: "XQUIK_ACCOUNT or HERMES_TWEET_ACCOUNT required for automated Twitter/X publishing",
};
}
const response = await postWithTimeout(buildXquikUrl(config.baseUrl), {
method: "POST",
headers: buildXquikHeaders(config.apiKey),
body: JSON.stringify({
account: config.account,
text: variant.body.slice(0, X_POST_LIMIT),
}),
});
const payload = await readJson(response);
if (!response.ok) {
const detail = stringValue((payload as { error?: unknown }).error)
|| stringValue((payload as { message?: unknown }).message)
|| response.statusText
|| "request failed";
return {
channel: variant.channel,
state: "failed",
error: `Hermes Tweet publish failed (${response.status}): ${detail}`,
};
}
return {
channel: variant.channel,
state: "live",
live_url: extractTweetUrl(payload, config.account),
published_at: new Date().toISOString(),
};
}
async unpublish(liveUrl: string, profile: Profile): Promise<[boolean, string | undefined]> {
return this.fallback.unpublish(liveUrl, profile);
}
}