I have been using this software Linphone successfully for a long time. It runs in a raspberry as a service.
I use it to call my phones. I think it should be able to detect a call and therefore activate an action. To look at this side maybe.
Look at this topic I created last year