@openclaw/sms 2026.7.2-beta.5 → 2026.8.1-beta.2
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/README.md +2 -2
- package/dist/channel-plugin-api.js +850 -524
- package/dist/index.js +1 -1
- package/dist/media-C_8jX0L8.js +404 -0
- package/dist/twilio-QS1jkUGH.js +456 -0
- package/openclaw.plugin.json +57 -5
- package/package.json +7 -7
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { isIP } from "node:net";
|
|
2
|
+
import { fetchWithSsrFGuard, isBlockedHostnameOrIp } from "openclaw/plugin-sdk/ssrf-runtime";
|
|
3
|
+
import { createHmac } from "node:crypto";
|
|
4
|
+
import { readRequestBodyWithLimit } from "openclaw/plugin-sdk/webhook-ingress";
|
|
5
|
+
import * as querystring from "node:querystring";
|
|
6
|
+
import { readResponseTextPrefix, readResponseWithLimit } from "openclaw/plugin-sdk/response-limit-runtime";
|
|
7
|
+
import { safeEqualSecret } from "openclaw/plugin-sdk/security-runtime";
|
|
8
|
+
//#region extensions/sms/src/phone.ts
|
|
9
|
+
function normalizeSmsPhoneNumber(raw) {
|
|
10
|
+
const trimmed = raw.trim().replace(/^(?:sms|twilio-sms):/i, "").trim();
|
|
11
|
+
if (!trimmed) return "";
|
|
12
|
+
return (trimmed.startsWith("+") ? trimmed : `+${trimmed}`).replace(/[^\d+]/g, "");
|
|
13
|
+
}
|
|
14
|
+
function looksLikeSmsPhoneNumber(raw) {
|
|
15
|
+
const normalized = normalizeSmsPhoneNumber(raw);
|
|
16
|
+
return /^\+[1-9]\d{6,14}$/.test(normalized);
|
|
17
|
+
}
|
|
18
|
+
function normalizeSmsAllowFrom(raw) {
|
|
19
|
+
if (raw.trim() === "*") return "*";
|
|
20
|
+
return normalizeSmsPhoneNumber(raw).toLowerCase();
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region extensions/sms/src/public-webhook-url.ts
|
|
24
|
+
const SMS_STATUS_CALLBACK_MAX_LENGTH = 4e3;
|
|
25
|
+
const SMS_STATUS_CALLBACK_READ_TIMEOUT_MS = 5e3;
|
|
26
|
+
const TWILIO_READ_TIMEOUT_MIN_MS = 100;
|
|
27
|
+
const TWILIO_READ_TIMEOUT_MAX_MS = 15e3;
|
|
28
|
+
const ABSOLUTE_HTTP_URL_PATTERN = /^https?:\/\//iu;
|
|
29
|
+
const RAW_AUTHORITY_PATTERN = /^[a-z0-9.:[\]-]+$/iu;
|
|
30
|
+
const RAW_PATH_QUERY_FRAGMENT_PATTERN = /^[a-z0-9\-._~%!$&'()*+,;=:@/?]*$/iu;
|
|
31
|
+
const INVALID_PERCENT_ESCAPE_PATTERN = /%(?![0-9a-f]{2})/iu;
|
|
32
|
+
function hasForbiddenRawUrlCharacter(value) {
|
|
33
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
34
|
+
const codePoint = value.charCodeAt(index);
|
|
35
|
+
if (codePoint <= 32 || codePoint === 92 || codePoint === 127) return true;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
function hasSafeRawUrlCharacters(value) {
|
|
40
|
+
const match = /^https?:\/\/([^/?#]*)([^#]*)(?:#(.*))?$/iu.exec(value);
|
|
41
|
+
if (!match) return false;
|
|
42
|
+
return RAW_AUTHORITY_PATTERN.test(match[1] ?? "") && RAW_PATH_QUERY_FRAGMENT_PATTERN.test(match[2] ?? "") && RAW_PATH_QUERY_FRAGMENT_PATTERN.test(match[3] ?? "");
|
|
43
|
+
}
|
|
44
|
+
function hasValidHostname(url) {
|
|
45
|
+
const hostname = url.hostname.startsWith("[") && url.hostname.endsWith("]") ? url.hostname.slice(1, -1) : url.hostname;
|
|
46
|
+
if (isBlockedHostnameOrIp(hostname)) return false;
|
|
47
|
+
if (isIP(hostname) !== 0) return true;
|
|
48
|
+
const normalized = hostname.endsWith(".") ? hostname.slice(0, -1) : hostname;
|
|
49
|
+
if (!normalized.includes(".") || normalized.length > 253) return false;
|
|
50
|
+
return normalized.split(".").every((label) => label.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/iu.test(label));
|
|
51
|
+
}
|
|
52
|
+
function parseSmsPublicWebhookUrl(value) {
|
|
53
|
+
const trimmed = value.trim();
|
|
54
|
+
if (!ABSOLUTE_HTTP_URL_PATTERN.test(trimmed) || !hasSafeRawUrlCharacters(trimmed) || INVALID_PERCENT_ESCAPE_PATTERN.test(trimmed) || hasForbiddenRawUrlCharacter(trimmed)) return;
|
|
55
|
+
let url;
|
|
56
|
+
try {
|
|
57
|
+
url = new URL(trimmed);
|
|
58
|
+
} catch {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (url.protocol !== "http:" && url.protocol !== "https:" || url.username || url.password || !hasValidHostname(url)) return;
|
|
62
|
+
return url;
|
|
63
|
+
}
|
|
64
|
+
function parseTwilioRetryPolicies(overrides) {
|
|
65
|
+
const policies = overrides.getAll("rp").flatMap((value) => value.split(",")).map((value) => value.trim()).filter(Boolean);
|
|
66
|
+
return policies.length > 0 ? [...new Set(policies)] : ["ct"];
|
|
67
|
+
}
|
|
68
|
+
function resolveTwilioStatusCallbackUrl(publicWebhookUrl) {
|
|
69
|
+
const trimmed = publicWebhookUrl.trim();
|
|
70
|
+
if (!trimmed || !parseSmsPublicWebhookUrl(trimmed)) return "";
|
|
71
|
+
const hashIndex = trimmed.indexOf("#");
|
|
72
|
+
const baseUrl = hashIndex === -1 ? trimmed : trimmed.slice(0, hashIndex);
|
|
73
|
+
const overrides = new URLSearchParams(hashIndex === -1 ? "" : trimmed.slice(hashIndex + 1));
|
|
74
|
+
const retryPolicies = parseTwilioRetryPolicies(overrides);
|
|
75
|
+
const normalizedRetryPolicies = new Set(retryPolicies.map((policy) => policy.toLowerCase()));
|
|
76
|
+
if (!normalizedRetryPolicies.has("all")) {
|
|
77
|
+
for (const requiredPolicy of [
|
|
78
|
+
"ct",
|
|
79
|
+
"rt",
|
|
80
|
+
"5xx"
|
|
81
|
+
]) if (!normalizedRetryPolicies.has(requiredPolicy)) retryPolicies.push(requiredPolicy);
|
|
82
|
+
}
|
|
83
|
+
overrides.set("rp", retryPolicies.join(","));
|
|
84
|
+
const configuredReadTimeout = Number(overrides.get("rt"));
|
|
85
|
+
const readTimeout = Number.isInteger(configuredReadTimeout) && configuredReadTimeout >= TWILIO_READ_TIMEOUT_MIN_MS && configuredReadTimeout <= TWILIO_READ_TIMEOUT_MAX_MS ? configuredReadTimeout : SMS_STATUS_CALLBACK_READ_TIMEOUT_MS;
|
|
86
|
+
overrides.set("rt", String(readTimeout));
|
|
87
|
+
const configuredRetryCount = Number(overrides.get("rc"));
|
|
88
|
+
const retryCount = Number.isInteger(configuredRetryCount) && configuredRetryCount >= 1 && configuredRetryCount <= 5 ? configuredRetryCount : 1;
|
|
89
|
+
overrides.set("rc", String(retryCount));
|
|
90
|
+
const callbackUrl = `${baseUrl}#${overrides.toString().replaceAll("%2C", ",")}`;
|
|
91
|
+
if (callbackUrl.length > SMS_STATUS_CALLBACK_MAX_LENGTH || !parseSmsPublicWebhookUrl(callbackUrl)) return "";
|
|
92
|
+
return callbackUrl;
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region extensions/sms/src/twilio.ts
|
|
96
|
+
const TWILIO_ACCOUNTS_URL = "https://api.twilio.com/2010-04-01/Accounts";
|
|
97
|
+
const TWILIO_MESSAGING_URL = "https://messaging.twilio.com/v1";
|
|
98
|
+
const TWILIO_API_HOSTNAME = "api.twilio.com";
|
|
99
|
+
const TWILIO_MESSAGING_HOSTNAME = "messaging.twilio.com";
|
|
100
|
+
const TWILIO_API_TIMEOUT_MS = 3e4;
|
|
101
|
+
const TWILIO_API_SUCCESS_BODY_LIMIT_BYTES = 1 * 1024 * 1024;
|
|
102
|
+
const TWILIO_API_ERROR_BODY_LIMIT_BYTES = 8 * 1024;
|
|
103
|
+
const TRUNCATED_RESPONSE_SUFFIX = "... [truncated]";
|
|
104
|
+
const WEBHOOK_BODY_LIMIT_BYTES = 32 * 1024;
|
|
105
|
+
const WEBHOOK_BODY_TIMEOUT_MS = 5e3;
|
|
106
|
+
const TWILIO_MESSAGE_BODY_MAX_LENGTH = 1600;
|
|
107
|
+
const TWILIO_MMS_MAX_OUTBOUND_MEDIA_COUNT = 10;
|
|
108
|
+
const TWILIO_MMS_MAX_BYTES = 5 * 1024 * 1024;
|
|
109
|
+
const SMS_MAX_INBOUND_MEDIA_DOWNLOADS = 10;
|
|
110
|
+
const TWILIO_CHANNEL_ADDRESS_RE = /^([a-z][a-z0-9-]*):(.*)$/i;
|
|
111
|
+
function firstString(value) {
|
|
112
|
+
if (Array.isArray(value)) return firstString(value[0]);
|
|
113
|
+
return typeof value === "string" ? value : "";
|
|
114
|
+
}
|
|
115
|
+
function firstTrimmedString(value) {
|
|
116
|
+
return firstString(value).trim();
|
|
117
|
+
}
|
|
118
|
+
function firstStringish(value) {
|
|
119
|
+
const first = Array.isArray(value) ? value[0] : value;
|
|
120
|
+
if (typeof first === "string") return first;
|
|
121
|
+
return typeof first === "number" ? String(first) : "";
|
|
122
|
+
}
|
|
123
|
+
function parseTwilioApiError(text) {
|
|
124
|
+
try {
|
|
125
|
+
const parsed = JSON.parse(text);
|
|
126
|
+
if (!parsed || typeof parsed !== "object") return {};
|
|
127
|
+
const record = parsed;
|
|
128
|
+
return {
|
|
129
|
+
code: typeof record.code === "number" ? record.code : void 0,
|
|
130
|
+
message: typeof record.message === "string" ? record.message : void 0
|
|
131
|
+
};
|
|
132
|
+
} catch {
|
|
133
|
+
return {};
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function parseTwilioSuccessPayload(text) {
|
|
137
|
+
if (!text.trim()) return {};
|
|
138
|
+
try {
|
|
139
|
+
const parsed = JSON.parse(text);
|
|
140
|
+
if (!parsed || typeof parsed !== "object") throw new Error("Twilio SMS send returned malformed JSON.");
|
|
141
|
+
const record = parsed;
|
|
142
|
+
return {
|
|
143
|
+
sid: typeof record.sid === "string" ? record.sid : void 0,
|
|
144
|
+
to: typeof record.to === "string" ? record.to : void 0,
|
|
145
|
+
from: typeof record.from === "string" ? record.from : void 0,
|
|
146
|
+
status: typeof record.status === "string" ? record.status : void 0
|
|
147
|
+
};
|
|
148
|
+
} catch (cause) {
|
|
149
|
+
if (cause instanceof Error && cause.message === "Twilio SMS send returned malformed JSON.") throw cause;
|
|
150
|
+
throw new Error("Twilio SMS send returned malformed JSON.", { cause });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function requestSearch(req) {
|
|
154
|
+
try {
|
|
155
|
+
return new URL(req.url ?? "/", "http://localhost").search;
|
|
156
|
+
} catch {
|
|
157
|
+
return "";
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function stripUrlFragment(url) {
|
|
161
|
+
const hashIndex = url.indexOf("#");
|
|
162
|
+
return hashIndex === -1 ? url : url.slice(0, hashIndex);
|
|
163
|
+
}
|
|
164
|
+
function resolveTwilioWebhookSignatureUrl(params) {
|
|
165
|
+
const signatureBaseUrl = stripUrlFragment(params.publicWebhookUrl);
|
|
166
|
+
if (signatureBaseUrl.includes("?")) return signatureBaseUrl;
|
|
167
|
+
const search = requestSearch(params.req);
|
|
168
|
+
if (!search) return signatureBaseUrl;
|
|
169
|
+
return `${signatureBaseUrl}${search}`;
|
|
170
|
+
}
|
|
171
|
+
var TwilioSmsApiError = class extends Error {
|
|
172
|
+
constructor(httpStatus, responseText, operation = "send") {
|
|
173
|
+
const parsed = parseTwilioApiError(responseText);
|
|
174
|
+
const detail = parsed.message ?? (responseText || "unknown");
|
|
175
|
+
super(`Twilio SMS ${operation} failed (${httpStatus}): ${detail}`);
|
|
176
|
+
this.name = "TwilioSmsApiError";
|
|
177
|
+
this.httpStatus = httpStatus;
|
|
178
|
+
this.responseText = responseText;
|
|
179
|
+
this.twilioCode = parsed.code;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
function parseTwilioFormBody(body) {
|
|
183
|
+
const parsed = querystring.parse(body);
|
|
184
|
+
const out = {};
|
|
185
|
+
for (const [key, value] of Object.entries(parsed)) out[key] = firstString(value);
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
function computeTwilioSignature(params) {
|
|
189
|
+
const data = params.url + Object.keys(params.form).toSorted().map((key) => `${key}${params.form[key] ?? ""}`).join("");
|
|
190
|
+
return createHmac("sha1", params.authToken).update(data).digest("base64");
|
|
191
|
+
}
|
|
192
|
+
function verifyTwilioSignature(params) {
|
|
193
|
+
if (!params.signature || !params.url || !params.authToken) return false;
|
|
194
|
+
return safeEqualSecret(params.signature, computeTwilioSignature({
|
|
195
|
+
url: params.url,
|
|
196
|
+
authToken: params.authToken,
|
|
197
|
+
form: params.form
|
|
198
|
+
}));
|
|
199
|
+
}
|
|
200
|
+
function parseTwilioInboundFrom(raw) {
|
|
201
|
+
const trimmed = raw.trim();
|
|
202
|
+
if (!trimmed) return null;
|
|
203
|
+
const channelAddress = trimmed.match(TWILIO_CHANNEL_ADDRESS_RE);
|
|
204
|
+
const kind = channelAddress?.[1]?.toLowerCase();
|
|
205
|
+
if (kind && kind !== "rcs") return null;
|
|
206
|
+
const phoneNumber = normalizeSmsPhoneNumber(channelAddress?.[2] ?? trimmed);
|
|
207
|
+
if (!looksLikeSmsPhoneNumber(phoneNumber)) return null;
|
|
208
|
+
return phoneNumber;
|
|
209
|
+
}
|
|
210
|
+
function resolveTwilioInboundSender(form) {
|
|
211
|
+
return parseTwilioInboundFrom(firstTrimmedString(form.From)) ?? "";
|
|
212
|
+
}
|
|
213
|
+
function buildTwilioInboundMessage(form) {
|
|
214
|
+
const from = resolveTwilioInboundSender(form);
|
|
215
|
+
const to = firstTrimmedString(form.To);
|
|
216
|
+
const body = firstString(form.Body);
|
|
217
|
+
const accountSid = firstString(form.AccountSid);
|
|
218
|
+
const messagingServiceSid = firstString(form.MessagingServiceSid);
|
|
219
|
+
const messageSid = resolveTwilioMessageSid(form);
|
|
220
|
+
const rawMediaCount = firstTrimmedString(form.NumMedia);
|
|
221
|
+
const mediaCount = rawMediaCount ? Number(rawMediaCount) : 0;
|
|
222
|
+
if (!Number.isSafeInteger(mediaCount) || mediaCount < 0) return null;
|
|
223
|
+
let unavailableMediaCount = Math.max(0, mediaCount - SMS_MAX_INBOUND_MEDIA_DOWNLOADS);
|
|
224
|
+
const media = Array.from({ length: Math.min(mediaCount, SMS_MAX_INBOUND_MEDIA_DOWNLOADS) }, (_value, index) => {
|
|
225
|
+
const url = firstTrimmedString(form[`MediaUrl${index}`]);
|
|
226
|
+
const contentType = firstTrimmedString(form[`MediaContentType${index}`]);
|
|
227
|
+
if (!url) {
|
|
228
|
+
unavailableMediaCount += 1;
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
url,
|
|
233
|
+
...contentType ? { contentType } : {}
|
|
234
|
+
};
|
|
235
|
+
}).filter((item) => item !== null);
|
|
236
|
+
if (!from || !to || !body && mediaCount === 0 || !messageSid) return null;
|
|
237
|
+
return {
|
|
238
|
+
accountSid,
|
|
239
|
+
from,
|
|
240
|
+
to,
|
|
241
|
+
body,
|
|
242
|
+
messageSid,
|
|
243
|
+
media,
|
|
244
|
+
...messagingServiceSid ? { messagingServiceSid } : {},
|
|
245
|
+
...unavailableMediaCount > 0 ? { unavailableMediaCount } : {}
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function resolveTwilioMessageSid(form) {
|
|
249
|
+
return firstTrimmedString(form.MessageSid) || firstTrimmedString(form.SmsSid) || firstTrimmedString(form.SmsMessageSid);
|
|
250
|
+
}
|
|
251
|
+
async function readTwilioWebhookForm(req) {
|
|
252
|
+
return parseTwilioFormBody(await readRequestBodyWithLimit(req, {
|
|
253
|
+
maxBytes: WEBHOOK_BODY_LIMIT_BYTES,
|
|
254
|
+
timeoutMs: WEBHOOK_BODY_TIMEOUT_MS
|
|
255
|
+
}));
|
|
256
|
+
}
|
|
257
|
+
function respondTwiml(res, statusCode, body = "") {
|
|
258
|
+
res.statusCode = statusCode;
|
|
259
|
+
res.setHeader("content-type", "text/xml; charset=utf-8");
|
|
260
|
+
res.end(body || "<Response></Response>");
|
|
261
|
+
}
|
|
262
|
+
function twilioApiUrl(accountSid, path, query) {
|
|
263
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
264
|
+
const url = new URL(`${TWILIO_ACCOUNTS_URL}/${encodeURIComponent(accountSid)}${normalizedPath}`);
|
|
265
|
+
if (query) url.search = query.toString();
|
|
266
|
+
return url.toString();
|
|
267
|
+
}
|
|
268
|
+
function twilioMessagingUrl(path, query) {
|
|
269
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
270
|
+
const url = new URL(`${TWILIO_MESSAGING_URL}${normalizedPath}`);
|
|
271
|
+
if (query) url.search = query.toString();
|
|
272
|
+
return url.toString();
|
|
273
|
+
}
|
|
274
|
+
function basicAuthHeader(account) {
|
|
275
|
+
return `Basic ${Buffer.from(`${account.accountSid}:${account.authToken}`).toString("base64")}`;
|
|
276
|
+
}
|
|
277
|
+
function appendTruncatedResponseSuffix(text) {
|
|
278
|
+
return `${text.trimEnd()}${TRUNCATED_RESPONSE_SUFFIX}`;
|
|
279
|
+
}
|
|
280
|
+
async function readTwilioApiResponseText(response) {
|
|
281
|
+
const maxBytes = response.ok ? TWILIO_API_SUCCESS_BODY_LIMIT_BYTES : TWILIO_API_ERROR_BODY_LIMIT_BYTES;
|
|
282
|
+
if (!response.ok) {
|
|
283
|
+
const prefix = await readResponseTextPrefix(response, maxBytes);
|
|
284
|
+
return prefix.truncated ? appendTruncatedResponseSuffix(prefix.text) : prefix.text;
|
|
285
|
+
}
|
|
286
|
+
const body = await readResponseWithLimit(response, maxBytes, { onOverflow: ({ size, maxBytes: limit }) => /* @__PURE__ */ new Error(`Twilio SMS API response body too large: ${size} bytes (limit: ${limit} bytes)`) });
|
|
287
|
+
return new TextDecoder().decode(body);
|
|
288
|
+
}
|
|
289
|
+
function normalizeRequestHeaders(headers) {
|
|
290
|
+
if (!headers) return {};
|
|
291
|
+
if (headers instanceof Headers) return Object.fromEntries(headers.entries());
|
|
292
|
+
if (Array.isArray(headers)) return Object.fromEntries(headers.map(([key, value]) => [key, value]));
|
|
293
|
+
return Object.fromEntries(Object.entries(headers));
|
|
294
|
+
}
|
|
295
|
+
async function requestTwilioApi(params) {
|
|
296
|
+
const init = {
|
|
297
|
+
...params.init,
|
|
298
|
+
headers: {
|
|
299
|
+
...normalizeRequestHeaders(params.init?.headers),
|
|
300
|
+
authorization: basicAuthHeader(params.account)
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
if (params.fetchImpl) {
|
|
304
|
+
const response = await params.fetchImpl(params.url, init);
|
|
305
|
+
return {
|
|
306
|
+
ok: response.ok,
|
|
307
|
+
status: response.status,
|
|
308
|
+
text: await readTwilioApiResponseText(response)
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
const guarded = await fetchWithSsrFGuard({
|
|
312
|
+
url: params.url,
|
|
313
|
+
init,
|
|
314
|
+
auditContext: "sms-twilio-api",
|
|
315
|
+
policy: { allowedHostnames: [params.allowedHostname] },
|
|
316
|
+
requireHttps: true,
|
|
317
|
+
timeoutMs: params.timeoutMs ?? TWILIO_API_TIMEOUT_MS
|
|
318
|
+
});
|
|
319
|
+
try {
|
|
320
|
+
return {
|
|
321
|
+
ok: guarded.response.ok,
|
|
322
|
+
status: guarded.response.status,
|
|
323
|
+
text: await readTwilioApiResponseText(guarded.response)
|
|
324
|
+
};
|
|
325
|
+
} finally {
|
|
326
|
+
await guarded.release();
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
function parseTwilioIncomingPhoneNumber(record) {
|
|
330
|
+
return {
|
|
331
|
+
sid: firstTrimmedString(record.sid),
|
|
332
|
+
phoneNumber: firstTrimmedString(record.phone_number ?? record.phoneNumber),
|
|
333
|
+
smsUrl: firstTrimmedString(record.sms_url ?? record.smsUrl),
|
|
334
|
+
smsMethod: firstTrimmedString(record.sms_method ?? record.smsMethod),
|
|
335
|
+
voiceUrl: firstTrimmedString(record.voice_url ?? record.voiceUrl)
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
function parseTwilioMessageLogEntry(record) {
|
|
339
|
+
return {
|
|
340
|
+
sid: firstTrimmedString(record.sid),
|
|
341
|
+
direction: firstTrimmedString(record.direction),
|
|
342
|
+
status: firstTrimmedString(record.status),
|
|
343
|
+
to: firstTrimmedString(record.to),
|
|
344
|
+
from: firstTrimmedString(record.from),
|
|
345
|
+
errorCode: firstStringish(record.error_code ?? record.errorCode).trim(),
|
|
346
|
+
body: firstString(record.body),
|
|
347
|
+
dateCreated: firstTrimmedString(record.date_created ?? record.dateCreated),
|
|
348
|
+
dateSent: firstTrimmedString(record.date_sent ?? record.dateSent)
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
function parseTwilioMessagingService(record) {
|
|
352
|
+
return {
|
|
353
|
+
sid: firstTrimmedString(record.sid),
|
|
354
|
+
inboundRequestUrl: firstTrimmedString(record.inbound_request_url ?? record.inboundRequestUrl),
|
|
355
|
+
inboundMethod: firstTrimmedString(record.inbound_method ?? record.inboundMethod),
|
|
356
|
+
useInboundWebhookOnNumber: Boolean(record.use_inbound_webhook_on_number ?? record.useInboundWebhookOnNumber)
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
function parseTwilioListPayload(text, key, parseEntry) {
|
|
360
|
+
if (!text.trim()) return [];
|
|
361
|
+
let parsed;
|
|
362
|
+
try {
|
|
363
|
+
parsed = JSON.parse(text);
|
|
364
|
+
} catch {
|
|
365
|
+
return [];
|
|
366
|
+
}
|
|
367
|
+
if (!parsed || typeof parsed !== "object") return [];
|
|
368
|
+
const items = parsed[key];
|
|
369
|
+
if (!Array.isArray(items)) return [];
|
|
370
|
+
return items.filter((item) => Boolean(item && typeof item === "object" && !Array.isArray(item))).map(parseEntry);
|
|
371
|
+
}
|
|
372
|
+
async function listTwilioIncomingPhoneNumbers(params) {
|
|
373
|
+
const query = new URLSearchParams();
|
|
374
|
+
if (params.phoneNumber) query.set("PhoneNumber", params.phoneNumber);
|
|
375
|
+
const response = await requestTwilioApi({
|
|
376
|
+
account: params.account,
|
|
377
|
+
url: twilioApiUrl(params.account.accountSid, "/IncomingPhoneNumbers.json", query),
|
|
378
|
+
allowedHostname: TWILIO_API_HOSTNAME,
|
|
379
|
+
fetchImpl: params.fetchImpl,
|
|
380
|
+
timeoutMs: params.timeoutMs
|
|
381
|
+
});
|
|
382
|
+
if (!response.ok) throw new TwilioSmsApiError(response.status, response.text, "phone-number lookup");
|
|
383
|
+
return parseTwilioListPayload(response.text, "incoming_phone_numbers", parseTwilioIncomingPhoneNumber);
|
|
384
|
+
}
|
|
385
|
+
async function retrieveTwilioMessagingService(params) {
|
|
386
|
+
const response = await requestTwilioApi({
|
|
387
|
+
account: params.account,
|
|
388
|
+
url: twilioMessagingUrl(`/Services/${encodeURIComponent(params.serviceSid)}`),
|
|
389
|
+
allowedHostname: TWILIO_MESSAGING_HOSTNAME,
|
|
390
|
+
fetchImpl: params.fetchImpl,
|
|
391
|
+
timeoutMs: params.timeoutMs
|
|
392
|
+
});
|
|
393
|
+
if (!response.ok) throw new TwilioSmsApiError(response.status, response.text, "messaging-service lookup");
|
|
394
|
+
let parsed;
|
|
395
|
+
try {
|
|
396
|
+
parsed = JSON.parse(response.text);
|
|
397
|
+
} catch {
|
|
398
|
+
throw new Error("Twilio Messaging Service lookup returned malformed JSON.");
|
|
399
|
+
}
|
|
400
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error("Twilio Messaging Service lookup returned malformed JSON.");
|
|
401
|
+
return parseTwilioMessagingService(parsed);
|
|
402
|
+
}
|
|
403
|
+
async function listTwilioMessages(params) {
|
|
404
|
+
const query = new URLSearchParams();
|
|
405
|
+
if (params.to) query.set("To", params.to);
|
|
406
|
+
if (params.from) query.set("From", params.from);
|
|
407
|
+
query.set("PageSize", String(params.pageSize ?? 5));
|
|
408
|
+
const response = await requestTwilioApi({
|
|
409
|
+
account: params.account,
|
|
410
|
+
url: twilioApiUrl(params.account.accountSid, "/Messages.json", query),
|
|
411
|
+
allowedHostname: TWILIO_API_HOSTNAME,
|
|
412
|
+
fetchImpl: params.fetchImpl,
|
|
413
|
+
timeoutMs: params.timeoutMs
|
|
414
|
+
});
|
|
415
|
+
if (!response.ok) throw new TwilioSmsApiError(response.status, response.text, "message lookup");
|
|
416
|
+
return parseTwilioListPayload(response.text, "messages", parseTwilioMessageLogEntry);
|
|
417
|
+
}
|
|
418
|
+
async function sendSmsViaTwilio(params) {
|
|
419
|
+
if (!params.account.fromNumber && !params.account.messagingServiceSid) throw new Error("Twilio SMS send requires fromNumber or messagingServiceSid.");
|
|
420
|
+
if (params.text && params.text.length > 1600) throw new Error(`Twilio SMS/MMS Body supports at most ${TWILIO_MESSAGE_BODY_MAX_LENGTH} characters.`);
|
|
421
|
+
const mediaUrls = (params.mediaUrls ?? []).map((url) => url.trim()).filter(Boolean);
|
|
422
|
+
if (!params.text && mediaUrls.length === 0) throw new Error("Twilio SMS/MMS send requires text or media.");
|
|
423
|
+
if (mediaUrls.length > TWILIO_MMS_MAX_OUTBOUND_MEDIA_COUNT) throw new Error(`Twilio MMS send supports at most ${TWILIO_MMS_MAX_OUTBOUND_MEDIA_COUNT} media URLs.`);
|
|
424
|
+
const body = new URLSearchParams({ To: params.to });
|
|
425
|
+
if (params.text) body.set("Body", params.text);
|
|
426
|
+
for (const mediaUrl of mediaUrls) body.append("MediaUrl", mediaUrl);
|
|
427
|
+
if (params.account.fromNumber) body.set("From", params.account.fromNumber);
|
|
428
|
+
else body.set("MessagingServiceSid", params.account.messagingServiceSid);
|
|
429
|
+
const statusCallback = resolveTwilioStatusCallbackUrl(params.account.publicWebhookUrl);
|
|
430
|
+
if (statusCallback) body.set("StatusCallback", statusCallback);
|
|
431
|
+
const init = {
|
|
432
|
+
method: "POST",
|
|
433
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
434
|
+
body
|
|
435
|
+
};
|
|
436
|
+
await params.onPlatformSendDispatch?.();
|
|
437
|
+
const response = await requestTwilioApi({
|
|
438
|
+
account: params.account,
|
|
439
|
+
url: twilioApiUrl(params.account.accountSid, "/Messages.json"),
|
|
440
|
+
allowedHostname: TWILIO_API_HOSTNAME,
|
|
441
|
+
init,
|
|
442
|
+
fetchImpl: params.fetchImpl
|
|
443
|
+
});
|
|
444
|
+
if (!response.ok) throw new TwilioSmsApiError(response.status, response.text);
|
|
445
|
+
const payload = parseTwilioSuccessPayload(response.text);
|
|
446
|
+
const sid = payload.sid?.trim();
|
|
447
|
+
if (!sid) throw new Error("Twilio SMS send response did not include a Message SID.");
|
|
448
|
+
return {
|
|
449
|
+
sid,
|
|
450
|
+
to: payload.to?.trim() || params.to,
|
|
451
|
+
...payload.from?.trim() ? { from: payload.from.trim() } : {},
|
|
452
|
+
...payload.status?.trim() ? { status: payload.status.trim() } : {}
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
//#endregion
|
|
456
|
+
export { normalizeSmsAllowFrom as _, listTwilioMessages as a, resolveTwilioMessageSid as c, retrieveTwilioMessagingService as d, sendSmsViaTwilio as f, looksLikeSmsPhoneNumber as g, resolveTwilioStatusCallbackUrl as h, listTwilioIncomingPhoneNumbers as i, resolveTwilioWebhookSignatureUrl as l, parseSmsPublicWebhookUrl as m, TWILIO_MMS_MAX_BYTES as n, readTwilioWebhookForm as o, verifyTwilioSignature as p, buildTwilioInboundMessage as r, resolveTwilioInboundSender as s, TWILIO_MESSAGE_BODY_MAX_LENGTH as t, respondTwiml as u, normalizeSmsPhoneNumber as v };
|
package/openclaw.plugin.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "sms",
|
|
3
3
|
"name": "SMS",
|
|
4
|
-
"description": "Twilio SMS channel plugin for OpenClaw
|
|
4
|
+
"description": "Twilio SMS/MMS channel plugin for OpenClaw messages.",
|
|
5
5
|
"activation": {
|
|
6
6
|
"onStartup": false
|
|
7
7
|
},
|
|
@@ -25,6 +25,9 @@
|
|
|
25
25
|
"enabled": {
|
|
26
26
|
"type": "boolean"
|
|
27
27
|
},
|
|
28
|
+
"configWrites": {
|
|
29
|
+
"type": "boolean"
|
|
30
|
+
},
|
|
28
31
|
"accountSid": {
|
|
29
32
|
"type": "string"
|
|
30
33
|
},
|
|
@@ -58,6 +61,29 @@
|
|
|
58
61
|
],
|
|
59
62
|
"additionalProperties": false
|
|
60
63
|
},
|
|
64
|
+
{
|
|
65
|
+
"type": "object",
|
|
66
|
+
"properties": {
|
|
67
|
+
"source": {
|
|
68
|
+
"type": "string",
|
|
69
|
+
"const": "store"
|
|
70
|
+
},
|
|
71
|
+
"provider": {
|
|
72
|
+
"type": "string",
|
|
73
|
+
"pattern": "^[a-z][a-z0-9_-]{0,63}$"
|
|
74
|
+
},
|
|
75
|
+
"id": {
|
|
76
|
+
"type": "string",
|
|
77
|
+
"pattern": "^[A-Z][A-Z0-9_]{0,127}$"
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
"required": [
|
|
81
|
+
"source",
|
|
82
|
+
"provider",
|
|
83
|
+
"id"
|
|
84
|
+
],
|
|
85
|
+
"additionalProperties": false
|
|
86
|
+
},
|
|
61
87
|
{
|
|
62
88
|
"type": "object",
|
|
63
89
|
"properties": {
|
|
@@ -166,6 +192,9 @@
|
|
|
166
192
|
"enabled": {
|
|
167
193
|
"type": "boolean"
|
|
168
194
|
},
|
|
195
|
+
"configWrites": {
|
|
196
|
+
"type": "boolean"
|
|
197
|
+
},
|
|
169
198
|
"accountSid": {
|
|
170
199
|
"type": "string"
|
|
171
200
|
},
|
|
@@ -199,6 +228,29 @@
|
|
|
199
228
|
],
|
|
200
229
|
"additionalProperties": false
|
|
201
230
|
},
|
|
231
|
+
{
|
|
232
|
+
"type": "object",
|
|
233
|
+
"properties": {
|
|
234
|
+
"source": {
|
|
235
|
+
"type": "string",
|
|
236
|
+
"const": "store"
|
|
237
|
+
},
|
|
238
|
+
"provider": {
|
|
239
|
+
"type": "string",
|
|
240
|
+
"pattern": "^[a-z][a-z0-9_-]{0,63}$"
|
|
241
|
+
},
|
|
242
|
+
"id": {
|
|
243
|
+
"type": "string",
|
|
244
|
+
"pattern": "^[A-Z][A-Z0-9_]{0,127}$"
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
"required": [
|
|
248
|
+
"source",
|
|
249
|
+
"provider",
|
|
250
|
+
"id"
|
|
251
|
+
],
|
|
252
|
+
"additionalProperties": false
|
|
253
|
+
},
|
|
202
254
|
{
|
|
203
255
|
"type": "object",
|
|
204
256
|
"properties": {
|
|
@@ -310,11 +362,11 @@
|
|
|
310
362
|
"additionalProperties": false
|
|
311
363
|
},
|
|
312
364
|
"label": "SMS",
|
|
313
|
-
"description": "Twilio-backed SMS with inbound webhooks and outbound replies.",
|
|
365
|
+
"description": "Twilio-backed SMS/MMS with inbound webhooks and outbound replies.",
|
|
314
366
|
"uiHints": {
|
|
315
367
|
"": {
|
|
316
368
|
"label": "SMS",
|
|
317
|
-
"help": "Twilio SMS channel configuration for inbound webhooks and outbound
|
|
369
|
+
"help": "Twilio SMS/MMS channel configuration for inbound webhooks and outbound replies."
|
|
318
370
|
},
|
|
319
371
|
"accountSid": {
|
|
320
372
|
"label": "Twilio Account SID",
|
|
@@ -326,7 +378,7 @@
|
|
|
326
378
|
},
|
|
327
379
|
"fromNumber": {
|
|
328
380
|
"label": "SMS From Number",
|
|
329
|
-
"help": "Twilio SMS-capable phone number in E.164 format
|
|
381
|
+
"help": "Twilio SMS-capable phone number in E.164 format; outbound attachments also require MMS capability.",
|
|
330
382
|
"presentation": "phone-number"
|
|
331
383
|
},
|
|
332
384
|
"messagingServiceSid": {
|
|
@@ -340,7 +392,7 @@
|
|
|
340
392
|
},
|
|
341
393
|
"publicWebhookUrl": {
|
|
342
394
|
"label": "SMS Public Webhook URL",
|
|
343
|
-
"help": "Public URL configured in Twilio for incoming messages. Must match Twilio's signed URL exactly."
|
|
395
|
+
"help": "Public URL configured in Twilio for incoming messages. Must match Twilio's signed URL exactly; outbound MMS also requires this same path to be reachable over HTTPS."
|
|
344
396
|
},
|
|
345
397
|
"webhookPath": {
|
|
346
398
|
"label": "SMS Webhook Path",
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openclaw/sms",
|
|
3
|
-
"version": "2026.
|
|
4
|
-
"description": "OpenClaw SMS channel plugin for Twilio
|
|
3
|
+
"version": "2026.8.1-beta.2",
|
|
4
|
+
"description": "OpenClaw SMS/MMS channel plugin for Twilio messages.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/openclaw/openclaw"
|
|
@@ -32,10 +32,10 @@
|
|
|
32
32
|
},
|
|
33
33
|
"label": "SMS",
|
|
34
34
|
"selectionLabel": "SMS (Twilio)",
|
|
35
|
-
"detailLabel": "Twilio SMS",
|
|
35
|
+
"detailLabel": "Twilio SMS/MMS",
|
|
36
36
|
"docsPath": "/channels/sms",
|
|
37
37
|
"docsLabel": "sms",
|
|
38
|
-
"blurb": "Twilio-backed SMS with inbound webhooks and outbound replies.",
|
|
38
|
+
"blurb": "Twilio-backed SMS/MMS with inbound webhooks and outbound replies.",
|
|
39
39
|
"order": 88,
|
|
40
40
|
"quickstartAllowFrom": true,
|
|
41
41
|
"setup": {
|
|
@@ -124,10 +124,10 @@
|
|
|
124
124
|
}
|
|
125
125
|
},
|
|
126
126
|
"compat": {
|
|
127
|
-
"pluginApi": ">=2026.
|
|
127
|
+
"pluginApi": ">=2026.8.1-beta.2"
|
|
128
128
|
},
|
|
129
129
|
"build": {
|
|
130
|
-
"openclawVersion": "2026.
|
|
130
|
+
"openclawVersion": "2026.8.1-beta.2",
|
|
131
131
|
"bundledDist": false
|
|
132
132
|
},
|
|
133
133
|
"install": {
|
|
@@ -151,7 +151,7 @@
|
|
|
151
151
|
"README.md"
|
|
152
152
|
],
|
|
153
153
|
"peerDependencies": {
|
|
154
|
-
"openclaw": ">=2026.
|
|
154
|
+
"openclaw": ">=2026.8.1-beta.2"
|
|
155
155
|
},
|
|
156
156
|
"peerDependenciesMeta": {
|
|
157
157
|
"openclaw": {
|