question

top notch avatar image
top notch asked ·

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.

apicallcall routingforward call
1 |1000 characters needed characters left characters exceeded

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)
    })
}
Share
1 |1000 characters needed characters left characters exceeded

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!!!

Share
1 |1000 characters needed characters left characters exceeded

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

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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