Best practice using Asterisk SIMPLE Message between SIP & PJSIP extensions

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 :slight_smile:

4 Likes

hy craig24x

can you explain to me how to do that on Freepbx?

Why are you using System() to call cURL commands to send to a local script that you could just call directly from System() and pass arguments into? The use of cURL seems a bit un-needed.

Assuming you’ve already enabled SIP message functionality for CHAN SIP to CHAN SIP & PJSIP to PJSIP, this would simply go in extensions_custom.conf.

From the Web GUI:

  • Select Admin > Config Edit
  • Under Asterisk Configuration Files, look for extensions_custom.conf
  • Paste into extensions_custom.conf

If you don’t want to setup subs for error handling, simply remove lines that contain Gotoif() or GoSub() and if you don’t care to have a debug log, remove the lines that contain System()

If you want more details around error handling , let me know and I’ll share my setup.

Partially preference, partially just needed an easy to read one liner that could be initiated directly from a dial plan. My other thought was using Asterisk’s Curl function, but it became cumbersome working out a universal format that could handle special characters as well as staying compatible with internal SIP and my provider’s SMS API. A simple System curl hit the sweet spot for me :relieved:

Thanks So Much… Its Now Forking Perfect