Detecting call on 3rd line

I have a script that answers the 1st line connected to asterisk (which is coming from another PBX) and then works through a list of external numbers calling out on the 2nd line.

Is there any way my script can detect if a 3rd line has a call come in, and join line 1 to line 3?

Thanks