Home > Net >  How to forward an incoming call to a Twilio number to another number?
How to forward an incoming call to a Twilio number to another number?

Time:01-22

I need to forward incoming calls to my Twilio number to a personal number. I need to do this by Java code because I have a few other Business logic executing when TwiMl App redirect the request and because of that, I can't use Twilio Studio.

I tried a few ways but didn't work and can't make the following method work. I think these are deprecated now,

Call call = Call.creator(
    new PhoneNumber(TWILIO_NUMBER),
    new PhoneNumber(FORWARD_TO),
    new PhoneNumber(TWILIO_NUMBER)
).create();

And even following redirect() method is also not there now.

Call.updater("call-sid")
    .redirect(FORWARD_TO).update();

So what I need is when a call comes to my Twilio number then that call should be forwarded to my personal number. So how to do this? Can anybody help me? Thanks in advance.

CodePudding user response:

In the endpoint that handles incoming calls, create a new instance of the VoiceResponse class and add a verb with the personal number as an argument:

VoiceResponse response = new VoiceResponse.Builder()
    .dial(new Dial.Builder().number(new Number.Builder(FORWARD_TO).build()).build())
    .build();

Finally, return the TwiML response as XML:

response.toXml();

CodePudding user response:

In the latest version of the Twilio Java library, the Call.creator and Call.updater methods have been deprecated. To forward an incoming call to a personal number, you can use the VoiceClient class to create a TwiML response that includes a verb that dials the personal number.

Here's an example of how you can forward an incoming call to a personal number using the Twilio Java library:

import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.twiml.VoiceResponse;
import com.twilio.twiml.voice.Dial;
import com.twilio.twiml.voice.Number;

public class ForwardCall {
    // Find your Account Sid and Auth Token at twilio.com/console
    public static final String ACCOUNT_SID = "your_account_sid";
    public static final String AUTH_TOKEN = "your_auth_token";
    public static final String TWILIO_NUMBER = "your_twilio_number";
    public static final String FORWARD_TO = "forward_to_number";

    public static void main(String[] args) {
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

        Number number = new Number.Builder(FORWARD_TO).build();
        Dial dial = new Dial.Builder().number(number).build();
        VoiceResponse response = new VoiceResponse.Builder().dial(dial).build();

        System.out.println(response.toXml());
    }
}

You can add this code as endpoint in TwiMl application and when a call comes to your Twilio number, the call will be forwarded to the personal number specified in the FORWARD_TO variable.

You can also execute other business logic in your endpoint before forwarding the call to personal number, using the same endpoint.

  • Related