From 7d4d1cc1531ef5208d4db917a5458b5d12b760c9 Mon Sep 17 00:00:00 2001 From: Edoardo Gallo Date: Thu, 21 Apr 2022 12:19:56 +0200 Subject: [PATCH 1/7] initial proposal for voice createDialer --- .../src/voice/index.ts | 16 +-- packages/core/src/types/voiceCall.ts | 37 +++--- packages/realtime-api/src/voice/Call.ts | 9 +- packages/realtime-api/src/voice/Voice.ts | 42 +++++-- .../src/voice/VoiceClient.test.ts | 107 ++++++++++++++++++ 5 files changed, 176 insertions(+), 35 deletions(-) create mode 100644 packages/realtime-api/src/voice/VoiceClient.test.ts diff --git a/internal/playground-realtime-api/src/voice/index.ts b/internal/playground-realtime-api/src/voice/index.ts index eee9c5f53..88a1c79d2 100644 --- a/internal/playground-realtime-api/src/voice/index.ts +++ b/internal/playground-realtime-api/src/voice/index.ts @@ -30,18 +30,12 @@ async function run() { }) try { - const call = await client.dial({ - devices: [ - [ - { - type: 'phone', - to: process.env.TO_NUMBER as string, - from: process.env.FROM_NUMBER as string, - timeout: 30, - }, - ], - ], + const dialer = client.createDialer().dialPhone({ + to: process.env.TO_NUMBER as string, + from: process.env.FROM_NUMBER as string, + timeout: 30, }) + const call = await client.dial(dialer) console.log('Dial resolved!', call.id) const sleep = (ms = 3000) => { diff --git a/packages/core/src/types/voiceCall.ts b/packages/core/src/types/voiceCall.ts index 1b6a19a4c..7b2db0bb9 100644 --- a/packages/core/src/types/voiceCall.ts +++ b/packages/core/src/types/voiceCall.ts @@ -299,6 +299,17 @@ export type VoiceCallDisconnectReason = | 'decline' | 'error' +export interface CreateVoiceDialerParams { + region?: string +} + +export interface VoiceDialer extends CreateVoiceDialerParams { + devices: VoiceCallDialMethodParams['devices'] + dialPhone(params: Omit): this + dialSip(params: Omit): this + inParallel(dialer: VoiceDialer): this +} + /** * Public Contract for a VoiceCall */ @@ -451,7 +462,7 @@ export interface VoiceCallContract { direction: 'inbound' | 'outbound' headers?: SipHeader[] - dial(params?: VoiceCallDialMethodParams): Promise + dial(params: VoiceDialer): Promise hangup(reason?: VoiceCallDisconnectReason): Promise answer(): Promise play(params: VoiceCallPlayMethodParams): Promise @@ -758,18 +769,18 @@ export interface CallingCallConnectEvent extends SwEvent { * 'calling.call.send_digits */ - export type CallingCallSendDigitsState = 'finished' - export interface CallingCallSendDigitsEventParams { - node_id: string - call_id: string - control_id: string - state: CallingCallSendDigitsState - } - - export interface CallingCallSendDigitsEvent extends SwEvent { - event_type: ToInternalVoiceEvent - params: CallingCallSendDigitsEventParams - } +export type CallingCallSendDigitsState = 'finished' +export interface CallingCallSendDigitsEventParams { + node_id: string + call_id: string + control_id: string + state: CallingCallSendDigitsState +} + +export interface CallingCallSendDigitsEvent extends SwEvent { + event_type: ToInternalVoiceEvent + params: CallingCallSendDigitsEventParams +} /** * ========== diff --git a/packages/realtime-api/src/voice/Call.ts b/packages/realtime-api/src/voice/Call.ts index 12aaf7227..03c323ba7 100644 --- a/packages/realtime-api/src/voice/Call.ts +++ b/packages/realtime-api/src/voice/Call.ts @@ -7,7 +7,7 @@ import { extendComponent, VoiceCallMethods, VoiceCallContract, - VoiceCallDialMethodParams, + VoiceDialer, VoiceCallDisconnectReason, VoiceCallPlayMethodParams, VoiceCallPlayAudioMethodParams, @@ -299,7 +299,7 @@ export class CallConsumer extends AutoApplyTransformsConsumer { this.runWorker('voiceCallDialWorker', { worker: voiceCallDialWorker, @@ -307,12 +307,13 @@ export class CallConsumer extends AutoApplyTransformsConsumer { reject(e) diff --git a/packages/realtime-api/src/voice/Voice.ts b/packages/realtime-api/src/voice/Voice.ts index 19760f051..c4bb57e55 100644 --- a/packages/realtime-api/src/voice/Voice.ts +++ b/packages/realtime-api/src/voice/Voice.ts @@ -1,13 +1,10 @@ -import { - connect, - BaseComponentOptions, - toExternalJSON, - VoiceCallDialMethodParams, -} from '@signalwire/core' +import { connect, BaseComponentOptions, toExternalJSON } from '@signalwire/core' import type { EmitterContract, EventTransform, CallingCallReceiveEventParams, + CreateVoiceDialerParams, + VoiceDialer, } from '@signalwire/core' import { RealtimeClient } from '../client/index' import { createCallObject, Call } from './Call' @@ -27,7 +24,8 @@ type EmitterTransformsEvents = 'calling.call.received' export interface Voice extends EmitterContract { /** @internal */ _session: RealtimeClient - dial(params: VoiceCallDialMethodParams): Promise + dial(dialer: VoiceDialer): Promise + createDialer(params?: CreateVoiceDialerParams): VoiceDialer } /** @internal */ @@ -70,6 +68,36 @@ class VoiceAPI extends AutoApplyTransformsConsumer { ], ]) } + + createDialer(params: CreateVoiceDialerParams = {}): VoiceDialer { + const devices: VoiceDialer['devices'] = [] + + const dialer: VoiceDialer = { + ...params, + devices, + dialPhone(params) { + devices.push([{ type: 'phone', ...params }]) + return dialer + }, + dialSip(params) { + devices.push([{ type: 'sip', ...params }]) + return dialer + }, + inParallel(dialer) { + const parallel = dialer.devices.map((row) => { + if (Array.isArray(row)) { + return row[0] + } + return row + }) + devices.push(parallel) + + return dialer + }, + } + + return dialer + } } /** @internal */ diff --git a/packages/realtime-api/src/voice/VoiceClient.test.ts b/packages/realtime-api/src/voice/VoiceClient.test.ts new file mode 100644 index 000000000..62b382844 --- /dev/null +++ b/packages/realtime-api/src/voice/VoiceClient.test.ts @@ -0,0 +1,107 @@ +import { Client } from './VoiceClient' + +describe('VideoClient', () => { + describe('Client', () => { + const token = '' + + describe('createDialer', () => { + it('should build a list of devices to dial', () => { + const voice = new Client({ + project: 'some-project', + token, + contexts: ['test'], + }) + + const dialer = voice.createDialer() + + dialer + .dialPhone({ from: '+1', to: '+2', timeout: 30 }) + .dialSip({ + from: 'sip:one', + to: 'sip:two', + headers: [{ name: 'foo', value: 'bar' }], + }) + .inParallel( + voice + .createDialer() + .dialPhone({ from: '+3', to: '+4' }) + .dialSip({ + from: 'sip:three', + to: 'sip:four', + headers: [{ name: 'baz', value: 'qux' }], + }) + .dialPhone({ from: '+5', to: '+6' }) + ) + + expect(dialer.devices).toStrictEqual([ + [ + { + type: 'phone', + from: '+1', + to: '+2', + timeout: 30, + }, + ], + [ + { + type: 'sip', + from: 'sip:one', + to: 'sip:two', + headers: [{ name: 'foo', value: 'bar' }], + }, + ], + [ + { + type: 'phone', + from: '+3', + to: '+4', + }, + { + type: 'sip', + from: 'sip:three', + to: 'sip:four', + headers: [{ name: 'baz', value: 'qux' }], + }, + { + type: 'phone', + from: '+5', + to: '+6', + }, + ], + ]) + }) + + it('should build a list of devices to dial including region', () => { + const voice = new Client({ + project: 'some-project', + token, + contexts: ['test'], + }) + + const dialer = voice.createDialer({ region: 'us' }) + dialer.inParallel( + voice + .createDialer() + .dialPhone({ from: '+3', to: '+4' }) + .dialPhone({ from: '+5', to: '+6' }) + ) + + expect(dialer.region).toBe('us') + expect(dialer.devices).toStrictEqual([ + [ + { + type: 'phone', + from: '+3', + to: '+4', + }, + { + type: 'phone', + from: '+5', + to: '+6', + }, + ], + ]) + }) + }) + }) +}) From 5652d7163af4c68575b3b70fb1019aacca76b89b Mon Sep 17 00:00:00 2001 From: Edoardo Gallo Date: Fri, 22 Apr 2022 13:34:49 +0200 Subject: [PATCH 2/7] move createDialer from VoiceClient to an util - moved tests --- .../src/voice/VoiceClient.test.ts | 107 ------------------ packages/realtime-api/src/voice/utils.test.ts | 88 +++++++++++++- packages/realtime-api/src/voice/utils.ts | 32 ++++++ 3 files changed, 119 insertions(+), 108 deletions(-) delete mode 100644 packages/realtime-api/src/voice/VoiceClient.test.ts diff --git a/packages/realtime-api/src/voice/VoiceClient.test.ts b/packages/realtime-api/src/voice/VoiceClient.test.ts deleted file mode 100644 index 62b382844..000000000 --- a/packages/realtime-api/src/voice/VoiceClient.test.ts +++ /dev/null @@ -1,107 +0,0 @@ -import { Client } from './VoiceClient' - -describe('VideoClient', () => { - describe('Client', () => { - const token = '' - - describe('createDialer', () => { - it('should build a list of devices to dial', () => { - const voice = new Client({ - project: 'some-project', - token, - contexts: ['test'], - }) - - const dialer = voice.createDialer() - - dialer - .dialPhone({ from: '+1', to: '+2', timeout: 30 }) - .dialSip({ - from: 'sip:one', - to: 'sip:two', - headers: [{ name: 'foo', value: 'bar' }], - }) - .inParallel( - voice - .createDialer() - .dialPhone({ from: '+3', to: '+4' }) - .dialSip({ - from: 'sip:three', - to: 'sip:four', - headers: [{ name: 'baz', value: 'qux' }], - }) - .dialPhone({ from: '+5', to: '+6' }) - ) - - expect(dialer.devices).toStrictEqual([ - [ - { - type: 'phone', - from: '+1', - to: '+2', - timeout: 30, - }, - ], - [ - { - type: 'sip', - from: 'sip:one', - to: 'sip:two', - headers: [{ name: 'foo', value: 'bar' }], - }, - ], - [ - { - type: 'phone', - from: '+3', - to: '+4', - }, - { - type: 'sip', - from: 'sip:three', - to: 'sip:four', - headers: [{ name: 'baz', value: 'qux' }], - }, - { - type: 'phone', - from: '+5', - to: '+6', - }, - ], - ]) - }) - - it('should build a list of devices to dial including region', () => { - const voice = new Client({ - project: 'some-project', - token, - contexts: ['test'], - }) - - const dialer = voice.createDialer({ region: 'us' }) - dialer.inParallel( - voice - .createDialer() - .dialPhone({ from: '+3', to: '+4' }) - .dialPhone({ from: '+5', to: '+6' }) - ) - - expect(dialer.region).toBe('us') - expect(dialer.devices).toStrictEqual([ - [ - { - type: 'phone', - from: '+3', - to: '+4', - }, - { - type: 'phone', - from: '+5', - to: '+6', - }, - ], - ]) - }) - }) - }) -}) diff --git a/packages/realtime-api/src/voice/utils.test.ts b/packages/realtime-api/src/voice/utils.test.ts index 424aa8d41..b634fc110 100644 --- a/packages/realtime-api/src/voice/utils.test.ts +++ b/packages/realtime-api/src/voice/utils.test.ts @@ -1,4 +1,4 @@ -import { toInternalDevices } from './utils' +import { toInternalDevices, createDialer } from './utils' describe('toInternalDevices', () => { it('should convert the user facing interface to the internal one', () => { @@ -127,3 +127,89 @@ describe('toInternalDevices', () => { ]) }) }) + +describe('createDialer', () => { + it('should build a list of devices to dial', () => { + const dialer = createDialer() + + dialer + .dialPhone({ from: '+1', to: '+2', timeout: 30 }) + .dialSip({ + from: 'sip:one', + to: 'sip:two', + headers: [{ name: 'foo', value: 'bar' }], + }) + .inParallel( + createDialer() + .dialPhone({ from: '+3', to: '+4' }) + .dialSip({ + from: 'sip:three', + to: 'sip:four', + headers: [{ name: 'baz', value: 'qux' }], + }) + .dialPhone({ from: '+5', to: '+6' }) + ) + + expect(dialer.devices).toStrictEqual([ + [ + { + type: 'phone', + from: '+1', + to: '+2', + timeout: 30, + }, + ], + [ + { + type: 'sip', + from: 'sip:one', + to: 'sip:two', + headers: [{ name: 'foo', value: 'bar' }], + }, + ], + [ + { + type: 'phone', + from: '+3', + to: '+4', + }, + { + type: 'sip', + from: 'sip:three', + to: 'sip:four', + headers: [{ name: 'baz', value: 'qux' }], + }, + { + type: 'phone', + from: '+5', + to: '+6', + }, + ], + ]) + }) + + it('should build a list of devices to dial including region', () => { + const dialer = createDialer({ region: 'us' }) + dialer.inParallel( + createDialer() + .dialPhone({ from: '+3', to: '+4' }) + .dialPhone({ from: '+5', to: '+6' }) + ) + + expect(dialer.region).toBe('us') + expect(dialer.devices).toStrictEqual([ + [ + { + type: 'phone', + from: '+3', + to: '+4', + }, + { + type: 'phone', + from: '+5', + to: '+6', + }, + ], + ]) + }) +}) diff --git a/packages/realtime-api/src/voice/utils.ts b/packages/realtime-api/src/voice/utils.ts index 8eff224b0..c89ad8d7b 100644 --- a/packages/realtime-api/src/voice/utils.ts +++ b/packages/realtime-api/src/voice/utils.ts @@ -3,6 +3,8 @@ import { VoiceCallDialMethodParams, VoiceCallPlayParams, VoiceCallPlayMethodParams, + CreateVoiceDialerParams, + VoiceDialer, toSnakeCaseKeys, } from '@signalwire/core' @@ -73,3 +75,33 @@ export const toInternalPlayParams = ( }) return result } + +export const createDialer = (params: CreateVoiceDialerParams = {}) => { + const devices: VoiceDialer['devices'] = [] + + const dialer: VoiceDialer = { + ...params, + devices, + dialPhone(params) { + devices.push([{ type: 'phone', ...params }]) + return dialer + }, + dialSip(params) { + devices.push([{ type: 'sip', ...params }]) + return dialer + }, + inParallel(dialer) { + const parallel = dialer.devices.map((row) => { + if (Array.isArray(row)) { + return row[0] + } + return row + }) + devices.push(parallel) + + return dialer + }, + } + + return dialer +} From 7cbc3bd2492605017d5a5dc380d510c890e9c03e Mon Sep 17 00:00:00 2001 From: Edoardo Gallo Date: Fri, 22 Apr 2022 13:35:07 +0200 Subject: [PATCH 3/7] remove from client and update playground --- .../src/voice/index.ts | 2 +- packages/realtime-api/src/voice/Voice.ts | 33 +------------------ 2 files changed, 2 insertions(+), 33 deletions(-) diff --git a/internal/playground-realtime-api/src/voice/index.ts b/internal/playground-realtime-api/src/voice/index.ts index 88a1c79d2..3eed81830 100644 --- a/internal/playground-realtime-api/src/voice/index.ts +++ b/internal/playground-realtime-api/src/voice/index.ts @@ -30,7 +30,7 @@ async function run() { }) try { - const dialer = client.createDialer().dialPhone({ + const dialer = Voice.createDialer().dialPhone({ to: process.env.TO_NUMBER as string, from: process.env.FROM_NUMBER as string, timeout: 30, diff --git a/packages/realtime-api/src/voice/Voice.ts b/packages/realtime-api/src/voice/Voice.ts index c4bb57e55..6cad72837 100644 --- a/packages/realtime-api/src/voice/Voice.ts +++ b/packages/realtime-api/src/voice/Voice.ts @@ -3,7 +3,6 @@ import type { EmitterContract, EventTransform, CallingCallReceiveEventParams, - CreateVoiceDialerParams, VoiceDialer, } from '@signalwire/core' import { RealtimeClient } from '../client/index' @@ -13,6 +12,7 @@ import type { RealTimeCallApiEvents } from '../types' import { AutoApplyTransformsConsumer } from '../AutoApplyTransformsConsumer' export * from './VoiceClient' +export { createDialer } from './utils' /** * List of events for {@link Voice.Call}. @@ -25,7 +25,6 @@ export interface Voice extends EmitterContract { /** @internal */ _session: RealtimeClient dial(dialer: VoiceDialer): Promise - createDialer(params?: CreateVoiceDialerParams): VoiceDialer } /** @internal */ @@ -68,36 +67,6 @@ class VoiceAPI extends AutoApplyTransformsConsumer { ], ]) } - - createDialer(params: CreateVoiceDialerParams = {}): VoiceDialer { - const devices: VoiceDialer['devices'] = [] - - const dialer: VoiceDialer = { - ...params, - devices, - dialPhone(params) { - devices.push([{ type: 'phone', ...params }]) - return dialer - }, - dialSip(params) { - devices.push([{ type: 'sip', ...params }]) - return dialer - }, - inParallel(dialer) { - const parallel = dialer.devices.map((row) => { - if (Array.isArray(row)) { - return row[0] - } - return row - }) - devices.push(parallel) - - return dialer - }, - } - - return dialer - } } /** @internal */ From 6f721e649eab71389c86fe40319fd404a966d2b5 Mon Sep 17 00:00:00 2001 From: Edoardo Gallo Date: Fri, 22 Apr 2022 13:53:55 +0200 Subject: [PATCH 4/7] change dialX with addX --- .../playground-realtime-api/src/voice/index.ts | 2 +- packages/core/src/types/voiceCall.ts | 4 ++-- packages/realtime-api/src/voice/utils.test.ts | 14 +++++++------- packages/realtime-api/src/voice/utils.ts | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/internal/playground-realtime-api/src/voice/index.ts b/internal/playground-realtime-api/src/voice/index.ts index 3eed81830..4c2f9e0a9 100644 --- a/internal/playground-realtime-api/src/voice/index.ts +++ b/internal/playground-realtime-api/src/voice/index.ts @@ -30,7 +30,7 @@ async function run() { }) try { - const dialer = Voice.createDialer().dialPhone({ + const dialer = Voice.createDialer().addPhone({ to: process.env.TO_NUMBER as string, from: process.env.FROM_NUMBER as string, timeout: 30, diff --git a/packages/core/src/types/voiceCall.ts b/packages/core/src/types/voiceCall.ts index 7b2db0bb9..3295baa66 100644 --- a/packages/core/src/types/voiceCall.ts +++ b/packages/core/src/types/voiceCall.ts @@ -305,8 +305,8 @@ export interface CreateVoiceDialerParams { export interface VoiceDialer extends CreateVoiceDialerParams { devices: VoiceCallDialMethodParams['devices'] - dialPhone(params: Omit): this - dialSip(params: Omit): this + addPhone(params: Omit): this + addSip(params: Omit): this inParallel(dialer: VoiceDialer): this } diff --git a/packages/realtime-api/src/voice/utils.test.ts b/packages/realtime-api/src/voice/utils.test.ts index b634fc110..dac99f743 100644 --- a/packages/realtime-api/src/voice/utils.test.ts +++ b/packages/realtime-api/src/voice/utils.test.ts @@ -133,21 +133,21 @@ describe('createDialer', () => { const dialer = createDialer() dialer - .dialPhone({ from: '+1', to: '+2', timeout: 30 }) - .dialSip({ + .addPhone({ from: '+1', to: '+2', timeout: 30 }) + .addSip({ from: 'sip:one', to: 'sip:two', headers: [{ name: 'foo', value: 'bar' }], }) .inParallel( createDialer() - .dialPhone({ from: '+3', to: '+4' }) - .dialSip({ + .addPhone({ from: '+3', to: '+4' }) + .addSip({ from: 'sip:three', to: 'sip:four', headers: [{ name: 'baz', value: 'qux' }], }) - .dialPhone({ from: '+5', to: '+6' }) + .addPhone({ from: '+5', to: '+6' }) ) expect(dialer.devices).toStrictEqual([ @@ -192,8 +192,8 @@ describe('createDialer', () => { const dialer = createDialer({ region: 'us' }) dialer.inParallel( createDialer() - .dialPhone({ from: '+3', to: '+4' }) - .dialPhone({ from: '+5', to: '+6' }) + .addPhone({ from: '+3', to: '+4' }) + .addPhone({ from: '+5', to: '+6' }) ) expect(dialer.region).toBe('us') diff --git a/packages/realtime-api/src/voice/utils.ts b/packages/realtime-api/src/voice/utils.ts index c89ad8d7b..959388cda 100644 --- a/packages/realtime-api/src/voice/utils.ts +++ b/packages/realtime-api/src/voice/utils.ts @@ -82,11 +82,11 @@ export const createDialer = (params: CreateVoiceDialerParams = {}) => { const dialer: VoiceDialer = { ...params, devices, - dialPhone(params) { + addPhone(params) { devices.push([{ type: 'phone', ...params }]) return dialer }, - dialSip(params) { + addSip(params) { devices.push([{ type: 'sip', ...params }]) return dialer }, From 88b3b5fa338d2b47706fa209203018e81e9faf94 Mon Sep 17 00:00:00 2001 From: Edoardo Gallo Date: Fri, 22 Apr 2022 13:55:06 +0200 Subject: [PATCH 5/7] changeset --- .changeset/forty-moons-dance.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/forty-moons-dance.md diff --git a/.changeset/forty-moons-dance.md b/.changeset/forty-moons-dance.md new file mode 100644 index 000000000..b726620a8 --- /dev/null +++ b/.changeset/forty-moons-dance.md @@ -0,0 +1,7 @@ +--- +'@sw-internal/playground-realtime-api': minor +'@signalwire/core': minor +'@signalwire/realtime-api': minor +--- + +Expose the `Voice.createDialer()` method to simplify dialing devices on a Voice Call. From 23bcc187b4bea802da545082b1064a2f35f073f7 Mon Sep 17 00:00:00 2001 From: Edoardo Gallo Date: Fri, 22 Apr 2022 14:08:16 +0200 Subject: [PATCH 6/7] add method to dialPhone and dialSip directly --- packages/core/src/types/voiceCall.ts | 22 +++++++++++-------- packages/realtime-api/src/voice/Voice.ts | 21 +++++++++++++++++- .../realtime-api/src/voice/VoiceClient.ts | 4 ++-- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/packages/core/src/types/voiceCall.ts b/packages/core/src/types/voiceCall.ts index 3295baa66..4ad3f17fd 100644 --- a/packages/core/src/types/voiceCall.ts +++ b/packages/core/src/types/voiceCall.ts @@ -132,6 +132,8 @@ export interface VoiceCallPhoneParams { timeout?: number } +export type OmitType = Omit + export interface VoiceCallSipParams { type: 'sip' from: string @@ -186,19 +188,19 @@ export interface VoiceCallPlayMethodParams { } export interface VoiceCallPlayAudioMethodParams - extends Omit { + extends OmitType { volume?: number } export interface VoiceCallPlaySilenceMethodParams - extends Omit {} + extends OmitType {} export interface VoiceCallPlayRingtoneMethodParams - extends Omit { + extends OmitType { volume?: number } export interface VoiceCallPlayTTSMethodParams - extends Omit { + extends OmitType { volume?: number } @@ -239,19 +241,19 @@ export type VoiceCallPromptMethodParams = SpeechOrDigits & { partialResults?: boolean } export type VoiceCallPromptAudioMethodParams = SpeechOrDigits & - Omit & { + OmitType & { volume?: number initialTimeout?: number partialResults?: boolean } export type VoiceCallPromptRingtoneMethodParams = SpeechOrDigits & - Omit & { + OmitType & { volume?: number initialTimeout?: number partialResults?: boolean } export type VoiceCallPromptTTSMethodParams = SpeechOrDigits & - Omit & { + OmitType & { volume?: number initialTimeout?: number partialResults?: boolean @@ -299,14 +301,16 @@ export type VoiceCallDisconnectReason = | 'decline' | 'error' +export type VoiceCallDialPhoneMethodParams = OmitType +export type VoiceCallDialSipMethodParams = OmitType export interface CreateVoiceDialerParams { region?: string } export interface VoiceDialer extends CreateVoiceDialerParams { devices: VoiceCallDialMethodParams['devices'] - addPhone(params: Omit): this - addSip(params: Omit): this + addPhone(params: VoiceCallDialPhoneMethodParams): this + addSip(params: VoiceCallDialSipMethodParams): this inParallel(dialer: VoiceDialer): this } diff --git a/packages/realtime-api/src/voice/Voice.ts b/packages/realtime-api/src/voice/Voice.ts index 6cad72837..4e21aa65e 100644 --- a/packages/realtime-api/src/voice/Voice.ts +++ b/packages/realtime-api/src/voice/Voice.ts @@ -4,15 +4,18 @@ import type { EventTransform, CallingCallReceiveEventParams, VoiceDialer, + VoiceCallDialPhoneMethodParams, + VoiceCallDialSipMethodParams, } from '@signalwire/core' import { RealtimeClient } from '../client/index' import { createCallObject, Call } from './Call' import { voiceCallReceiveWorker } from './workers' +import { createDialer } from './utils' import type { RealTimeCallApiEvents } from '../types' import { AutoApplyTransformsConsumer } from '../AutoApplyTransformsConsumer' export * from './VoiceClient' -export { createDialer } from './utils' +export { createDialer } /** * List of events for {@link Voice.Call}. @@ -25,6 +28,8 @@ export interface Voice extends EmitterContract { /** @internal */ _session: RealtimeClient dial(dialer: VoiceDialer): Promise + dialPhone(params: VoiceCallDialPhoneMethodParams): Promise + dialSip(params: VoiceCallDialSipMethodParams): Promise } /** @internal */ @@ -67,6 +72,20 @@ class VoiceAPI extends AutoApplyTransformsConsumer { ], ]) } + + dialPhone(params: VoiceCallDialPhoneMethodParams) { + const dialer = createDialer().addPhone(params) + // dial is available through the VoiceClient Proxy + // @ts-expect-error + return this.dial(dialer) + } + + dialSip(params: VoiceCallDialSipMethodParams) { + const dialer = createDialer().addSip(params) + // dial is available through the VoiceClient Proxy + // @ts-expect-error + return this.dial(dialer) + } } /** @internal */ diff --git a/packages/realtime-api/src/voice/VoiceClient.ts b/packages/realtime-api/src/voice/VoiceClient.ts index 82c7154c7..52666f651 100644 --- a/packages/realtime-api/src/voice/VoiceClient.ts +++ b/packages/realtime-api/src/voice/VoiceClient.ts @@ -38,7 +38,7 @@ const VoiceClient = function (options?: VoiceClientOptions) { return voice.once(...args) } - const callDial: Call['dial'] = async (...args) => { + const callDial: Call['dial'] = async (dialer) => { await clientConnect(client) const call = createCallObject({ @@ -46,7 +46,7 @@ const VoiceClient = function (options?: VoiceClientOptions) { emitter, }) - await call.dial(...args) + await call.dial(dialer) return call } From 3c14bd854167ac87ddd06c160fc149339001f814 Mon Sep 17 00:00:00 2001 From: Edoardo Gallo Date: Fri, 22 Apr 2022 14:09:33 +0200 Subject: [PATCH 7/7] update playground with examples --- internal/playground-realtime-api/src/voice/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/playground-realtime-api/src/voice/index.ts b/internal/playground-realtime-api/src/voice/index.ts index 4c2f9e0a9..2ee76e198 100644 --- a/internal/playground-realtime-api/src/voice/index.ts +++ b/internal/playground-realtime-api/src/voice/index.ts @@ -30,12 +30,20 @@ async function run() { }) try { - const dialer = Voice.createDialer().addPhone({ + // Using createDialer util + // const dialer = Voice.createDialer().addPhone({ + // to: process.env.TO_NUMBER as string, + // from: process.env.FROM_NUMBER as string, + // timeout: 30, + // }) + // const call = await client.dial(dialer) + + // Using dialPhone Alias + const call = await client.dialPhone({ to: process.env.TO_NUMBER as string, from: process.env.FROM_NUMBER as string, timeout: 30, }) - const call = await client.dial(dialer) console.log('Dial resolved!', call.id) const sleep = (ms = 3000) => {