PJSIP add Diversion on outgoing leg

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.