As the title mentions, I’m sharing what I came up with to solution for an instance in which I needed SIP and PJSIP to message each other. There are an abundance of tutorials online for enabling SIP messaging for either SIP or for PJSIP, but they don’t intermix. Messages will fail between technology types without a way to distinguish which technology type asterisk should use per extension.
For this, I’ve done the following with a single message context (stored in extensions_custom.conf)
The key is to separate extension ranges for each technology type as to allow actions to be taken based on the extension range.
My 2XX range is SIP, while the 3XX range is PJSIP. The context also has some error handling based on device states and creates a csv log using php via curl commands. I didn’t detail those below as that is purely preference and lines for Gotoif(), GoSub(), and System() can be removed without impacting functionality.
[sip-msg]
;2XX - Range 200-299 for internal SIP extensions
exten => _2XX,1,Set(DEVSTATE=${DEVICE_STATE(SIP/${EXTEN})})
same => n,GotoIf($["${DEVSTATE}" = "UNAVAILABLE"]?errormsg1)
same => n,GotoIf($["${DEVSTATE}" = "INVALID"]?errormsg2)
same => n,GotoIf($["${DEVSTATE}" = "UNKNOWN"]?errormsg2)
same => n,MessageSend(sip:${EXTEN},${MESSAGE(from)})
same => n,System(curl --data "from=${MESSAGE(from)}" --data "to=${EXTEN}" --data-urlencode "message=${MESSAGE(body)}" 127.0.0.1/messagelog-internal.php?)
same => n,Hangup()
same => n(errormsg1),Log(NOTICE,Extension is ${DEVSTATE})
same => n,Gosub(msg-error1,s,1(${MESSAGE(from)},${EXTEN},${DEVSTATE}))
same => n(errormsg2),Log(NOTICE,Extension is ${DEVSTATE})
same => n,Gosub(msg-error2,s,1(${MESSAGE(from)},${EXTEN},${DEVSTATE}))
;3XX - Range 300-399 for internal PJSIP extensions
exten => _3XX,1,Set(DEVSTATE=${DEVICE_STATE(PJSIP/${EXTEN})})
same => n,GotoIf($["${DEVSTATE}" = "UNAVAILABLE"]?errormsg1)
same => n,GotoIf($["${DEVSTATE}" = "INVALID"]?errormsg2)
same => n,GotoIf($["${DEVSTATE}" = "UNKNOWN"]?errormsg2)
same => n,MessageSend(pjsip:${EXTEN},${MESSAGE(from)})
same => n,System(curl --data "from=${MESSAGE(from)}" --data "to=${EXTEN}" --data-urlencode "message=${MESSAGE(body)}" 127.0.0.1/messagelog-internal.php?)
same => n,Hangup()
same => n(errormsg1),Log(NOTICE,Extension is ${DEVSTATE})
same => n,Gosub(msg-error1,s,1(${MESSAGE(from)},${EXTEN},${DEVSTATE}))
same => n(errormsg2),Log(NOTICE,Extension is ${DEVSTATE})
same => n,Gosub(msg-error2,s,1(${MESSAGE(from)},${EXTEN},${DEVSTATE}))
;11 digit North America - send sms via http gateway
exten => _1NXXXXXXXXX,1,Set(CALLERID(num)=11233214587)
same => n,set(MESSAGE(from)=${MESSAGE(from):5:3})
same => n,System(do your external stuff here)
same => n,System(curl --data "from=${MESSAGE(from)}" --data "outboundid=${CALLERID(num)}" --data "to=${EXTEN}" --data-urlencode "message=${MESSAGE(body)}" 127.0.0.1/messagelog-outbound.php?)
same => n,Hangup()
Also worth mentioning, this assumes you know how to set SIP to accept out of call messages and point both SIP and PJSIP to the message context [sip-msg].
Hopefully this helps someone