Intercept calls before hangup

How can I intercept a call when one party hangs up, and I want to play an automated message to the other party?
Thanks