Look into a pbx system like asterisk and freeswitch.