Retry call via another SIP trunk based on response

Hello everyone!

I’m trying to achieve something and I couldn’t figure it out how to do it.
I have more than one SIP provider and sometimes a call that cannot be completed via one trunk, works just fine via the other one.
I want to try dialing via one of the SIP trunks and in case it returns me “404 - Not found” I want asterisk to try dialing via the other SIP trunk. But just when the response is 404 on the first trunk. How can I accomplish this?

Thanks in advance!

https://wiki.asterisk.org/wiki/display/AST/Hangup+Cause

However, 404 is an usual code to want to retry on. Normally you should use dialplan patterns to prevent a number being sent to to a network which won’t recognize it.

Thanks David,

I’ll take a look at this example.
About the 404, unfortunately here, some providers have problems with their own dialplans (I don’t know why) and are unable to call to some numbers, then placing the call via another provider solves the problem.

https://wiki.asterisk.org/wiki/display/AST/Asterisk+18+Function_PJSIP_RESPONSE_HEADER

This says to be reading the 200 OK response. I’m curious if this will work with different kind of 2xx/4xx/5xx responses.

If it will, then you can read the response and make a decision then.

I’ve tried David’s solution but it didn’t work out, because when the code is 404, it doesn’t show up in the handler.

PitzKey, I’m using chan_sip, so I think this pjsip function won’t help me unfortunately.

Is there another way to do this?

I found this article and now I can read the SIP Code 404:

https://www.answergig.com/1293/how-to-get-the-sip-response-code-in-asterisk-11

The problem is in the last part of the tutorial, when I need to return the variable to the main context via SHARED variable, it doesn’t work. I’m still trying to find a way to do this

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.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.