Asterisk/SMS

From fakedWiki
Revision as of 21:09, 26 August 2016 by Jan (talk | contribs) (1 revision imported)
Jump to: navigation, search

Message Types

The key in understanding the message types is that Asterisk can act as an mobile phone and SMSC - depeding on who we're talking to - while the other parties are always acting as only one role.

  • MTRX: Mobile Terminated, Received - the message was received from the T-Com SMSC by Asterisk (as a mobile phone)
  • MORX: Mobile Originated, Received - the message was received by Asterisk (as a SMSC) from the SIP phone
  • MTTX: Mobile Terminated, Transmit - the message will be send from Asterisk (as a SMSC) to the SIP phone
  • MOTX: Mobile Originated, Transmit - the message will be send to the T-Com SMSC from Asterisk (as a mobile phone)

Numbers used

  • 100: SIP phone extension
  • 7670: extension called by SIP phone (SMSC configured in SIP phone = 767)
  • 97886206: extension for landline in Asterisk
  • 0193010: T-Com SMSC for sending SMS via the landline
    • 01930100: Caller ID sent by T-Com SMS to Asterisk
    • 01930101: Caller ID sent by Asterisk to SIP phone, disguising as the T-Com SMSC

extensions.conf

Your dialplan needs two new extensions:

  • The extension for handling incoming SMS from SIP phones has to be added to your [default] context.
  • The extension for handling incoming SMS from the T-Com SMSC has to be added to the context which handles incoming calls on your landline, for me that is [from-capi].

Calls going to those extensions will be handed over to each respective context, where the SMS() Application deals with them - either acting as as mobile phone when receiving from the T-Com SMSC (message type = MTRX), or as an SMSC when receiving from a SIP phone (message type = MORX).


You also need two new contexts (not four, as sending SMS is not handled in the dialplan, but by re-sending received SMS),

  • The context [sms-mtrx] answers the call from the T-Com SMSC as as mobile phone with SMS(<queuename>, a) and then calls an external script (sms-mttx.sh) to send the received SMS to a SIP phone. (MTRX -> MTTX)
  • The context [sms-morx] answers the call from the SIP phone as a SMSC with SMS(<queuename>, sa) and then calls an external script (sms-motx.sh) to send the received SMS to the T-Com SMSC. (MORX -> MOTX)
; internal calls
[default]
exten => 7670,1,Goto(sms-morx,${EXTEN},1) ; SMS from SIP phone to Asterisk

; calls from ISDN
[from-capi]
exten => 97886206,n,GotoIf($["${CALLERID(num)}" = "01930100"]?sms-mtrx,${EXTEN},1)      ; SMS from T-COM SMSC to Asterisk

; SMS / Mobile Terminated / Receive (T-Com SMSC -> Asterisk)
[sms-mtrx]
exten => _X.,1,NoOp(Incoming SMS from T-Com SMSC)
exten => _X.,n,Answer()
exten => _X.,n,Wait(1)
exten => _X.,n,SMS(${EXTEN},a)
exten => _X.,n,System(/etc/asterisk/sms-mtrx.sh)        ; Forward to SIP phone
exten => _X.,n,Wait(1)
exten => _X.,n,Hangup()

; SMS / Mobile Originated / Receive (SIP Phone -> Asterisk)
[sms-morx]
exten => _X.,1,NoOp(Incoming SMS from ${CALLERID(num)})
exten => _X.,n,Answer()
exten => _X.,n,Wait(1)
exten => _X.,n,SMS(${CALLERID(num)},sa)
exten => _X.,n,System(/etc/asterisk/sms-morx.sh)        ; Forward to T-Com SMSC
exten => _X.,n,Wait(1)
exten => _X.,n,Hangup()

; SMS / Mobile Terminated / Transmit (Asterisk -> SIP phone)
;[sms-mttx]
; resend messages from [sms-mtrx] via sms-mttx.sh

; SMS / Mobile Originated / Transmit (Asterisk -> T-Com SMSC)
;[sms-motx]
; resend messages from [sms-morx] via sms-motx.sh

Scripts

The scripts must be ran as the asterisk user, or the user your Asterisk runs as. In theory this shouldn't matter, as only Asterisk will run them anyways. If you want to use the sms-*tx.sh scripts for sending SMS from something else, like for Nagios notifications or from a webinterface, don't run them as any other user than asterisk - it will fsck up the permissions, and you'll send an endless stream of SMS, which may be very expensive. Just sayin'...

sms-motx.sh

Send SMS via ISDN to T-Com SMSC. Needs 3 arguments: the sender's number, the recipient's number and the text.

#!/bin/bash

SRC="${1}"
DST="${2}"
MSG="${3}"

CALLERID="${SRC}"
CHANNEL='CAPI/contr1/0193010'

smsq --mo --tx \
  --motx-callerid="${CALLERID}" \
  --motx-channel="${CHANNEL}" \
  --da="${DST}" \
  --ud="${MSG}"

sms-mttx.sh

Send SMS to a SIP phone, disguised as T-Com SMSC. Needs 3 arguments: the sender's number, the recipient's number and the text.

#!/bin/bash

SRC="${1}"
DST="${2}"
MSG="${3}"

CALLERID='01930101'
CHANNEL="SIP/${DST}"

smsq --mt --tx \
  --mttx-callerid="${CALLERID}" \
  --mttx-channel="${CHANNEL}" \
  --oa="${SRC}" \
  --ud="${MSG}"

sms-mtrx.sh

Forward all SMS received from the T-Com SMSC to SIP phone 100. (uses sms-mttx.sh)

#!/bin/bash

SPOOL='/var/spool/asterisk/sms/mtrx'

for SMS in `ls -1 "$SPOOL"`; do
  SRC=`grep -e '^oa=' "$SPOOL/$SMS" | sed 's/oa=//'`
  MSG=`grep -e '^ud=' "$SPOOL/$SMS" | sed 's/ud=//'`
  /etc/asterisk/sms-mttx.sh "${SRC}" "100" "${MSG}"
  rm -f "$SPOOL/$SMS"
done

sms-morx.sh

Forward all SMS received from SIP phones to the T-Com SMSC. (uses sms-motx.sh)

#!/bin/bash

SPOOL='/var/spool/asterisk/sms/morx'

for SMS in `ls -1 "$SPOOL"`; do
  DST=`grep -e '^da=' "$SPOOL/$SMS" | sed 's/da=//'`
  MSG=`grep -e '^ud=' "$SPOOL/$SMS" | sed 's/ud=//'`
  /etc/asterisk/sms-motx.sh "97886206" "${DST}" "${MSG}"
  rm -f "$SPOOL/$SMS"
done

Todo

  • check for the Caller ID of the T-Com SMSC on all MSNs
  • forward incoming SMS to different SIP extensions, based on the called MSN
  • call T-Com SMSC from a different MSN, based on the SIP extension

extensions.conf

[from-capi]
exten => _X./01930100,1,Goto(sms-mtrx,${EXTEN},1)

[sms-mtrx]
exten => _X.,n,SMS(${EXTEN},a)
exten => _X.,n,System(/etc/asterisk/sms-mtrx.sh "${EXTEN}")

[sms-morx]
exten => _X.,n,SMS(${CALLERID(num)},sa)
exten => _X.,n,System(/etc/asterisk/sms-morx.sh "${CALLERID(num)}")

sms.conf

MTRX["97886206"]="100"
MORX["100"]="97886206"
MORX["101"]="97886206"
MORX["102"]="97886206"

sms-mtrx.sh

#!/bin/bash

QUEUE="${1}"

source "/etc/asterisk/sms.conf"

SPOOL='/var/spool/asterisk/sms/mtrx'

for SMS in `ls -1 "$SPOOL/{$QUEUE}.*"`; do
  SRC=`grep -e '^oa=' "$SPOOL/$SMS" | sed 's/oa=//'`
  MSG=`grep -e '^ud=' "$SPOOL/$SMS" | sed 's/ud=//'`
  /etc/asterisk/sms-mttx.sh "${SRC}" "$MTRX{${QUEUE}}" "${MSG}"
  rm -f "$SPOOL/$SMS"
done

sms-morx.sh

#!/bin/bash

QUEUE="${1}"

source "/etc/asterisk/sms.conf"

SPOOL='/var/spool/asterisk/sms/morx'

for SMS in `ls -1 "$SPOOL"`; do
  DST=`grep -e '^da=' "$SPOOL/$SMS" | sed 's/da=//'`
  MSG=`grep -e '^ud=' "$SPOOL/$SMS" | sed 's/ud=//'`
  /etc/asterisk/sms-motx.sh "${MORX[${QUEUE}]}" "${DST}" "${MSG}"
  rm -f "$SPOOL/$SMS"
done

sms-motx.sh

#!/bin/bash

SRC="${1}"
DST="${2}"
MSG="${3}"

CALLERID="${SRC}"
CHANNEL="CAPI/contr1/${CALLERID}:0193010"

smsq --mo --tx \
  --motx-callerid="${CALLERID}" \
  --motx-channel="${CHANNEL}" \
  --da="${DST}" \
  --ud="${MSG}"