@openclaw/twitch 2026.2.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/README.md +89 -0
- package/index.ts +20 -0
- package/openclaw.plugin.json +9 -0
- package/package.json +20 -0
- package/src/access-control.test.ts +491 -0
- package/src/access-control.ts +166 -0
- package/src/actions.ts +174 -0
- package/src/client-manager-registry.ts +115 -0
- package/src/config-schema.ts +84 -0
- package/src/config.test.ts +87 -0
- package/src/config.ts +116 -0
- package/src/monitor.ts +273 -0
- package/src/onboarding.test.ts +316 -0
- package/src/onboarding.ts +417 -0
- package/src/outbound.test.ts +403 -0
- package/src/outbound.ts +187 -0
- package/src/plugin.test.ts +39 -0
- package/src/plugin.ts +274 -0
- package/src/probe.test.ts +196 -0
- package/src/probe.ts +119 -0
- package/src/resolver.ts +137 -0
- package/src/runtime.ts +14 -0
- package/src/send.test.ts +276 -0
- package/src/send.ts +136 -0
- package/src/status.test.ts +270 -0
- package/src/status.ts +179 -0
- package/src/test-fixtures.ts +30 -0
- package/src/token.test.ts +171 -0
- package/src/token.ts +91 -0
- package/src/twitch-client.test.ts +589 -0
- package/src/twitch-client.ts +277 -0
- package/src/types.ts +143 -0
- package/src/utils/markdown.ts +98 -0
- package/src/utils/twitch.ts +78 -0
- package/test/setup.ts +7 -0
package/src/resolver.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Twitch resolver adapter for channel/user name resolution.
|
|
3
|
+
*
|
|
4
|
+
* This module implements the ChannelResolverAdapter interface to resolve
|
|
5
|
+
* Twitch usernames to user IDs via the Twitch Helix API.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { ApiClient } from "@twurple/api";
|
|
9
|
+
import { StaticAuthProvider } from "@twurple/auth";
|
|
10
|
+
import type { ChannelResolveKind, ChannelResolveResult } from "./types.js";
|
|
11
|
+
import type { ChannelLogSink, TwitchAccountConfig } from "./types.js";
|
|
12
|
+
import { normalizeToken } from "./utils/twitch.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Normalize a Twitch username - strip @ prefix and convert to lowercase
|
|
16
|
+
*/
|
|
17
|
+
function normalizeUsername(input: string): string {
|
|
18
|
+
const trimmed = input.trim();
|
|
19
|
+
if (trimmed.startsWith("@")) {
|
|
20
|
+
return trimmed.slice(1).toLowerCase();
|
|
21
|
+
}
|
|
22
|
+
return trimmed.toLowerCase();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Create a logger that includes the Twitch prefix
|
|
27
|
+
*/
|
|
28
|
+
function createLogger(logger?: ChannelLogSink): ChannelLogSink {
|
|
29
|
+
return {
|
|
30
|
+
info: (msg: string) => logger?.info(msg),
|
|
31
|
+
warn: (msg: string) => logger?.warn(msg),
|
|
32
|
+
error: (msg: string) => logger?.error(msg),
|
|
33
|
+
debug: (msg: string) => logger?.debug?.(msg) ?? (() => {}),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Resolve Twitch usernames to user IDs via the Helix API
|
|
39
|
+
*
|
|
40
|
+
* @param inputs - Array of usernames or user IDs to resolve
|
|
41
|
+
* @param account - Twitch account configuration with auth credentials
|
|
42
|
+
* @param kind - Type of target to resolve ("user" or "group")
|
|
43
|
+
* @param logger - Optional logger
|
|
44
|
+
* @returns Promise resolving to array of ChannelResolveResult
|
|
45
|
+
*/
|
|
46
|
+
export async function resolveTwitchTargets(
|
|
47
|
+
inputs: string[],
|
|
48
|
+
account: TwitchAccountConfig,
|
|
49
|
+
kind: ChannelResolveKind,
|
|
50
|
+
logger?: ChannelLogSink,
|
|
51
|
+
): Promise<ChannelResolveResult[]> {
|
|
52
|
+
const log = createLogger(logger);
|
|
53
|
+
|
|
54
|
+
if (!account.clientId || !account.accessToken) {
|
|
55
|
+
log.error("Missing Twitch client ID or accessToken");
|
|
56
|
+
return inputs.map((input) => ({
|
|
57
|
+
input,
|
|
58
|
+
resolved: false,
|
|
59
|
+
note: "missing Twitch credentials",
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const normalizedToken = normalizeToken(account.accessToken);
|
|
64
|
+
|
|
65
|
+
const authProvider = new StaticAuthProvider(account.clientId, normalizedToken);
|
|
66
|
+
const apiClient = new ApiClient({ authProvider });
|
|
67
|
+
|
|
68
|
+
const results: ChannelResolveResult[] = [];
|
|
69
|
+
|
|
70
|
+
for (const input of inputs) {
|
|
71
|
+
const normalized = normalizeUsername(input);
|
|
72
|
+
|
|
73
|
+
if (!normalized) {
|
|
74
|
+
results.push({
|
|
75
|
+
input,
|
|
76
|
+
resolved: false,
|
|
77
|
+
note: "empty input",
|
|
78
|
+
});
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const looksLikeUserId = /^\d+$/.test(normalized);
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
if (looksLikeUserId) {
|
|
86
|
+
const user = await apiClient.users.getUserById(normalized);
|
|
87
|
+
|
|
88
|
+
if (user) {
|
|
89
|
+
results.push({
|
|
90
|
+
input,
|
|
91
|
+
resolved: true,
|
|
92
|
+
id: user.id,
|
|
93
|
+
name: user.name,
|
|
94
|
+
});
|
|
95
|
+
log.debug?.(`Resolved user ID ${normalized} -> ${user.name}`);
|
|
96
|
+
} else {
|
|
97
|
+
results.push({
|
|
98
|
+
input,
|
|
99
|
+
resolved: false,
|
|
100
|
+
note: "user ID not found",
|
|
101
|
+
});
|
|
102
|
+
log.warn(`User ID ${normalized} not found`);
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
const user = await apiClient.users.getUserByName(normalized);
|
|
106
|
+
|
|
107
|
+
if (user) {
|
|
108
|
+
results.push({
|
|
109
|
+
input,
|
|
110
|
+
resolved: true,
|
|
111
|
+
id: user.id,
|
|
112
|
+
name: user.name,
|
|
113
|
+
note: user.displayName !== user.name ? `display: ${user.displayName}` : undefined,
|
|
114
|
+
});
|
|
115
|
+
log.debug?.(`Resolved username ${normalized} -> ${user.id} (${user.name})`);
|
|
116
|
+
} else {
|
|
117
|
+
results.push({
|
|
118
|
+
input,
|
|
119
|
+
resolved: false,
|
|
120
|
+
note: "username not found",
|
|
121
|
+
});
|
|
122
|
+
log.warn(`Username ${normalized} not found`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
} catch (error) {
|
|
126
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
127
|
+
results.push({
|
|
128
|
+
input,
|
|
129
|
+
resolved: false,
|
|
130
|
+
note: `API error: ${errorMessage}`,
|
|
131
|
+
});
|
|
132
|
+
log.error(`Failed to resolve ${input}: ${errorMessage}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return results;
|
|
137
|
+
}
|
package/src/runtime.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PluginRuntime } from "openclaw/plugin-sdk";
|
|
2
|
+
|
|
3
|
+
let runtime: PluginRuntime | null = null;
|
|
4
|
+
|
|
5
|
+
export function setTwitchRuntime(next: PluginRuntime) {
|
|
6
|
+
runtime = next;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function getTwitchRuntime(): PluginRuntime {
|
|
10
|
+
if (!runtime) {
|
|
11
|
+
throw new Error("Twitch runtime not initialized");
|
|
12
|
+
}
|
|
13
|
+
return runtime;
|
|
14
|
+
}
|
package/src/send.test.ts
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for send.ts module
|
|
3
|
+
*
|
|
4
|
+
* Tests cover:
|
|
5
|
+
* - Message sending with valid configuration
|
|
6
|
+
* - Account resolution and validation
|
|
7
|
+
* - Channel normalization
|
|
8
|
+
* - Markdown stripping
|
|
9
|
+
* - Error handling for missing/invalid accounts
|
|
10
|
+
* - Registry integration
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, expect, it, vi } from "vitest";
|
|
14
|
+
import { sendMessageTwitchInternal } from "./send.js";
|
|
15
|
+
import {
|
|
16
|
+
BASE_TWITCH_TEST_ACCOUNT,
|
|
17
|
+
installTwitchTestHooks,
|
|
18
|
+
makeTwitchTestConfig,
|
|
19
|
+
} from "./test-fixtures.js";
|
|
20
|
+
|
|
21
|
+
// Mock dependencies
|
|
22
|
+
vi.mock("./config.js", () => ({
|
|
23
|
+
DEFAULT_ACCOUNT_ID: "default",
|
|
24
|
+
getAccountConfig: vi.fn(),
|
|
25
|
+
}));
|
|
26
|
+
|
|
27
|
+
vi.mock("./utils/twitch.js", () => ({
|
|
28
|
+
generateMessageId: vi.fn(() => "test-msg-id"),
|
|
29
|
+
isAccountConfigured: vi.fn(() => true),
|
|
30
|
+
normalizeTwitchChannel: (channel: string) => channel.toLowerCase().replace(/^#/, ""),
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
vi.mock("./utils/markdown.js", () => ({
|
|
34
|
+
stripMarkdownForTwitch: vi.fn((text: string) => text.replace(/\*\*/g, "")),
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
vi.mock("./client-manager-registry.js", () => ({
|
|
38
|
+
getClientManager: vi.fn(),
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
describe("send", () => {
|
|
42
|
+
const mockLogger = {
|
|
43
|
+
info: vi.fn(),
|
|
44
|
+
warn: vi.fn(),
|
|
45
|
+
error: vi.fn(),
|
|
46
|
+
debug: vi.fn(),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const mockAccount = {
|
|
50
|
+
...BASE_TWITCH_TEST_ACCOUNT,
|
|
51
|
+
accessToken: "test123",
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const mockConfig = makeTwitchTestConfig(mockAccount);
|
|
55
|
+
installTwitchTestHooks();
|
|
56
|
+
|
|
57
|
+
describe("sendMessageTwitchInternal", () => {
|
|
58
|
+
it("should send a message successfully", async () => {
|
|
59
|
+
const { getAccountConfig } = await import("./config.js");
|
|
60
|
+
const { getClientManager } = await import("./client-manager-registry.js");
|
|
61
|
+
const { stripMarkdownForTwitch } = await import("./utils/markdown.js");
|
|
62
|
+
|
|
63
|
+
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
|
|
64
|
+
vi.mocked(getClientManager).mockReturnValue({
|
|
65
|
+
sendMessage: vi.fn().mockResolvedValue({
|
|
66
|
+
ok: true,
|
|
67
|
+
messageId: "twitch-msg-123",
|
|
68
|
+
}),
|
|
69
|
+
} as unknown as ReturnType<typeof getClientManager>);
|
|
70
|
+
vi.mocked(stripMarkdownForTwitch).mockImplementation((text) => text);
|
|
71
|
+
|
|
72
|
+
const result = await sendMessageTwitchInternal(
|
|
73
|
+
"#testchannel",
|
|
74
|
+
"Hello Twitch!",
|
|
75
|
+
mockConfig,
|
|
76
|
+
"default",
|
|
77
|
+
false,
|
|
78
|
+
mockLogger as unknown as Console,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
expect(result.ok).toBe(true);
|
|
82
|
+
expect(result.messageId).toBe("twitch-msg-123");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("should strip markdown when enabled", async () => {
|
|
86
|
+
const { getAccountConfig } = await import("./config.js");
|
|
87
|
+
const { getClientManager } = await import("./client-manager-registry.js");
|
|
88
|
+
const { stripMarkdownForTwitch } = await import("./utils/markdown.js");
|
|
89
|
+
|
|
90
|
+
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
|
|
91
|
+
vi.mocked(getClientManager).mockReturnValue({
|
|
92
|
+
sendMessage: vi.fn().mockResolvedValue({
|
|
93
|
+
ok: true,
|
|
94
|
+
messageId: "twitch-msg-456",
|
|
95
|
+
}),
|
|
96
|
+
} as unknown as ReturnType<typeof getClientManager>);
|
|
97
|
+
vi.mocked(stripMarkdownForTwitch).mockImplementation((text) => text.replace(/\*\*/g, ""));
|
|
98
|
+
|
|
99
|
+
await sendMessageTwitchInternal(
|
|
100
|
+
"#testchannel",
|
|
101
|
+
"**Bold** text",
|
|
102
|
+
mockConfig,
|
|
103
|
+
"default",
|
|
104
|
+
true,
|
|
105
|
+
mockLogger as unknown as Console,
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
expect(stripMarkdownForTwitch).toHaveBeenCalledWith("**Bold** text");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should return error when account not found", async () => {
|
|
112
|
+
const { getAccountConfig } = await import("./config.js");
|
|
113
|
+
|
|
114
|
+
vi.mocked(getAccountConfig).mockReturnValue(null);
|
|
115
|
+
|
|
116
|
+
const result = await sendMessageTwitchInternal(
|
|
117
|
+
"#testchannel",
|
|
118
|
+
"Hello!",
|
|
119
|
+
mockConfig,
|
|
120
|
+
"nonexistent",
|
|
121
|
+
false,
|
|
122
|
+
mockLogger as unknown as Console,
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
expect(result.ok).toBe(false);
|
|
126
|
+
expect(result.error).toContain("Account not found: nonexistent");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("should return error when account not configured", async () => {
|
|
130
|
+
const { getAccountConfig } = await import("./config.js");
|
|
131
|
+
const { isAccountConfigured } = await import("./utils/twitch.js");
|
|
132
|
+
|
|
133
|
+
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
|
|
134
|
+
vi.mocked(isAccountConfigured).mockReturnValue(false);
|
|
135
|
+
|
|
136
|
+
const result = await sendMessageTwitchInternal(
|
|
137
|
+
"#testchannel",
|
|
138
|
+
"Hello!",
|
|
139
|
+
mockConfig,
|
|
140
|
+
"default",
|
|
141
|
+
false,
|
|
142
|
+
mockLogger as unknown as Console,
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
expect(result.ok).toBe(false);
|
|
146
|
+
expect(result.error).toContain("not properly configured");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("should return error when no channel specified", async () => {
|
|
150
|
+
const { getAccountConfig } = await import("./config.js");
|
|
151
|
+
const { isAccountConfigured } = await import("./utils/twitch.js");
|
|
152
|
+
|
|
153
|
+
// Set channel to undefined to trigger the error (bypassing type check)
|
|
154
|
+
const accountWithoutChannel = {
|
|
155
|
+
...mockAccount,
|
|
156
|
+
channel: undefined as unknown as string,
|
|
157
|
+
};
|
|
158
|
+
vi.mocked(getAccountConfig).mockReturnValue(accountWithoutChannel);
|
|
159
|
+
vi.mocked(isAccountConfigured).mockReturnValue(true);
|
|
160
|
+
|
|
161
|
+
const result = await sendMessageTwitchInternal(
|
|
162
|
+
"",
|
|
163
|
+
"Hello!",
|
|
164
|
+
mockConfig,
|
|
165
|
+
"default",
|
|
166
|
+
false,
|
|
167
|
+
mockLogger as unknown as Console,
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
expect(result.ok).toBe(false);
|
|
171
|
+
expect(result.error).toContain("No channel specified");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("should skip sending empty message after markdown stripping", async () => {
|
|
175
|
+
const { getAccountConfig } = await import("./config.js");
|
|
176
|
+
const { isAccountConfigured } = await import("./utils/twitch.js");
|
|
177
|
+
const { stripMarkdownForTwitch } = await import("./utils/markdown.js");
|
|
178
|
+
|
|
179
|
+
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
|
|
180
|
+
vi.mocked(isAccountConfigured).mockReturnValue(true);
|
|
181
|
+
vi.mocked(stripMarkdownForTwitch).mockReturnValue("");
|
|
182
|
+
|
|
183
|
+
const result = await sendMessageTwitchInternal(
|
|
184
|
+
"#testchannel",
|
|
185
|
+
"**Only markdown**",
|
|
186
|
+
mockConfig,
|
|
187
|
+
"default",
|
|
188
|
+
true,
|
|
189
|
+
mockLogger as unknown as Console,
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
expect(result.ok).toBe(true);
|
|
193
|
+
expect(result.messageId).toBe("skipped");
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("should return error when client manager not found", async () => {
|
|
197
|
+
const { getAccountConfig } = await import("./config.js");
|
|
198
|
+
const { isAccountConfigured } = await import("./utils/twitch.js");
|
|
199
|
+
const { getClientManager } = await import("./client-manager-registry.js");
|
|
200
|
+
|
|
201
|
+
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
|
|
202
|
+
vi.mocked(isAccountConfigured).mockReturnValue(true);
|
|
203
|
+
vi.mocked(getClientManager).mockReturnValue(undefined);
|
|
204
|
+
|
|
205
|
+
const result = await sendMessageTwitchInternal(
|
|
206
|
+
"#testchannel",
|
|
207
|
+
"Hello!",
|
|
208
|
+
mockConfig,
|
|
209
|
+
"default",
|
|
210
|
+
false,
|
|
211
|
+
mockLogger as unknown as Console,
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
expect(result.ok).toBe(false);
|
|
215
|
+
expect(result.error).toContain("Client manager not found");
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("should handle send errors gracefully", async () => {
|
|
219
|
+
const { getAccountConfig } = await import("./config.js");
|
|
220
|
+
const { isAccountConfigured } = await import("./utils/twitch.js");
|
|
221
|
+
const { getClientManager } = await import("./client-manager-registry.js");
|
|
222
|
+
|
|
223
|
+
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
|
|
224
|
+
vi.mocked(isAccountConfigured).mockReturnValue(true);
|
|
225
|
+
vi.mocked(getClientManager).mockReturnValue({
|
|
226
|
+
sendMessage: vi.fn().mockRejectedValue(new Error("Connection lost")),
|
|
227
|
+
} as unknown as ReturnType<typeof getClientManager>);
|
|
228
|
+
|
|
229
|
+
const result = await sendMessageTwitchInternal(
|
|
230
|
+
"#testchannel",
|
|
231
|
+
"Hello!",
|
|
232
|
+
mockConfig,
|
|
233
|
+
"default",
|
|
234
|
+
false,
|
|
235
|
+
mockLogger as unknown as Console,
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
expect(result.ok).toBe(false);
|
|
239
|
+
expect(result.error).toBe("Connection lost");
|
|
240
|
+
expect(mockLogger.error).toHaveBeenCalled();
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("should use account channel when channel parameter is empty", async () => {
|
|
244
|
+
const { getAccountConfig } = await import("./config.js");
|
|
245
|
+
const { isAccountConfigured } = await import("./utils/twitch.js");
|
|
246
|
+
const { getClientManager } = await import("./client-manager-registry.js");
|
|
247
|
+
|
|
248
|
+
vi.mocked(getAccountConfig).mockReturnValue(mockAccount);
|
|
249
|
+
vi.mocked(isAccountConfigured).mockReturnValue(true);
|
|
250
|
+
const mockSend = vi.fn().mockResolvedValue({
|
|
251
|
+
ok: true,
|
|
252
|
+
messageId: "twitch-msg-789",
|
|
253
|
+
});
|
|
254
|
+
vi.mocked(getClientManager).mockReturnValue({
|
|
255
|
+
sendMessage: mockSend,
|
|
256
|
+
} as unknown as ReturnType<typeof getClientManager>);
|
|
257
|
+
|
|
258
|
+
await sendMessageTwitchInternal(
|
|
259
|
+
"",
|
|
260
|
+
"Hello!",
|
|
261
|
+
mockConfig,
|
|
262
|
+
"default",
|
|
263
|
+
false,
|
|
264
|
+
mockLogger as unknown as Console,
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
expect(mockSend).toHaveBeenCalledWith(
|
|
268
|
+
mockAccount,
|
|
269
|
+
"testchannel", // normalized account channel
|
|
270
|
+
"Hello!",
|
|
271
|
+
mockConfig,
|
|
272
|
+
"default",
|
|
273
|
+
);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
});
|
package/src/send.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Twitch message sending functions with dependency injection support.
|
|
3
|
+
*
|
|
4
|
+
* These functions are the primary interface for sending messages to Twitch.
|
|
5
|
+
* They support dependency injection via the `deps` parameter for testability.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk";
|
|
9
|
+
import { getClientManager as getRegistryClientManager } from "./client-manager-registry.js";
|
|
10
|
+
import { DEFAULT_ACCOUNT_ID, getAccountConfig } from "./config.js";
|
|
11
|
+
import { resolveTwitchToken } from "./token.js";
|
|
12
|
+
import { stripMarkdownForTwitch } from "./utils/markdown.js";
|
|
13
|
+
import { generateMessageId, isAccountConfigured, normalizeTwitchChannel } from "./utils/twitch.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Result from sending a message to Twitch.
|
|
17
|
+
*/
|
|
18
|
+
export interface SendMessageResult {
|
|
19
|
+
/** Whether the send was successful */
|
|
20
|
+
ok: boolean;
|
|
21
|
+
/** The message ID (generated for tracking) */
|
|
22
|
+
messageId: string;
|
|
23
|
+
/** Error message if the send failed */
|
|
24
|
+
error?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Internal send function used by the outbound adapter.
|
|
29
|
+
*
|
|
30
|
+
* This function has access to the full OpenClaw config and handles
|
|
31
|
+
* account resolution, markdown stripping, and actual message sending.
|
|
32
|
+
*
|
|
33
|
+
* @param channel - The channel name
|
|
34
|
+
* @param text - The message text
|
|
35
|
+
* @param cfg - Full OpenClaw configuration
|
|
36
|
+
* @param accountId - Account ID to use
|
|
37
|
+
* @param stripMarkdown - Whether to strip markdown (default: true)
|
|
38
|
+
* @param logger - Logger instance
|
|
39
|
+
* @returns Result with message ID and status
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const result = await sendMessageTwitchInternal(
|
|
43
|
+
* "#mychannel",
|
|
44
|
+
* "Hello Twitch!",
|
|
45
|
+
* openclawConfig,
|
|
46
|
+
* "default",
|
|
47
|
+
* true,
|
|
48
|
+
* console,
|
|
49
|
+
* );
|
|
50
|
+
*/
|
|
51
|
+
export async function sendMessageTwitchInternal(
|
|
52
|
+
channel: string,
|
|
53
|
+
text: string,
|
|
54
|
+
cfg: OpenClawConfig,
|
|
55
|
+
accountId: string = DEFAULT_ACCOUNT_ID,
|
|
56
|
+
stripMarkdown: boolean = true,
|
|
57
|
+
logger: Console = console,
|
|
58
|
+
): Promise<SendMessageResult> {
|
|
59
|
+
const account = getAccountConfig(cfg, accountId);
|
|
60
|
+
if (!account) {
|
|
61
|
+
const availableIds = Object.keys(cfg.channels?.twitch?.accounts ?? {});
|
|
62
|
+
return {
|
|
63
|
+
ok: false,
|
|
64
|
+
messageId: generateMessageId(),
|
|
65
|
+
error: `Account not found: ${accountId}. Available accounts: ${availableIds.join(", ") || "none"}`,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const tokenResolution = resolveTwitchToken(cfg, { accountId });
|
|
70
|
+
if (!isAccountConfigured(account, tokenResolution.token)) {
|
|
71
|
+
return {
|
|
72
|
+
ok: false,
|
|
73
|
+
messageId: generateMessageId(),
|
|
74
|
+
error:
|
|
75
|
+
`Account ${accountId} is not properly configured. ` +
|
|
76
|
+
"Required: username, clientId, and token (config or env for default account).",
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const normalizedChannel = channel || account.channel;
|
|
81
|
+
if (!normalizedChannel) {
|
|
82
|
+
return {
|
|
83
|
+
ok: false,
|
|
84
|
+
messageId: generateMessageId(),
|
|
85
|
+
error: "No channel specified and no default channel in account config",
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const cleanedText = stripMarkdown ? stripMarkdownForTwitch(text) : text;
|
|
90
|
+
if (!cleanedText) {
|
|
91
|
+
return {
|
|
92
|
+
ok: true,
|
|
93
|
+
messageId: "skipped",
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const clientManager = getRegistryClientManager(accountId);
|
|
98
|
+
if (!clientManager) {
|
|
99
|
+
return {
|
|
100
|
+
ok: false,
|
|
101
|
+
messageId: generateMessageId(),
|
|
102
|
+
error: `Client manager not found for account: ${accountId}. Please start the Twitch gateway first.`,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
const result = await clientManager.sendMessage(
|
|
108
|
+
account,
|
|
109
|
+
normalizeTwitchChannel(normalizedChannel),
|
|
110
|
+
cleanedText,
|
|
111
|
+
cfg,
|
|
112
|
+
accountId,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
if (!result.ok) {
|
|
116
|
+
return {
|
|
117
|
+
ok: false,
|
|
118
|
+
messageId: result.messageId ?? generateMessageId(),
|
|
119
|
+
error: result.error ?? "Send failed",
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
ok: true,
|
|
125
|
+
messageId: result.messageId ?? generateMessageId(),
|
|
126
|
+
};
|
|
127
|
+
} catch (error) {
|
|
128
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
129
|
+
logger.error(`Failed to send message: ${errorMsg}`);
|
|
130
|
+
return {
|
|
131
|
+
ok: false,
|
|
132
|
+
messageId: generateMessageId(),
|
|
133
|
+
error: errorMsg,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|