Vonage / vonage-node-sdk

Vonage API client for Node.js. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.
Apache License 2.0
375 stars 178 forks source link

How Do I: transfer call #915

Closed AI-General closed 3 months ago

AI-General commented 4 months ago

How do I

transfer call

API/Product

Voice

Code Sample

Now I am in call with real number 1.

I need to transfer number 1 to number 2.

I used this code:

    transfer() {
        vonage.voice.transferCallWithNCCO(this.call_id, [
            {
                action: 'talk',
                text: 'Hello, you are being connected, please wait...'
            },
            {
                "action": "connect",
                "endpoint": [{
                    "type": "phone",
                    "number": BOSS_NUMBER,
                    "dtmfAnswer": "auto"
                }]
            }], (err, res) => {
                if (err) {
                    console.error(err);
                } else {
                    console.log(res);
                }
            });
    };

But I met following error:

            throw new vetch_1.VetchError(`Request failed with status code ${response.status}`, request, response);
                  ^

VetchError: Request failed with status code 400
    at Voice.parseResponse (/root/VoiceBot/vonage/node_modules/@vonage/server-client/dist/client.js:302:19)
    at Voice.sendRequest (/root/VoiceBot/vonage/node_modules/@vonage/server-client/dist/client.js:221:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Voice.sendRequestWithData (/root/VoiceBot/vonage/node_modules/@vonage/server-client/dist/client.js:196:16)
    at async Voice.callAction (/root/VoiceBot/vonage/node_modules/@vonage/voice/dist/voice.js:476:9) {
  config: {
    url: 'https://api.nexmo.com/v1/calls/60ce05e1-7c69-4281-b2f4-37e0586eeb39',
    method: 'PUT',
    type: 'application/json',
    data: {
      action: 'transfer',
      destination: {
        type: 'ncco',
        ncco: [
          {
            action: 'talk',
            text: 'Hello, you are being connected, please wait...'
          },
          { action: 'connect', endpoint: [Array] }
        ]
      }
    },
    headers: {
      'user-agent': '@vonage/server-sdk/3.0.0, node/18.17.1,',
      'content-type': 'application/json',
      Authorization: 'Bearer <My token>'
    }
  },
  response: Response {
    size: 0,
    timeout: 0,
    [Symbol(Body internals)]: {
      body: PassThrough {
        _readableState: ReadableState {
          objectMode: false,
          highWaterMark: 16384,
          buffer: BufferList { head: [Object], tail: [Object], length: 1 },
          length: 146,
          pipes: [],
          flowing: null,
          ended: true,
          endEmitted: false,
          reading: false,
          constructed: true,
          sync: false,
          needReadable: false,
          emittedReadable: false,
          readableListening: false,
          resumeScheduled: false,
          errorEmitted: false,
          emitClose: true,
          autoDestroy: true,
          destroyed: false,
          errored: null,
          closed: false,
          closeEmitted: false,
          defaultEncoding: 'utf8',
          awaitDrainWriters: null,
          multiAwaitDrain: false,
          readingMore: false,
          dataEmitted: false,
          decoder: null,
          encoding: null,
          [Symbol(kPaused)]: null
        },
        _events: [Object: null prototype] {
          prefinish: [Function: prefinish],
          error: [Function (anonymous)]
        },
        _eventsCount: 2,
        _maxListeners: undefined,
        _writableState: WritableState {
          objectMode: false,
          highWaterMark: 16384,
          finalCalled: true,
          needDrain: false,
          ending: true,
          ended: true,
          finished: true,
          destroyed: false,
          decodeStrings: true,
          defaultEncoding: 'utf8',
          length: 0,
          writing: false,
          corked: 0,
          sync: false,
          bufferProcessing: false,
          onwrite: [Function: bound onwrite],
          writecb: null,
          writelen: 0,
          afterWriteTickInfo: null,
          buffered: [],
          bufferedIndex: 0,
          allBuffers: true,
          allNoop: true,
          pendingcb: 0,
          constructed: true,
          prefinished: true,
          errorEmitted: false,
          emitClose: true,
          autoDestroy: true,
          errored: null,
          closed: false,
          closeEmitted: false,
          [Symbol(kOnFinished)]: []
        },
        allowHalfOpen: true,
        [Symbol(kCapture)]: false,
        [Symbol(kCallback)]: null
      },
      disturbed: false,
      error: null
    },
    [Symbol(Response internals)]: {
      url: 'https://api.nexmo.com/v1/calls/60ce05e1-7c69-4281-b2f4-37e0586eeb39',
      status: 400,
      statusText: 'Bad Request',
      headers: Headers {
        [Symbol(map)]: [Object: null prototype] {
          date: [ 'Thu, 08 Feb 2024 20:11:53 GMT' ],
          'content-type': [ 'application/json' ],
          'content-length': [ '146' ],
          connection: [ 'close' ],
          server: [ 'envoy' ],
          'x-nexmo-trace-id': [ '7624fee7-a0f4-4dd6-babf-05136976e6fd' ],
          'x-vonage-region': [ 'us-3' ],
          'x-envoy-upstream-service-time': [ '63' ],
          'content-disposition': [ 'attachment; filename="api.txt"' ],
          'x-frame-options': [ 'deny' ],
          'x-xss-protection': [ '1; mode=block' ],
          'strict-transport-security': [ 'max-age=31536000; includeSubdomains' ],
          'x-content-type-options': [ 'nosniff' ],
          'x-ratelimit-limit': [
            '200, 200;w=1;name="crd|account_id|generic_key^nexmo-vapi.vapi|generic_key^solo.setDescriptor.uniqueValue"'
          ],
          'x-ratelimit-remaining': [ '199' ],
          'x-ratelimit-reset': [ '1' ]
        }
      },
      counter: 0
    }
  }
}

How to fix this?

Please help me.

Thank you

AI-General commented 4 months ago

Should I use 2 vonage phone for this?

Actually, if I use 2 vonage phone numbers, is it possible, I just need to make 2 websocket tunnel and transfer messages. But I want to do that without vonage phone.

Thank you

manchuck commented 4 months ago

@AI-General, I am checking your code sample. It still uses callback, which is how it was done in version 2 of the SDK. The error is showing a package that is in version 3. I don't know which version of the SDK you are using.

In any case, in Version 3, the transferCallWithNCCO method can transfer the call. Here is a link to our documentation which has a code sample for you: https://developer.vonage.com/en/voice/voice-api/code-snippets/controlling-conversations/transfer-a-call-inline-ncco?source=voice&lang=javascript