PJSIP add Diversion on outgoing leg

Hi all,

Using ChanSIP it was pretty easy to add a diversion header in a custom dialplan before sending the call externally:

exten => s,n,Diversion:sip:${FROM_DID}@ssw
exten => s,n,Dial(SIP/trunk_out/XXXXXXXXX)

Doing the same using PJSIP, it seems a bit more complicated. I found a working setup using:

exten => addheader,1,Set(PJSIP_HEADER(add,Diversion)=sip:${FROM_DID}@ssw)
exten => addheader,n,Return

exten => s,n,Dial(PJSIP/[email protected]_trunk_out,,b(outgoing-call^addheader^1))

Is this the best way to accomplish the same? :slight_smile:


I don’t understand how the first case could possibly work, as it is syntactically invalid.

The correct way of setting Diversion headers, in Asterisk, is with the REDIRECTING function. You shouldn’t try to set it explicitly as it is owned by Asterisk.

I don’t know how well FreePBX handles connected line updates, but it is possible that, with the right high level settings, it may even work automatically.

PS Please markup dialplan (and logs) as pre-formatted text, as the forum can garble them, otherwise, e.g for what you provided

Hi @david55,

Thanks for your quick reply. (changed the markup)

Some of our inbound routes are arriving directly on a custom destination… which forwards the call externally. How can we force a specific number to be set in the Diversion Header using the REDIRECTING function? (using a PJSIP trunk)


I imagine you will need to do it in a pre-dial hook (the same as you would have to do for custom headers).

The same procedure should work for all technologies that can generate a signalling event message equivalent to a SIP Diverson header, i.e. it should work for chan_sip, and may work for ISDN.

I tried the following line… with no luck. The Diversion Header is not showing up in the outbound call:

exten => s,n,Set(REDIRECTING(from-num,i)=${FROM_DID})

You don’t want the inhibit option. Does the log show that FROM_DID is being substituted. Otherwise you may have to copy it to an inheritable variable.

Even when setting the value fixed… the Diversion is not showing in the outbound call.
So I guess there is no “better” option than the one I already provided? :slight_smile:

Have you enabled send_diversion for the endpoint. I’m not sure if this has been added to the GUI, or whether you will have to use customisation on the endpoint.

I have an inbound route are arriving directly on a custom destination… so no endpoint in between.

You can’t use chan_pjsip without their being endpoint. pjsip_trunk_out is the endpoint in your example.

I see yes. In the GUI there is no setting… But I don’t think this will be the issue. When using the subroutine it works.

That’s because you are bypassing the proper mechanisms,

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