You can change the api_hangup_hook definition in "local_extension" dial plan to use your custom script but you would probably need to call the existing script if you want normal CDR logs etc.
or you can edit the existing hangup hook lua script but isn't advisable due to possible conflicts when upgrading.
or you can set hangup_after_bridge to false in the necessary dial plans then set your script as an action below the bridge statement. This could require a few tweaks to your scripts to handle ending the call properly.
I'm no expert with the above scenarios but should point you in the right direction