SIP/SMS with FreePBX - Wiki

Check out our YouTube channel to watch our simple tutorials that will help you set up most of our features.

SIP/SMS with FreePBX

From Wiki

Jump to: navigation, search



This page will detail the basic configuration required in order to send and receive SMS messages through FreePBX

The information in this page is based on the newer PJSIP channel driver. This is because the older chan_sip driver does not correctly implement authentication for SIP messaging, which is mandatory with servers for security reasons. The current version of FreePBX supports using both SIP channel drivers side by side without any issue.

For this particular tutorial, we assume the following:

  • You have configured your FreePBX so that it has a PJSIP trunk that is registering with one of the POPs (Point of Presence).
  • You have added one or more PJSIP extensions to your FreePBX configuration, with appropriate routes for sending and receiving phone calls. Each of these extensions is named after one of the SMS enabled DIDs from your account and the caller ID is set to the same number.
  • You have a soft-phone that is configured for registering with the above FreePBX extension, for making and receiving phone calls. This soft-phone also has the ability to send and receive SIP text messages.

Setting the messaging contexts

Setting the inbound messaging context

The first step in implementing SIP messaging with FreePBX is setting the contexts for inbound and outbound messaging. These contexts are the places, in the FreePBX dial-plan, where inbound and outbound messages will be handled. The inbound context is specified as part of your PJSIP Trunk settings:

  1. Go to Connectivity/Trunks. Find the PJSIP Trunk that is the one connecting to the POP in the list and edit it.
  2. In PJSIP Settings, choose the Advanced tab.
  3. Scroll to the bottom of the page and enter sms-in in the Message Context field.
  4. Submit your changes and Apply the new config.

Setting the outbound messaging context

Setting the outbound context is a little trickier. This setting needs to be applied to each PJSIP extension that is to be used for sending messages. However, the setting does not exist in the user interface, so it needs to be added manually using the configuration editor.

  1. Go to Admin/Config Edit. In the Asterisk custom Configuration Files, find pjsip.endpoint_custom_post.conf.
  2. For each of your PJSIP extension, add the following block of text, making sure to replace NNNNNNNNNN with the extension identifier, which should be the same as one of your DIDs, as mentioned above. Note the (+) symbol, which specifies that the setting is to be added to the existing extension:
[NNNNNNNNNN](+)         ; Replace NNNNNNNNNN with the corresponding PJSIP extension name

Filling in the dial-plan

The next step is to fill in your FreePBX dial-plan with the necessary code to handle inbound and outbound SMS.

  1. Go to Admin/Config Edit. In the Asterisk custom Configuration Files, find extensions_custom.conf.
  2. Add each of the following blocks of code, making the appropriate replacements as indicated:

Filling in the inbound messaging context

The following assumes that there is a direct correspondence between the destination number, taken from the X-SMS-To SIP header, and the name of the PJSIP profile the message should be sent to. Otherwise, you will need to add some code to do the mapping.

exten => _.,1,NoOp(Inbound SMS dialplan invoked)
exten => _.,n,NoOp(To ${MESSAGE(to)})
exten => _.,n,NoOp(From ${MESSAGE(from)})
exten => _.,n,NoOp(Body ${MESSAGE(body)})
exten => _.,n,Set(NUMBER_TO=${MESSAGE_DATA(X-SMS-To)})
exten => _.,n,Set(HOST_TO=${CUT(MESSAGE(to),@,2)})
exten => _.,n,Set(ACTUAL_FROM=${MESSAGE(from)})
exten => _.,n,Set(ACTUAL_TO=pjsip:${NUMBER_TO}@${HOST_TO})
exten => _.,n,MessageSend(${ACTUAL_TO},${ACTUAL_FROM})
exten => _.,n,NoOp(Send status is ${MESSAGE_SEND_STATUS})
exten => _.,n,Hangup()

Filling in the outbound messaging context

Make sure you replace TRUNK_NAME with the name of your PJSIP trunk that is registering with a server. Also replace VOIPMS_ACCOUNT with the account (or sub-account) that is used to do so, as well as VOIPMS_SERVER_NAME with the domain name of the server (something like for example).

exten => _.,1,NoOp(Outbound Message dialplan invoked)
exten => _.,n,NoOp(To ${MESSAGE(to)})
exten => _.,n,NoOp(From ${MESSAGE(from)})
exten => _.,n,NoOp(Body ${MESSAGE(body)})
exten => _.,n,Set(NUMBER_FROM=${CUT(CUT(MESSAGE(from),@,1),:,2)})
exten => _.,n,Set(NUMBER_TO=${CUT(CUT(MESSAGE(to),@,1),:,2)})
exten => _.,n,Set(ACTUAL_FROM="${NUMBER_FROM}" <sip:VOIPMS_ACCOUNT@VOIPMS_SERVER_NAME>) ; Replace VOIPMS_ACCOUNT and VOIPMS_SERVER_NAME with their corresponding values
exten => _.,n,Set(ACTUAL_TO=pjsip:TRUNK_NAME/<sip:${NUMBER_TO}@VOIPMS_SERVER_NAME>)      ; Replace TRUNK_NAME and VOIPMS_SERVER_NAME with their corresponding values
exten => _.,n,MessageSend(${ACTUAL_TO},${ACTUAL_FROM})
exten => _.,n,NoOp(Send status is ${MESSAGE_SEND_STATUS})
exten => _.,n,Hangup()

Personal tools
Actions Blog
Guides (Français)
Guías (Español)