I got it!
The answer is in a comment in this post:
It’s basically the same as the last link I posted, the only difference is that you need to pass the channel as an argument. I’ll paste the answer here in case the link goes offline:
[outboundsip]
exten => s,1,Dial(${SIPCHAN}/${CALLEDNUM}@0435440806,25,gb(outboundsip^set_handler^1(${CHANNEL})))
exten => s,2,Set(SIPcause=${SHARED(SIPcause):4})
… do whatever you need in this extension next.
exten => set_handler,1,NoOp(${ARG1})
exten => set_handler,2,Set(CHANNEL(hangup_handler_push)=outboundsip,outbound_handler,1(${ARG1}))
exten => set_handler,n,Return()
exten => outbound_handler,1,NoOp(Destination channel ${ARG1} has hungup)
same => n,Set(HANGUPCAUSE_STRING=${HANGUPCAUSE_KEYS()}) ; If no hangup causes are available then its probably because it is a regular call and the call ended normally so we just return.
same => n,ExecIf($[“${HANGUPCAUSE_STRING}” = “”]?Return())
same => n,NoOp(Got Channel ID ${HANGUPCAUSE_STRING} master ${ARG1} with Technology Cause Code ${HANGUPCAUSE(${HANGUPCAUSE_STRING},tech)}, Asterisk Cause Code ${HANGUPCAUSE(${HANGUPCAUSE_STRING},ast)})
same => n,Set(SHARED(SIPcause,${ARG1})=${HANGUPCAUSE(${HANGUPCAUSE_STRING},tech)})
same => n,Return()
That’s it! If you need to read the SIP code to make a decision based on it, this is what you need to do.
I forgot to mention, but I’m using Asterisk 18 and it works in this version.