Run Cmd on Disconnect

Asterisk 11.0.1
Cisco 7971

My intention on call connect is to send a CiscoIPPhoneStatus (an non-closable window with some information about the caller) within the dial plan via a System call. As an example, it could be for caller ID info (run by a script, and then returned to the phone), and thus would need to be cleared upon disconnect.

However, since it is an non-closable window, the CiscoIPPhoneStatus message was designed to be closed manually, therefore you need another command to close it.

I can already send the show command upon ring, that is done. And I have the available command to close it, I just need help triggering it automatically.

How can I detect:

  1. a pickup by another line and thus send a system command (i.e. a cancel of the CiscoIPPhoneStatus) on the phones which did not pick the line?
  2. a hangup or disconnect and thus send a system command (i.e a cancel the CiscoIPPhoneStatus) to the phone which answered?

Thank you kindly.