team-telnyx / telnyx-node

Node SDK for the Telnyx API
https://developers.telnyx.com/docs/api/v2/overview
MIT License
54 stars 20 forks source link

Leftover state on subsequent resource instance method calls #26

Closed lucasassisrosa closed 4 years ago

lucasassisrosa commented 4 years ago

Description:

when we initialize the SDK and create a new resource through the constructor, we see the new object created with the id updated and the first flow works fine. Subsequent uses of the same constructor do not. The state is not being cleared and the action is being taken against the previous object.

This can be exemplified with a webhook flow (see examples folder):

  /**
   * Inbound Call Control:
   * first we listen for an initiation event and then answer the call
   */
  if (event.data.event_type === 'call.initiated') {
    console.log('Call Initiated. Answering call with call control id: ' + event.data.payload.call_control_id);

    const call = new telnyx.Call({call_control_id: event.data.payload.call_control_id});
    call.answer();
  }

The SDK is outputting the following error:

{
   "errorType": "Error",
   "errorMessage": "(Status 422) (Request uuid) Call has already ended",
   "type": "TelnyxInvalidParametersError",
   "stack": [
       "Error: (Status 422) (Request uuid) Call has already ended",
       "    at Constructor._Error (/var/task/node_modules/telnyx/lib/Error.js:12:17)",
       "    at Constructor (/var/task/node_modules/telnyx/lib/utils.js:129:13)",
       "    at new Constructor (/var/task/node_modules/telnyx/lib/utils.js:129:13)",
       "    at Constructor._buildError (/var/task/node_modules/telnyx/lib/TelnyxResource.js:214:13)",
       "    at IncomingMessage.<anonymous> (/var/task/node_modules/telnyx/lib/TelnyxResource.js:161:28)",
       "    at IncomingMessage.emit (events.js:215:7)",
       "    at IncomingMessage.EventEmitter.emit (domain.js:476:20)",
       "    at endReadableNT (_stream_readable.js:1183:12)",
       "    at /var/task/node_modules/async-listener/glue.js:188:31",
       "    at processTicksAndRejections (internal/process/task_queues.js:80:21)"
   ],
   "message": "(Status 422) (Request uuid) Call has already ended",
   "raw": {
       "errors": [
           {
               "code": "90018",
               "detail": "This call is no longer active and can't receive commands.",
               "title": "Call has already ended"
           }
       ],
       "headers": {
           "access-control-allow-credentials": "true",
           "access-control-allow-origin": "*",
           "access-control-expose-headers": "Total,Total-Pages",
           "cache-control": "max-age=0, private, must-revalidate",
           "content-length": "173",
           "content-type": "application/json; charset=utf-8",
           "date": "Mon, 10 Feb 2020 19:40:18 GMT",
           "server": "Telnyx API",
           "x-envoy-upstream-service-time": "44"
       },
       "statusCode": 422,
       "requestId": "uuid"
   },
   "headers": {
       "access-control-allow-credentials": "true",
       "access-control-allow-origin": "*",
       "access-control-expose-headers": "Total,Total-Pages",
       "cache-control": "max-age=0, private, must-revalidate",
       "content-length": "173",
       "content-type": "application/json; charset=utf-8",
       "date": "Mon, 10 Feb 2020 19:40:18 GMT",
       "server": "Telnyx API",
       "x-request-id": "uuid",
       "x-envoy-upstream-service-time": "44"
   },
   "requestId": "uuid",
   "statusCode": 422
}

Target Version: 1.7.0 Resource: Calls

lucasassisrosa commented 4 years ago

Reopening this due to:

The wrong call control ID is being used after creating a new call and bridging the call.

SDK v1.10.0

I'm placing calls using the CC methods in the node SDK. the CC flow is as follows:

initiated (A) -> answer (A) -> dial (B) -> answer (B) -> bridge (A +B)

The first time around, the call completes. The second time around, I get "Call has already ended." When I check the request ID in graylog, it is the wrong call control ID.

Steps to Reproduce

This should work in theory:

  1. Set up a node CC app. Set up the initiated handler to answer a call if parked (A leg).
  2. [A leg] In the answered handler, create a new call with telnyx.calls.create to a transfer destination.
  3. [B leg] In the answered handler, bridge the call to B leg.
  4. Hang up on either side.
  5. Call the app again. Observe that you receive this error: https://developers.telnyx.com/docs/api/v2/overview/errors/90018