@openclaw/voice-call 2026.5.2 → 2026.5.3-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/dist/api.js +2 -0
- package/dist/call-status-CXldV5o8.js +32 -0
- package/dist/cli-metadata.js +12 -0
- package/dist/config-7w04YpHh.js +548 -0
- package/dist/config-compat-B0me39_4.js +129 -0
- package/dist/guarded-json-api-Btx5EE4w.js +591 -0
- package/dist/http-headers-BrnxBasF.js +10 -0
- package/dist/index.js +1284 -0
- package/dist/mock-CeKvfVEd.js +135 -0
- package/dist/plivo-B-a7KFoT.js +393 -0
- package/dist/realtime-handler-B63CIDP2.js +325 -0
- package/dist/realtime-transcription.runtime-B2h70y2W.js +2 -0
- package/dist/realtime-voice.runtime-Bkh4nvLn.js +2 -0
- package/dist/response-generator-BrcmwDZU.js +182 -0
- package/dist/response-model-CyF5K80p.js +12 -0
- package/dist/runtime-api.js +6 -0
- package/dist/runtime-entry-88ytYAQa.js +3119 -0
- package/dist/runtime-entry.js +2 -0
- package/dist/setup-api.js +37 -0
- package/dist/telnyx-jjBE8boz.js +260 -0
- package/dist/twilio-1OqbcXLL.js +676 -0
- package/dist/voice-mapping-BYDGdWGx.js +40 -0
- package/package.json +14 -6
- package/api.ts +0 -16
- package/cli-metadata.ts +0 -10
- package/config-api.ts +0 -12
- package/index.test.ts +0 -943
- package/index.ts +0 -794
- package/runtime-api.ts +0 -20
- package/runtime-entry.ts +0 -1
- package/setup-api.ts +0 -47
- package/src/allowlist.test.ts +0 -18
- package/src/allowlist.ts +0 -19
- package/src/cli.ts +0 -845
- package/src/config-compat.test.ts +0 -120
- package/src/config-compat.ts +0 -227
- package/src/config.test.ts +0 -479
- package/src/config.ts +0 -808
- package/src/core-bridge.ts +0 -14
- package/src/deep-merge.test.ts +0 -40
- package/src/deep-merge.ts +0 -23
- package/src/gateway-continue-operation.ts +0 -200
- package/src/http-headers.test.ts +0 -16
- package/src/http-headers.ts +0 -15
- package/src/manager/context.ts +0 -42
- package/src/manager/events.test.ts +0 -581
- package/src/manager/events.ts +0 -288
- package/src/manager/lifecycle.ts +0 -53
- package/src/manager/lookup.test.ts +0 -52
- package/src/manager/lookup.ts +0 -35
- package/src/manager/outbound.test.ts +0 -528
- package/src/manager/outbound.ts +0 -486
- package/src/manager/state.ts +0 -48
- package/src/manager/store.ts +0 -106
- package/src/manager/timers.test.ts +0 -129
- package/src/manager/timers.ts +0 -113
- package/src/manager/twiml.test.ts +0 -13
- package/src/manager/twiml.ts +0 -17
- package/src/manager.closed-loop.test.ts +0 -236
- package/src/manager.inbound-allowlist.test.ts +0 -188
- package/src/manager.notify.test.ts +0 -377
- package/src/manager.restore.test.ts +0 -183
- package/src/manager.test-harness.ts +0 -127
- package/src/manager.ts +0 -392
- package/src/media-stream.test.ts +0 -768
- package/src/media-stream.ts +0 -708
- package/src/providers/base.ts +0 -97
- package/src/providers/mock.test.ts +0 -78
- package/src/providers/mock.ts +0 -185
- package/src/providers/plivo.test.ts +0 -93
- package/src/providers/plivo.ts +0 -601
- package/src/providers/shared/call-status.test.ts +0 -24
- package/src/providers/shared/call-status.ts +0 -24
- package/src/providers/shared/guarded-json-api.test.ts +0 -106
- package/src/providers/shared/guarded-json-api.ts +0 -42
- package/src/providers/telnyx.test.ts +0 -340
- package/src/providers/telnyx.ts +0 -394
- package/src/providers/twilio/api.test.ts +0 -145
- package/src/providers/twilio/api.ts +0 -93
- package/src/providers/twilio/twiml-policy.test.ts +0 -84
- package/src/providers/twilio/twiml-policy.ts +0 -87
- package/src/providers/twilio/webhook.ts +0 -34
- package/src/providers/twilio.test.ts +0 -591
- package/src/providers/twilio.ts +0 -861
- package/src/providers/twilio.types.ts +0 -17
- package/src/realtime-defaults.ts +0 -3
- package/src/realtime-fast-context.test.ts +0 -88
- package/src/realtime-fast-context.ts +0 -165
- package/src/realtime-transcription.runtime.ts +0 -4
- package/src/realtime-voice.runtime.ts +0 -5
- package/src/response-generator.test.ts +0 -321
- package/src/response-generator.ts +0 -318
- package/src/response-model.test.ts +0 -71
- package/src/response-model.ts +0 -23
- package/src/runtime.test.ts +0 -536
- package/src/runtime.ts +0 -510
- package/src/telephony-audio.test.ts +0 -61
- package/src/telephony-audio.ts +0 -12
- package/src/telephony-tts.test.ts +0 -196
- package/src/telephony-tts.ts +0 -235
- package/src/test-fixtures.ts +0 -73
- package/src/tts-provider-voice.test.ts +0 -34
- package/src/tts-provider-voice.ts +0 -21
- package/src/tunnel.test.ts +0 -166
- package/src/tunnel.ts +0 -314
- package/src/types.ts +0 -291
- package/src/utils.test.ts +0 -17
- package/src/utils.ts +0 -14
- package/src/voice-mapping.test.ts +0 -34
- package/src/voice-mapping.ts +0 -68
- package/src/webhook/realtime-handler.test.ts +0 -598
- package/src/webhook/realtime-handler.ts +0 -485
- package/src/webhook/stale-call-reaper.test.ts +0 -88
- package/src/webhook/stale-call-reaper.ts +0 -38
- package/src/webhook/tailscale.test.ts +0 -214
- package/src/webhook/tailscale.ts +0 -129
- package/src/webhook-exposure.test.ts +0 -33
- package/src/webhook-exposure.ts +0 -84
- package/src/webhook-security.test.ts +0 -770
- package/src/webhook-security.ts +0 -994
- package/src/webhook.hangup-once.lifecycle.test.ts +0 -135
- package/src/webhook.test.ts +0 -1470
- package/src/webhook.ts +0 -908
- package/src/webhook.types.ts +0 -5
- package/src/websocket-test-support.ts +0 -72
- package/tsconfig.json +0 -16
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/text-runtime";
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
//#region extensions/voice-call/src/providers/mock.ts
|
|
4
|
+
/**
|
|
5
|
+
* Mock voice call provider for local testing.
|
|
6
|
+
*
|
|
7
|
+
* Events are driven via webhook POST with JSON body:
|
|
8
|
+
* - { events: NormalizedEvent[] } for bulk events
|
|
9
|
+
* - { event: NormalizedEvent } for single event
|
|
10
|
+
*/
|
|
11
|
+
var MockProvider = class {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.name = "mock";
|
|
14
|
+
}
|
|
15
|
+
verifyWebhook(_ctx) {
|
|
16
|
+
return { ok: true };
|
|
17
|
+
}
|
|
18
|
+
parseWebhookEvent(ctx, _options) {
|
|
19
|
+
try {
|
|
20
|
+
const payload = JSON.parse(ctx.rawBody);
|
|
21
|
+
const events = [];
|
|
22
|
+
if (Array.isArray(payload.events)) for (const evt of payload.events) {
|
|
23
|
+
const normalized = this.normalizeEvent(evt);
|
|
24
|
+
if (normalized) events.push(normalized);
|
|
25
|
+
}
|
|
26
|
+
else if (payload.event) {
|
|
27
|
+
const normalized = this.normalizeEvent(payload.event);
|
|
28
|
+
if (normalized) events.push(normalized);
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
events,
|
|
32
|
+
statusCode: 200
|
|
33
|
+
};
|
|
34
|
+
} catch {
|
|
35
|
+
return {
|
|
36
|
+
events: [],
|
|
37
|
+
statusCode: 400
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
normalizeEvent(evt) {
|
|
42
|
+
if (!evt.type || !evt.callId) return null;
|
|
43
|
+
const base = {
|
|
44
|
+
id: evt.id ?? crypto.randomUUID(),
|
|
45
|
+
callId: evt.callId,
|
|
46
|
+
providerCallId: evt.providerCallId,
|
|
47
|
+
timestamp: evt.timestamp ?? Date.now()
|
|
48
|
+
};
|
|
49
|
+
switch (evt.type) {
|
|
50
|
+
case "call.initiated":
|
|
51
|
+
case "call.ringing":
|
|
52
|
+
case "call.answered":
|
|
53
|
+
case "call.active": return {
|
|
54
|
+
...base,
|
|
55
|
+
type: evt.type
|
|
56
|
+
};
|
|
57
|
+
case "call.speaking": {
|
|
58
|
+
const payload = evt;
|
|
59
|
+
return {
|
|
60
|
+
...base,
|
|
61
|
+
type: evt.type,
|
|
62
|
+
text: payload.text ?? ""
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
case "call.speech": {
|
|
66
|
+
const payload = evt;
|
|
67
|
+
return {
|
|
68
|
+
...base,
|
|
69
|
+
type: evt.type,
|
|
70
|
+
transcript: payload.transcript ?? "",
|
|
71
|
+
isFinal: payload.isFinal ?? true,
|
|
72
|
+
confidence: payload.confidence
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
case "call.silence": {
|
|
76
|
+
const payload = evt;
|
|
77
|
+
return {
|
|
78
|
+
...base,
|
|
79
|
+
type: evt.type,
|
|
80
|
+
durationMs: payload.durationMs ?? 0
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
case "call.dtmf": {
|
|
84
|
+
const payload = evt;
|
|
85
|
+
return {
|
|
86
|
+
...base,
|
|
87
|
+
type: evt.type,
|
|
88
|
+
digits: payload.digits ?? ""
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
case "call.ended": {
|
|
92
|
+
const payload = evt;
|
|
93
|
+
return {
|
|
94
|
+
...base,
|
|
95
|
+
type: evt.type,
|
|
96
|
+
reason: payload.reason ?? "completed"
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
case "call.error": {
|
|
100
|
+
const payload = evt;
|
|
101
|
+
return {
|
|
102
|
+
...base,
|
|
103
|
+
type: evt.type,
|
|
104
|
+
error: payload.error ?? "unknown error",
|
|
105
|
+
retryable: payload.retryable
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
default: return null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async initiateCall(input) {
|
|
112
|
+
return {
|
|
113
|
+
providerCallId: `mock-${input.callId}`,
|
|
114
|
+
status: "initiated"
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
async hangupCall(_input) {}
|
|
118
|
+
async playTts(_input) {}
|
|
119
|
+
async sendDtmf(_input) {}
|
|
120
|
+
async startListening(_input) {}
|
|
121
|
+
async stopListening(_input) {}
|
|
122
|
+
async getCallStatus(input) {
|
|
123
|
+
const id = normalizeLowercaseStringOrEmpty(input.providerCallId);
|
|
124
|
+
if (id.includes("stale") || id.includes("ended") || id.includes("completed")) return {
|
|
125
|
+
status: "completed",
|
|
126
|
+
isTerminal: true
|
|
127
|
+
};
|
|
128
|
+
return {
|
|
129
|
+
status: "in-progress",
|
|
130
|
+
isTerminal: false
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
//#endregion
|
|
135
|
+
export { MockProvider };
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { t as escapeXml } from "./voice-mapping-BYDGdWGx.js";
|
|
2
|
+
import { t as getHeader } from "./http-headers-BrnxBasF.js";
|
|
3
|
+
import { n as reconstructWebhookUrl, r as verifyPlivoWebhook, t as guardedJsonApiRequest } from "./guarded-json-api-Btx5EE4w.js";
|
|
4
|
+
import { normalizeLowercaseStringOrEmpty, normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
|
|
5
|
+
import crypto from "node:crypto";
|
|
6
|
+
//#region extensions/voice-call/src/providers/plivo.ts
|
|
7
|
+
function createPlivoRequestDedupeKey(ctx) {
|
|
8
|
+
const nonceV3 = getHeader(ctx.headers, "x-plivo-signature-v3-nonce");
|
|
9
|
+
if (nonceV3) return `plivo:v3:${nonceV3}`;
|
|
10
|
+
const nonceV2 = getHeader(ctx.headers, "x-plivo-signature-v2-nonce");
|
|
11
|
+
if (nonceV2) return `plivo:v2:${nonceV2}`;
|
|
12
|
+
return `plivo:fallback:${crypto.createHash("sha256").update(ctx.rawBody).digest("hex")}`;
|
|
13
|
+
}
|
|
14
|
+
var PlivoProvider = class PlivoProvider {
|
|
15
|
+
constructor(config, options = {}) {
|
|
16
|
+
this.name = "plivo";
|
|
17
|
+
this.requestUuidToCallUuid = /* @__PURE__ */ new Map();
|
|
18
|
+
this.callIdToWebhookUrl = /* @__PURE__ */ new Map();
|
|
19
|
+
this.callUuidToWebhookUrl = /* @__PURE__ */ new Map();
|
|
20
|
+
this.pendingSpeakByCallId = /* @__PURE__ */ new Map();
|
|
21
|
+
this.pendingListenByCallId = /* @__PURE__ */ new Map();
|
|
22
|
+
if (!config.authId) throw new Error("Plivo Auth ID is required");
|
|
23
|
+
if (!config.authToken) throw new Error("Plivo Auth Token is required");
|
|
24
|
+
this.authId = config.authId;
|
|
25
|
+
this.authToken = config.authToken;
|
|
26
|
+
this.baseUrl = `https://api.plivo.com/v1/Account/${this.authId}`;
|
|
27
|
+
this.apiHost = new URL(this.baseUrl).hostname;
|
|
28
|
+
this.options = options;
|
|
29
|
+
}
|
|
30
|
+
async apiRequest(params) {
|
|
31
|
+
const { method, endpoint, body, allowNotFound } = params;
|
|
32
|
+
return await guardedJsonApiRequest({
|
|
33
|
+
url: `${this.baseUrl}${endpoint}`,
|
|
34
|
+
method,
|
|
35
|
+
headers: {
|
|
36
|
+
Authorization: `Basic ${Buffer.from(`${this.authId}:${this.authToken}`).toString("base64")}`,
|
|
37
|
+
"Content-Type": "application/json"
|
|
38
|
+
},
|
|
39
|
+
body,
|
|
40
|
+
allowNotFound,
|
|
41
|
+
allowedHostnames: [this.apiHost],
|
|
42
|
+
auditContext: "voice-call.plivo.api",
|
|
43
|
+
errorPrefix: "Plivo API error"
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
verifyWebhook(ctx) {
|
|
47
|
+
const result = verifyPlivoWebhook(ctx, this.authToken, {
|
|
48
|
+
publicUrl: this.options.publicUrl,
|
|
49
|
+
skipVerification: this.options.skipVerification,
|
|
50
|
+
allowedHosts: this.options.webhookSecurity?.allowedHosts,
|
|
51
|
+
trustForwardingHeaders: this.options.webhookSecurity?.trustForwardingHeaders,
|
|
52
|
+
trustedProxyIPs: this.options.webhookSecurity?.trustedProxyIPs,
|
|
53
|
+
remoteIP: ctx.remoteAddress
|
|
54
|
+
});
|
|
55
|
+
if (!result.ok) console.warn(`[plivo] Webhook verification failed: ${result.reason}`);
|
|
56
|
+
return {
|
|
57
|
+
ok: result.ok,
|
|
58
|
+
reason: result.reason,
|
|
59
|
+
isReplay: result.isReplay,
|
|
60
|
+
verifiedRequestKey: result.verifiedRequestKey
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
parseWebhookEvent(ctx, options) {
|
|
64
|
+
const flow = normalizeOptionalString(ctx.query?.flow) ?? "";
|
|
65
|
+
const parsed = this.parseBody(ctx.rawBody);
|
|
66
|
+
if (!parsed) return {
|
|
67
|
+
events: [],
|
|
68
|
+
statusCode: 400
|
|
69
|
+
};
|
|
70
|
+
const callUuid = parsed.get("CallUUID") || void 0;
|
|
71
|
+
if (callUuid) {
|
|
72
|
+
const webhookBase = this.baseWebhookUrlFromCtx(ctx);
|
|
73
|
+
if (webhookBase) this.callUuidToWebhookUrl.set(callUuid, webhookBase);
|
|
74
|
+
}
|
|
75
|
+
if (flow === "xml-speak") {
|
|
76
|
+
const callId = this.getCallIdFromQuery(ctx);
|
|
77
|
+
const pending = callId ? this.pendingSpeakByCallId.get(callId) : void 0;
|
|
78
|
+
if (callId) this.pendingSpeakByCallId.delete(callId);
|
|
79
|
+
return {
|
|
80
|
+
events: [],
|
|
81
|
+
providerResponseBody: pending ? PlivoProvider.xmlSpeak(pending.text, pending.locale) : PlivoProvider.xmlKeepAlive(),
|
|
82
|
+
providerResponseHeaders: { "Content-Type": "text/xml" },
|
|
83
|
+
statusCode: 200
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (flow === "xml-listen") {
|
|
87
|
+
const callId = this.getCallIdFromQuery(ctx);
|
|
88
|
+
const pending = callId ? this.pendingListenByCallId.get(callId) : void 0;
|
|
89
|
+
if (callId) this.pendingListenByCallId.delete(callId);
|
|
90
|
+
const actionUrl = this.buildActionUrl(ctx, {
|
|
91
|
+
flow: "getinput",
|
|
92
|
+
callId
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
events: [],
|
|
96
|
+
providerResponseBody: actionUrl && callId ? PlivoProvider.xmlGetInputSpeech({
|
|
97
|
+
actionUrl,
|
|
98
|
+
language: pending?.language
|
|
99
|
+
}) : PlivoProvider.xmlKeepAlive(),
|
|
100
|
+
providerResponseHeaders: { "Content-Type": "text/xml" },
|
|
101
|
+
statusCode: 200
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const callIdFromQuery = this.getCallIdFromQuery(ctx);
|
|
105
|
+
const dedupeKey = options?.verifiedRequestKey ?? createPlivoRequestDedupeKey(ctx);
|
|
106
|
+
const event = this.normalizeEvent(parsed, callIdFromQuery, dedupeKey);
|
|
107
|
+
return {
|
|
108
|
+
events: event ? [event] : [],
|
|
109
|
+
providerResponseBody: flow === "answer" || flow === "getinput" ? PlivoProvider.xmlKeepAlive() : PlivoProvider.xmlEmpty(),
|
|
110
|
+
providerResponseHeaders: { "Content-Type": "text/xml" },
|
|
111
|
+
statusCode: 200
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
normalizeEvent(params, callIdOverride, dedupeKey) {
|
|
115
|
+
const callUuid = params.get("CallUUID") || "";
|
|
116
|
+
const requestUuid = params.get("RequestUUID") || "";
|
|
117
|
+
if (requestUuid && callUuid) this.requestUuidToCallUuid.set(requestUuid, callUuid);
|
|
118
|
+
const direction = params.get("Direction");
|
|
119
|
+
const from = params.get("From") || void 0;
|
|
120
|
+
const to = params.get("To") || void 0;
|
|
121
|
+
const callStatus = params.get("CallStatus");
|
|
122
|
+
const baseEvent = {
|
|
123
|
+
id: crypto.randomUUID(),
|
|
124
|
+
dedupeKey,
|
|
125
|
+
callId: callIdOverride || callUuid || requestUuid,
|
|
126
|
+
providerCallId: callUuid || requestUuid || void 0,
|
|
127
|
+
timestamp: Date.now(),
|
|
128
|
+
direction: direction === "inbound" ? "inbound" : direction === "outbound" ? "outbound" : void 0,
|
|
129
|
+
from,
|
|
130
|
+
to
|
|
131
|
+
};
|
|
132
|
+
const digits = params.get("Digits");
|
|
133
|
+
if (digits) return {
|
|
134
|
+
...baseEvent,
|
|
135
|
+
type: "call.dtmf",
|
|
136
|
+
digits
|
|
137
|
+
};
|
|
138
|
+
const transcript = PlivoProvider.extractTranscript(params);
|
|
139
|
+
if (transcript) return {
|
|
140
|
+
...baseEvent,
|
|
141
|
+
type: "call.speech",
|
|
142
|
+
transcript,
|
|
143
|
+
isFinal: true
|
|
144
|
+
};
|
|
145
|
+
if (callStatus === "ringing") return {
|
|
146
|
+
...baseEvent,
|
|
147
|
+
type: "call.ringing"
|
|
148
|
+
};
|
|
149
|
+
if (callStatus === "in-progress") return {
|
|
150
|
+
...baseEvent,
|
|
151
|
+
type: "call.answered"
|
|
152
|
+
};
|
|
153
|
+
if (callStatus === "completed" || callStatus === "busy" || callStatus === "no-answer" || callStatus === "failed") return {
|
|
154
|
+
...baseEvent,
|
|
155
|
+
type: "call.ended",
|
|
156
|
+
reason: callStatus === "completed" ? "completed" : callStatus === "busy" ? "busy" : callStatus === "no-answer" ? "no-answer" : "failed"
|
|
157
|
+
};
|
|
158
|
+
if (params.get("Event") === "StartApp" && callUuid) return {
|
|
159
|
+
...baseEvent,
|
|
160
|
+
type: "call.answered"
|
|
161
|
+
};
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
async initiateCall(input) {
|
|
165
|
+
const webhookUrl = new URL(input.webhookUrl);
|
|
166
|
+
webhookUrl.searchParams.set("provider", "plivo");
|
|
167
|
+
webhookUrl.searchParams.set("callId", input.callId);
|
|
168
|
+
const answerUrl = new URL(webhookUrl);
|
|
169
|
+
answerUrl.searchParams.set("flow", "answer");
|
|
170
|
+
const hangupUrl = new URL(webhookUrl);
|
|
171
|
+
hangupUrl.searchParams.set("flow", "hangup");
|
|
172
|
+
this.callIdToWebhookUrl.set(input.callId, input.webhookUrl);
|
|
173
|
+
const ringTimeoutSec = this.options.ringTimeoutSec ?? 30;
|
|
174
|
+
const result = await this.apiRequest({
|
|
175
|
+
method: "POST",
|
|
176
|
+
endpoint: "/Call/",
|
|
177
|
+
body: {
|
|
178
|
+
from: PlivoProvider.normalizeNumber(input.from),
|
|
179
|
+
to: PlivoProvider.normalizeNumber(input.to),
|
|
180
|
+
answer_url: answerUrl.toString(),
|
|
181
|
+
answer_method: "POST",
|
|
182
|
+
hangup_url: hangupUrl.toString(),
|
|
183
|
+
hangup_method: "POST",
|
|
184
|
+
hangup_on_ring: ringTimeoutSec
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
const requestUuid = Array.isArray(result.request_uuid) ? result.request_uuid[0] : result.request_uuid;
|
|
188
|
+
if (!requestUuid) throw new Error("Plivo call create returned no request_uuid");
|
|
189
|
+
return {
|
|
190
|
+
providerCallId: requestUuid,
|
|
191
|
+
status: "initiated"
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
async hangupCall(input) {
|
|
195
|
+
const callUuid = this.requestUuidToCallUuid.get(input.providerCallId);
|
|
196
|
+
if (callUuid) {
|
|
197
|
+
await this.apiRequest({
|
|
198
|
+
method: "DELETE",
|
|
199
|
+
endpoint: `/Call/${callUuid}/`,
|
|
200
|
+
allowNotFound: true
|
|
201
|
+
});
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
await this.apiRequest({
|
|
205
|
+
method: "DELETE",
|
|
206
|
+
endpoint: `/Call/${input.providerCallId}/`,
|
|
207
|
+
allowNotFound: true
|
|
208
|
+
});
|
|
209
|
+
await this.apiRequest({
|
|
210
|
+
method: "DELETE",
|
|
211
|
+
endpoint: `/Request/${input.providerCallId}/`,
|
|
212
|
+
allowNotFound: true
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
resolveCallContext(params) {
|
|
216
|
+
const callUuid = this.requestUuidToCallUuid.get(params.providerCallId) ?? params.providerCallId;
|
|
217
|
+
const webhookBase = this.callUuidToWebhookUrl.get(callUuid) || this.callIdToWebhookUrl.get(params.callId);
|
|
218
|
+
if (!webhookBase) throw new Error("Missing webhook URL for this call (provider state missing)");
|
|
219
|
+
if (!callUuid) throw new Error(`Missing Plivo CallUUID for ${params.operation}`);
|
|
220
|
+
return {
|
|
221
|
+
callUuid,
|
|
222
|
+
webhookBase
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
async transferCallLeg(params) {
|
|
226
|
+
const transferUrl = new URL(params.webhookBase);
|
|
227
|
+
transferUrl.searchParams.set("provider", "plivo");
|
|
228
|
+
transferUrl.searchParams.set("flow", params.flow);
|
|
229
|
+
transferUrl.searchParams.set("callId", params.callId);
|
|
230
|
+
await this.apiRequest({
|
|
231
|
+
method: "POST",
|
|
232
|
+
endpoint: `/Call/${params.callUuid}/`,
|
|
233
|
+
body: {
|
|
234
|
+
legs: "aleg",
|
|
235
|
+
aleg_url: transferUrl.toString(),
|
|
236
|
+
aleg_method: "POST"
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
async playTts(input) {
|
|
241
|
+
const { callUuid, webhookBase } = this.resolveCallContext({
|
|
242
|
+
providerCallId: input.providerCallId,
|
|
243
|
+
callId: input.callId,
|
|
244
|
+
operation: "playTts"
|
|
245
|
+
});
|
|
246
|
+
this.pendingSpeakByCallId.set(input.callId, {
|
|
247
|
+
text: input.text,
|
|
248
|
+
locale: input.locale
|
|
249
|
+
});
|
|
250
|
+
await this.transferCallLeg({
|
|
251
|
+
callUuid,
|
|
252
|
+
webhookBase,
|
|
253
|
+
callId: input.callId,
|
|
254
|
+
flow: "xml-speak"
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
async startListening(input) {
|
|
258
|
+
const { callUuid, webhookBase } = this.resolveCallContext({
|
|
259
|
+
providerCallId: input.providerCallId,
|
|
260
|
+
callId: input.callId,
|
|
261
|
+
operation: "startListening"
|
|
262
|
+
});
|
|
263
|
+
this.pendingListenByCallId.set(input.callId, { language: input.language });
|
|
264
|
+
await this.transferCallLeg({
|
|
265
|
+
callUuid,
|
|
266
|
+
webhookBase,
|
|
267
|
+
callId: input.callId,
|
|
268
|
+
flow: "xml-listen"
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
async stopListening(_input) {}
|
|
272
|
+
async getCallStatus(input) {
|
|
273
|
+
const terminalStatuses = new Set([
|
|
274
|
+
"completed",
|
|
275
|
+
"busy",
|
|
276
|
+
"failed",
|
|
277
|
+
"timeout",
|
|
278
|
+
"no-answer",
|
|
279
|
+
"cancel",
|
|
280
|
+
"machine",
|
|
281
|
+
"hangup"
|
|
282
|
+
]);
|
|
283
|
+
try {
|
|
284
|
+
const data = await guardedJsonApiRequest({
|
|
285
|
+
url: `${this.baseUrl}/Call/${input.providerCallId}/`,
|
|
286
|
+
method: "GET",
|
|
287
|
+
headers: { Authorization: `Basic ${Buffer.from(`${this.authId}:${this.authToken}`).toString("base64")}` },
|
|
288
|
+
allowNotFound: true,
|
|
289
|
+
allowedHostnames: [this.apiHost],
|
|
290
|
+
auditContext: "plivo-get-call-status",
|
|
291
|
+
errorPrefix: "Plivo get call status error"
|
|
292
|
+
});
|
|
293
|
+
if (!data) return {
|
|
294
|
+
status: "not-found",
|
|
295
|
+
isTerminal: true
|
|
296
|
+
};
|
|
297
|
+
const status = data.call_status ?? "unknown";
|
|
298
|
+
return {
|
|
299
|
+
status,
|
|
300
|
+
isTerminal: terminalStatuses.has(status)
|
|
301
|
+
};
|
|
302
|
+
} catch {
|
|
303
|
+
return {
|
|
304
|
+
status: "error",
|
|
305
|
+
isTerminal: false,
|
|
306
|
+
isUnknown: true
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
static normalizeNumber(numberOrSip) {
|
|
311
|
+
const trimmed = numberOrSip.trim();
|
|
312
|
+
if (normalizeLowercaseStringOrEmpty(trimmed).startsWith("sip:")) return trimmed;
|
|
313
|
+
return trimmed.replace(/[^\d+]/g, "");
|
|
314
|
+
}
|
|
315
|
+
static xmlEmpty() {
|
|
316
|
+
return `<?xml version="1.0" encoding="UTF-8"?><Response></Response>`;
|
|
317
|
+
}
|
|
318
|
+
static xmlKeepAlive() {
|
|
319
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
320
|
+
<Response>
|
|
321
|
+
<Wait length="300" />
|
|
322
|
+
</Response>`;
|
|
323
|
+
}
|
|
324
|
+
static xmlSpeak(text, locale) {
|
|
325
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
326
|
+
<Response>
|
|
327
|
+
<Speak language="${escapeXml(locale || "en-US")}">${escapeXml(text)}</Speak>
|
|
328
|
+
<Wait length="300" />
|
|
329
|
+
</Response>`;
|
|
330
|
+
}
|
|
331
|
+
static xmlGetInputSpeech(params) {
|
|
332
|
+
const language = params.language || "en-US";
|
|
333
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
334
|
+
<Response>
|
|
335
|
+
<GetInput inputType="speech" method="POST" action="${escapeXml(params.actionUrl)}" language="${escapeXml(language)}" executionTimeout="30" speechEndTimeout="1" redirect="false">
|
|
336
|
+
</GetInput>
|
|
337
|
+
<Wait length="300" />
|
|
338
|
+
</Response>`;
|
|
339
|
+
}
|
|
340
|
+
getCallIdFromQuery(ctx) {
|
|
341
|
+
return normalizeOptionalString(ctx.query?.callId) || void 0;
|
|
342
|
+
}
|
|
343
|
+
buildActionUrl(ctx, opts) {
|
|
344
|
+
const base = this.baseWebhookUrlFromCtx(ctx);
|
|
345
|
+
if (!base) return null;
|
|
346
|
+
const u = new URL(base);
|
|
347
|
+
u.searchParams.set("provider", "plivo");
|
|
348
|
+
u.searchParams.set("flow", opts.flow);
|
|
349
|
+
if (opts.callId) u.searchParams.set("callId", opts.callId);
|
|
350
|
+
return u.toString();
|
|
351
|
+
}
|
|
352
|
+
baseWebhookUrlFromCtx(ctx) {
|
|
353
|
+
try {
|
|
354
|
+
if (this.options.publicUrl) {
|
|
355
|
+
const base = new URL(this.options.publicUrl);
|
|
356
|
+
base.pathname = new URL(ctx.url).pathname;
|
|
357
|
+
return `${base.origin}${base.pathname}`;
|
|
358
|
+
}
|
|
359
|
+
const u = new URL(reconstructWebhookUrl(ctx, {
|
|
360
|
+
allowedHosts: this.options.webhookSecurity?.allowedHosts,
|
|
361
|
+
trustForwardingHeaders: this.options.webhookSecurity?.trustForwardingHeaders,
|
|
362
|
+
trustedProxyIPs: this.options.webhookSecurity?.trustedProxyIPs,
|
|
363
|
+
remoteIP: ctx.remoteAddress
|
|
364
|
+
}));
|
|
365
|
+
return `${u.origin}${u.pathname}`;
|
|
366
|
+
} catch {
|
|
367
|
+
return null;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
parseBody(rawBody) {
|
|
371
|
+
try {
|
|
372
|
+
return new URLSearchParams(rawBody);
|
|
373
|
+
} catch {
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
static extractTranscript(params) {
|
|
378
|
+
for (const key of [
|
|
379
|
+
"Speech",
|
|
380
|
+
"Transcription",
|
|
381
|
+
"TranscriptionText",
|
|
382
|
+
"SpeechResult",
|
|
383
|
+
"RecognizedSpeech",
|
|
384
|
+
"Text"
|
|
385
|
+
]) {
|
|
386
|
+
const value = params.get(key);
|
|
387
|
+
if (value && value.trim()) return value.trim();
|
|
388
|
+
}
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
//#endregion
|
|
393
|
+
export { PlivoProvider };
|