Newbie question! How can I control the dial plan from code?

Here is the scenario I wanna do:

  1. Dial a phone number.
  2. If the call is answered, redirect the call to another channel.

The question is what should I do to do that scenario? I can dial a number from code using the AMI interface (Originate command), but it just return a message and no call handler or something? If I passed a dial plan context to the Originate command, that context is always executed even if the called person does not answer :question:

Any help please?