Skip to main content
The First Step
August 16, 2019
Solved

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.

    PhongVu
    PhongVuAnswer
    Community Manager
    August 16, 2019

    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)
        })
    }

    2 replies

    PhongVu
    Community Manager
    PhongVuAnswer
    Community Manager
    August 16, 2019

    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)
        })
    }
    The First Step
    August 17, 2019

    Thank you so much!!!