question

top notch avatar image
top notch asked Becky-Community_Manager edited

How to create forward call optionally based on incoming number?

Hello,

Now i can get the incoming number using webhook, and I want to make optional forwarding call.

If the incoming number is 1111, then it will make forward call,

If the incoming number is 2222, then don't forward and disconnect.

It must be dynamic and programmatic.(Prefer to use Node.js).

So it means, specific people can contact me.

Please provide me any sample code or guide.

Thanks in advance.

rest api
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered

You can use the Call Control API to forward a call to another number. Assumed that you got the incoming call event via webhook. Parse the notification payload to get the call information from the "activeCalls"
try the code below: (assumed that platform is the platform instance you get from the RingCentral JavaScript SDK)

for (var call of activeCalls){
  if (call.telephonyStatus == "Ringing" && call.direction == "Inbound"){
    console.log("CALLER ID: " + call.from)
    if (call.from == "1111"){
      forwardCall(call, "number to forward the call to")
    }else if (call.from == "2222"){
      disconnect(call)
    }
    break
  }
}

function forwardCall(call, toNumber){
  var endpoint = '/restapi/v1.0/account/accountId/telephony/sessions/'
  endpoint += call.telephonySessionId
  endpoint += '/parties/' + call.partyId + '/forward'
  platform.post(endpoint, {"phoneNumber":toNumber} )
    .then(function(resp){
      console.log(resp)
      var json = resp.json()
      console.log(JSON.stringify(json))
    })
    .catch(function(e){
      console.log(e)
    })
}

function rejectCall(call){
  var endpoint = '/restapi/v1.0/account/accountId/telephony/sessions/'
  endpoint += call.telephonySessionId
  endpoint += '/parties/' + call.partyId + '/reject'
  platform.post(endpoint)
    .then(function(resp){
      console.log(resp)
      var json = resp.json()
      console.log(JSON.stringify(json))
    })
    .catch(function(e){
      console.log(e)
    })
}
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

top notch avatar image
top notch answered

Thank you so much!!!

1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys