@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.
@@ -0,0 +1,277 @@
1
+ import { RefreshingAuthProvider, StaticAuthProvider } from "@twurple/auth";
2
+ import { ChatClient, LogLevel } from "@twurple/chat";
3
+ import type { OpenClawConfig } from "openclaw/plugin-sdk";
4
+ import { resolveTwitchToken } from "./token.js";
5
+ import type { ChannelLogSink, TwitchAccountConfig, TwitchChatMessage } from "./types.js";
6
+ import { normalizeToken } from "./utils/twitch.js";
7
+
8
+ /**
9
+ * Manages Twitch chat client connections
10
+ */
11
+ export class TwitchClientManager {
12
+ private clients = new Map<string, ChatClient>();
13
+ private messageHandlers = new Map<string, (message: TwitchChatMessage) => void>();
14
+
15
+ constructor(private logger: ChannelLogSink) {}
16
+
17
+ /**
18
+ * Create an auth provider for the account.
19
+ */
20
+ private async createAuthProvider(
21
+ account: TwitchAccountConfig,
22
+ normalizedToken: string,
23
+ ): Promise<StaticAuthProvider | RefreshingAuthProvider> {
24
+ if (!account.clientId) {
25
+ throw new Error("Missing Twitch client ID");
26
+ }
27
+
28
+ if (account.clientSecret) {
29
+ const authProvider = new RefreshingAuthProvider({
30
+ clientId: account.clientId,
31
+ clientSecret: account.clientSecret,
32
+ });
33
+
34
+ await authProvider
35
+ .addUserForToken({
36
+ accessToken: normalizedToken,
37
+ refreshToken: account.refreshToken ?? null,
38
+ expiresIn: account.expiresIn ?? null,
39
+ obtainmentTimestamp: account.obtainmentTimestamp ?? Date.now(),
40
+ })
41
+ .then((userId) => {
42
+ this.logger.info(
43
+ `Added user ${userId} to RefreshingAuthProvider for ${account.username}`,
44
+ );
45
+ })
46
+ .catch((err) => {
47
+ this.logger.error(
48
+ `Failed to add user to RefreshingAuthProvider: ${err instanceof Error ? err.message : String(err)}`,
49
+ );
50
+ });
51
+
52
+ authProvider.onRefresh((userId, token) => {
53
+ this.logger.info(
54
+ `Access token refreshed for user ${userId} (expires in ${token.expiresIn ? `${token.expiresIn}s` : "unknown"})`,
55
+ );
56
+ });
57
+
58
+ authProvider.onRefreshFailure((userId, error) => {
59
+ this.logger.error(`Failed to refresh access token for user ${userId}: ${error.message}`);
60
+ });
61
+
62
+ const refreshStatus = account.refreshToken
63
+ ? "automatic token refresh enabled"
64
+ : "token refresh disabled (no refresh token)";
65
+ this.logger.info(`Using RefreshingAuthProvider for ${account.username} (${refreshStatus})`);
66
+
67
+ return authProvider;
68
+ }
69
+
70
+ this.logger.info(`Using StaticAuthProvider for ${account.username} (no clientSecret provided)`);
71
+ return new StaticAuthProvider(account.clientId, normalizedToken);
72
+ }
73
+
74
+ /**
75
+ * Get or create a chat client for an account
76
+ */
77
+ async getClient(
78
+ account: TwitchAccountConfig,
79
+ cfg?: OpenClawConfig,
80
+ accountId?: string,
81
+ ): Promise<ChatClient> {
82
+ const key = this.getAccountKey(account);
83
+
84
+ const existing = this.clients.get(key);
85
+ if (existing) {
86
+ return existing;
87
+ }
88
+
89
+ const tokenResolution = resolveTwitchToken(cfg, {
90
+ accountId,
91
+ });
92
+
93
+ if (!tokenResolution.token) {
94
+ this.logger.error(
95
+ `Missing Twitch token for account ${account.username} (set channels.twitch.accounts.${account.username}.token or OPENCLAW_TWITCH_ACCESS_TOKEN for default)`,
96
+ );
97
+ throw new Error("Missing Twitch token");
98
+ }
99
+
100
+ this.logger.debug?.(`Using ${tokenResolution.source} token source for ${account.username}`);
101
+
102
+ if (!account.clientId) {
103
+ this.logger.error(`Missing Twitch client ID for account ${account.username}`);
104
+ throw new Error("Missing Twitch client ID");
105
+ }
106
+
107
+ const normalizedToken = normalizeToken(tokenResolution.token);
108
+
109
+ const authProvider = await this.createAuthProvider(account, normalizedToken);
110
+
111
+ const client = new ChatClient({
112
+ authProvider,
113
+ channels: [account.channel],
114
+ rejoinChannelsOnReconnect: true,
115
+ requestMembershipEvents: true,
116
+ logger: {
117
+ minLevel: LogLevel.WARNING,
118
+ custom: {
119
+ log: (level, message) => {
120
+ switch (level) {
121
+ case LogLevel.CRITICAL:
122
+ this.logger.error(message);
123
+ break;
124
+ case LogLevel.ERROR:
125
+ this.logger.error(message);
126
+ break;
127
+ case LogLevel.WARNING:
128
+ this.logger.warn(message);
129
+ break;
130
+ case LogLevel.INFO:
131
+ this.logger.info(message);
132
+ break;
133
+ case LogLevel.DEBUG:
134
+ this.logger.debug?.(message);
135
+ break;
136
+ case LogLevel.TRACE:
137
+ this.logger.debug?.(message);
138
+ break;
139
+ }
140
+ },
141
+ },
142
+ },
143
+ });
144
+
145
+ this.setupClientHandlers(client, account);
146
+
147
+ client.connect();
148
+
149
+ this.clients.set(key, client);
150
+ this.logger.info(`Connected to Twitch as ${account.username}`);
151
+
152
+ return client;
153
+ }
154
+
155
+ /**
156
+ * Set up message and event handlers for a client
157
+ */
158
+ private setupClientHandlers(client: ChatClient, account: TwitchAccountConfig): void {
159
+ const key = this.getAccountKey(account);
160
+
161
+ // Handle incoming messages
162
+ client.onMessage((channelName, _user, messageText, msg) => {
163
+ const handler = this.messageHandlers.get(key);
164
+ if (handler) {
165
+ const normalizedChannel = channelName.startsWith("#") ? channelName.slice(1) : channelName;
166
+ const from = `twitch:${msg.userInfo.userName}`;
167
+ const preview = messageText.slice(0, 100).replace(/\n/g, "\\n");
168
+ this.logger.debug?.(
169
+ `twitch inbound: channel=${normalizedChannel} from=${from} len=${messageText.length} preview="${preview}"`,
170
+ );
171
+
172
+ handler({
173
+ username: msg.userInfo.userName,
174
+ displayName: msg.userInfo.displayName,
175
+ userId: msg.userInfo.userId,
176
+ message: messageText,
177
+ channel: normalizedChannel,
178
+ id: msg.id,
179
+ timestamp: new Date(),
180
+ isMod: msg.userInfo.isMod,
181
+ isOwner: msg.userInfo.isBroadcaster,
182
+ isVip: msg.userInfo.isVip,
183
+ isSub: msg.userInfo.isSubscriber,
184
+ chatType: "group",
185
+ });
186
+ }
187
+ });
188
+
189
+ this.logger.info(`Set up handlers for ${key}`);
190
+ }
191
+
192
+ /**
193
+ * Set a message handler for an account
194
+ * @returns A function that removes the handler when called
195
+ */
196
+ onMessage(
197
+ account: TwitchAccountConfig,
198
+ handler: (message: TwitchChatMessage) => void,
199
+ ): () => void {
200
+ const key = this.getAccountKey(account);
201
+ this.messageHandlers.set(key, handler);
202
+ return () => {
203
+ this.messageHandlers.delete(key);
204
+ };
205
+ }
206
+
207
+ /**
208
+ * Disconnect a client
209
+ */
210
+ async disconnect(account: TwitchAccountConfig): Promise<void> {
211
+ const key = this.getAccountKey(account);
212
+ const client = this.clients.get(key);
213
+
214
+ if (client) {
215
+ client.quit();
216
+ this.clients.delete(key);
217
+ this.messageHandlers.delete(key);
218
+ this.logger.info(`Disconnected ${key}`);
219
+ }
220
+ }
221
+
222
+ /**
223
+ * Disconnect all clients
224
+ */
225
+ async disconnectAll(): Promise<void> {
226
+ this.clients.forEach((client) => client.quit());
227
+ this.clients.clear();
228
+ this.messageHandlers.clear();
229
+ this.logger.info(" Disconnected all clients");
230
+ }
231
+
232
+ /**
233
+ * Send a message to a channel
234
+ */
235
+ async sendMessage(
236
+ account: TwitchAccountConfig,
237
+ channel: string,
238
+ message: string,
239
+ cfg?: OpenClawConfig,
240
+ accountId?: string,
241
+ ): Promise<{ ok: boolean; error?: string; messageId?: string }> {
242
+ try {
243
+ const client = await this.getClient(account, cfg, accountId);
244
+
245
+ // Generate a message ID (Twurple's say() doesn't return the message ID, so we generate one)
246
+ const messageId = crypto.randomUUID();
247
+
248
+ // Send message (Twurple handles rate limiting)
249
+ await client.say(channel, message);
250
+
251
+ return { ok: true, messageId };
252
+ } catch (error) {
253
+ this.logger.error(
254
+ `Failed to send message: ${error instanceof Error ? error.message : String(error)}`,
255
+ );
256
+ return {
257
+ ok: false,
258
+ error: error instanceof Error ? error.message : String(error),
259
+ };
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Generate a unique key for an account
265
+ */
266
+ public getAccountKey(account: TwitchAccountConfig): string {
267
+ return `${account.username}:${account.channel}`;
268
+ }
269
+
270
+ /**
271
+ * Clear all clients and handlers (for testing)
272
+ */
273
+ _clearForTest(): void {
274
+ this.clients.clear();
275
+ this.messageHandlers.clear();
276
+ }
277
+ }
package/src/types.ts ADDED
@@ -0,0 +1,143 @@
1
+ /**
2
+ * Twitch channel plugin types.
3
+ *
4
+ * This file defines Twitch-specific types. Generic channel types are imported
5
+ * from OpenClaw core.
6
+ */
7
+
8
+ import type {
9
+ ChannelGatewayContext,
10
+ ChannelOutboundAdapter,
11
+ ChannelOutboundContext,
12
+ ChannelResolveKind,
13
+ ChannelResolveResult,
14
+ ChannelStatusAdapter,
15
+ } from "../../../src/channels/plugins/types.adapters.js";
16
+ import type {
17
+ ChannelAccountSnapshot,
18
+ ChannelCapabilities,
19
+ ChannelLogSink,
20
+ ChannelMessageActionAdapter,
21
+ ChannelMessageActionContext,
22
+ ChannelMeta,
23
+ } from "../../../src/channels/plugins/types.core.js";
24
+ import type { ChannelPlugin } from "../../../src/channels/plugins/types.plugin.js";
25
+ import type { OpenClawConfig } from "../../../src/config/config.js";
26
+ import type { OutboundDeliveryResult } from "../../../src/infra/outbound/deliver.js";
27
+ import type { RuntimeEnv } from "../../../src/runtime.js";
28
+
29
+ // ============================================================================
30
+ // Twitch-Specific Types
31
+ // ============================================================================
32
+
33
+ /**
34
+ * Twitch user roles that can be allowed to interact with the bot
35
+ */
36
+ export type TwitchRole = "moderator" | "owner" | "vip" | "subscriber" | "all";
37
+
38
+ /**
39
+ * Account configuration for a Twitch channel
40
+ */
41
+ export interface TwitchAccountConfig {
42
+ /** Twitch username */
43
+ username: string;
44
+ /** Twitch OAuth access token (requires chat:read and chat:write scopes) */
45
+ accessToken: string;
46
+ /** Twitch client ID (from Twitch Developer Portal or twitchtokengenerator.com) */
47
+ clientId: string;
48
+ /** Channel name to join (required) */
49
+ channel: string;
50
+ /** Enable this account */
51
+ enabled?: boolean;
52
+ /** Allowlist of Twitch user IDs who can interact with the bot (use IDs for safety, not usernames) */
53
+ allowFrom?: Array<string>;
54
+ /** Roles allowed to interact with the bot (e.g., ["mod", "vip", "sub"]) */
55
+ allowedRoles?: TwitchRole[];
56
+ /** Require @mention to trigger bot responses */
57
+ requireMention?: boolean;
58
+ /** Outbound response prefix override for this channel/account. */
59
+ responsePrefix?: string;
60
+ /** Twitch client secret (required for token refresh via RefreshingAuthProvider) */
61
+ clientSecret?: string;
62
+ /** Refresh token (required for automatic token refresh) */
63
+ refreshToken?: string;
64
+ /** Token expiry time in seconds (optional, for token refresh tracking) */
65
+ expiresIn?: number | null;
66
+ /** Timestamp when token was obtained (optional, for token refresh tracking) */
67
+ obtainmentTimestamp?: number;
68
+ }
69
+
70
+ /**
71
+ * Message target for Twitch
72
+ */
73
+ export interface TwitchTarget {
74
+ /** Account ID */
75
+ accountId: string;
76
+ /** Channel name (defaults to account's channel) */
77
+ channel?: string;
78
+ }
79
+
80
+ /**
81
+ * Twitch message from chat
82
+ */
83
+ export interface TwitchChatMessage {
84
+ /** Username of sender */
85
+ username: string;
86
+ /** Twitch user ID of sender (unique, persistent identifier) */
87
+ userId?: string;
88
+ /** Message text */
89
+ message: string;
90
+ /** Channel name */
91
+ channel: string;
92
+ /** Display name (may include special characters) */
93
+ displayName?: string;
94
+ /** Message ID */
95
+ id?: string;
96
+ /** Timestamp */
97
+ timestamp?: Date;
98
+ /** Whether the sender is a moderator */
99
+ isMod?: boolean;
100
+ /** Whether the sender is the channel owner/broadcaster */
101
+ isOwner?: boolean;
102
+ /** Whether the sender is a VIP */
103
+ isVip?: boolean;
104
+ /** Whether the sender is a subscriber */
105
+ isSub?: boolean;
106
+ /** Chat type */
107
+ chatType?: "group";
108
+ }
109
+
110
+ /**
111
+ * Send result from Twitch client
112
+ */
113
+ export interface SendResult {
114
+ ok: boolean;
115
+ error?: string;
116
+ messageId?: string;
117
+ }
118
+
119
+ // Re-export core types for convenience
120
+ export type {
121
+ ChannelAccountSnapshot,
122
+ ChannelGatewayContext,
123
+ ChannelLogSink,
124
+ ChannelMessageActionAdapter,
125
+ ChannelMessageActionContext,
126
+ ChannelMeta,
127
+ ChannelOutboundAdapter,
128
+ ChannelStatusAdapter,
129
+ ChannelCapabilities,
130
+ ChannelResolveKind,
131
+ ChannelResolveResult,
132
+ ChannelPlugin,
133
+ ChannelOutboundContext,
134
+ OutboundDeliveryResult,
135
+ };
136
+
137
+ import type { z } from "zod";
138
+ // Import and re-export the schema type
139
+ import type { TwitchConfigSchema } from "./config-schema.js";
140
+ export type TwitchConfig = z.infer<typeof TwitchConfigSchema>;
141
+
142
+ export type { OpenClawConfig };
143
+ export type { RuntimeEnv };
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Markdown utilities for Twitch chat
3
+ *
4
+ * Twitch chat doesn't support markdown formatting, so we strip it before sending.
5
+ * Based on OpenClaw's markdownToText in src/agents/tools/web-fetch-utils.ts.
6
+ */
7
+
8
+ /**
9
+ * Strip markdown formatting from text for Twitch compatibility.
10
+ *
11
+ * Removes images, links, bold, italic, strikethrough, code blocks, inline code,
12
+ * headers, and list formatting. Replaces newlines with spaces since Twitch
13
+ * is a single-line chat medium.
14
+ *
15
+ * @param markdown - The markdown text to strip
16
+ * @returns Plain text with markdown removed
17
+ */
18
+ export function stripMarkdownForTwitch(markdown: string): string {
19
+ return (
20
+ markdown
21
+ // Images
22
+ .replace(/!\[[^\]]*]\([^)]+\)/g, "")
23
+ // Links
24
+ .replace(/\[([^\]]+)]\([^)]+\)/g, "$1")
25
+ // Bold (**text**)
26
+ .replace(/\*\*([^*]+)\*\*/g, "$1")
27
+ // Bold (__text__)
28
+ .replace(/__([^_]+)__/g, "$1")
29
+ // Italic (*text*)
30
+ .replace(/\*([^*]+)\*/g, "$1")
31
+ // Italic (_text_)
32
+ .replace(/_([^_]+)_/g, "$1")
33
+ // Strikethrough (~~text~~)
34
+ .replace(/~~([^~]+)~~/g, "$1")
35
+ // Code blocks
36
+ .replace(/```[\s\S]*?```/g, (block) => block.replace(/```[^\n]*\n?/g, "").replace(/```/g, ""))
37
+ // Inline code
38
+ .replace(/`([^`]+)`/g, "$1")
39
+ // Headers
40
+ .replace(/^#{1,6}\s+/gm, "")
41
+ // Lists
42
+ .replace(/^\s*[-*+]\s+/gm, "")
43
+ .replace(/^\s*\d+\.\s+/gm, "")
44
+ // Normalize whitespace
45
+ .replace(/\r/g, "") // Remove carriage returns
46
+ .replace(/[ \t]+\n/g, "\n") // Remove trailing spaces before newlines
47
+ .replace(/\n/g, " ") // Replace newlines with spaces (for Twitch)
48
+ .replace(/[ \t]{2,}/g, " ") // Reduce multiple spaces to single
49
+ .trim()
50
+ );
51
+ }
52
+
53
+ /**
54
+ * Simple word-boundary chunker for Twitch (500 char limit).
55
+ * Strips markdown before chunking to avoid breaking markdown patterns.
56
+ *
57
+ * @param text - The text to chunk
58
+ * @param limit - Maximum characters per chunk (Twitch limit is 500)
59
+ * @returns Array of text chunks
60
+ */
61
+ export function chunkTextForTwitch(text: string, limit: number): string[] {
62
+ // First, strip markdown
63
+ const cleaned = stripMarkdownForTwitch(text);
64
+ if (!cleaned) {
65
+ return [];
66
+ }
67
+ if (limit <= 0) {
68
+ return [cleaned];
69
+ }
70
+ if (cleaned.length <= limit) {
71
+ return [cleaned];
72
+ }
73
+
74
+ const chunks: string[] = [];
75
+ let remaining = cleaned;
76
+
77
+ while (remaining.length > limit) {
78
+ // Find the last space before the limit
79
+ const window = remaining.slice(0, limit);
80
+ const lastSpaceIndex = window.lastIndexOf(" ");
81
+
82
+ if (lastSpaceIndex === -1) {
83
+ // No space found, hard split at limit
84
+ chunks.push(window);
85
+ remaining = remaining.slice(limit);
86
+ } else {
87
+ // Split at the last space
88
+ chunks.push(window.slice(0, lastSpaceIndex));
89
+ remaining = remaining.slice(lastSpaceIndex + 1);
90
+ }
91
+ }
92
+
93
+ if (remaining) {
94
+ chunks.push(remaining);
95
+ }
96
+
97
+ return chunks;
98
+ }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Twitch-specific utility functions
3
+ */
4
+
5
+ /**
6
+ * Normalize Twitch channel names.
7
+ *
8
+ * Removes the '#' prefix if present, converts to lowercase, and trims whitespace.
9
+ * Twitch channel names are case-insensitive and don't use the '#' prefix in the API.
10
+ *
11
+ * @param channel - The channel name to normalize
12
+ * @returns Normalized channel name
13
+ *
14
+ * @example
15
+ * normalizeTwitchChannel("#TwitchChannel") // "twitchchannel"
16
+ * normalizeTwitchChannel("MyChannel") // "mychannel"
17
+ */
18
+ export function normalizeTwitchChannel(channel: string): string {
19
+ const trimmed = channel.trim().toLowerCase();
20
+ return trimmed.startsWith("#") ? trimmed.slice(1) : trimmed;
21
+ }
22
+
23
+ /**
24
+ * Create a standardized error message for missing target.
25
+ *
26
+ * @param provider - The provider name (e.g., "Twitch")
27
+ * @param hint - Optional hint for how to fix the issue
28
+ * @returns Error object with descriptive message
29
+ */
30
+ export function missingTargetError(provider: string, hint?: string): Error {
31
+ return new Error(`Delivering to ${provider} requires target${hint ? ` ${hint}` : ""}`);
32
+ }
33
+
34
+ /**
35
+ * Generate a unique message ID for Twitch messages.
36
+ *
37
+ * Twurple's say() doesn't return the message ID, so we generate one
38
+ * for tracking purposes.
39
+ *
40
+ * @returns A unique message ID
41
+ */
42
+ export function generateMessageId(): string {
43
+ return `${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
44
+ }
45
+
46
+ /**
47
+ * Normalize OAuth token by removing the "oauth:" prefix if present.
48
+ *
49
+ * Twurple doesn't require the "oauth:" prefix, so we strip it for consistency.
50
+ *
51
+ * @param token - The OAuth token to normalize
52
+ * @returns Normalized token without "oauth:" prefix
53
+ *
54
+ * @example
55
+ * normalizeToken("oauth:abc123") // "abc123"
56
+ * normalizeToken("abc123") // "abc123"
57
+ */
58
+ export function normalizeToken(token: string): string {
59
+ return token.startsWith("oauth:") ? token.slice(6) : token;
60
+ }
61
+
62
+ /**
63
+ * Check if an account is properly configured with required credentials.
64
+ *
65
+ * @param account - The Twitch account config to check
66
+ * @returns true if the account has required credentials
67
+ */
68
+ export function isAccountConfigured(
69
+ account: {
70
+ username?: string;
71
+ accessToken?: string;
72
+ clientId?: string;
73
+ },
74
+ resolvedToken?: string | null,
75
+ ): boolean {
76
+ const token = resolvedToken ?? account?.accessToken;
77
+ return Boolean(account?.username && token && account?.clientId);
78
+ }
package/test/setup.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Vitest setup file for Twitch plugin tests.
3
+ *
4
+ * Re-exports the root test setup to avoid duplication.
5
+ */
6
+
7
+ export * from "../../../test/setup.js";