Thanks for sharing, this are my two cents:
Dialplan context:
;---------- SMS -------------------------------------
[websms]
exten => _*.,1,Goto(from-internal,${EXTEN},1)
exten => _XXXX,1,Verbose(4, "SMS checking dialplan invoked")
same = n,GosubIf($[${LEN(${MESSAGE(from)})} > 0]?sms)
same = n,Verbose(4, "It is not SMS, going to Call ${EXTEN}")
same = n,Goto(from-internal,${EXTEN},1)
same = n,HangUp()
same = n(sms),Macro(sms-user,)
same = n,HangUp()
exten => h,1,Macro(hangupcall,)
[app-fakeanswer]
exten => _X.,1,NoCDR
same = n,Verbose(4, "Sending message from queue")
same = n,Set(ThisDevState=${DEVICE_STATE(PJSIP/${EXTEN})})
same = n,GotoIf($[${ThisDevState}=UNAVAILABLE]?hang)
same = n,GotoIf($[${ThisDevState}=UNKNOWN]?hang)
same = n,GotoIf($[${ThisDevState}=INVALID]?hang)
same = n,Answer()
same = n,Hangup()
same = n(hang),Hangup()
[macro-sms-user]
exten => s,1,Verbose(4, "Sending message, To ${MESSAGE(to)}, From ${MESSAGE(from)}}")
same = n,Set(LOCAL(EndpointTo)=${CUT(MESSAGE(to),@,1)})
same = n,Set(LOCAL(Tech)=${CUT(EndpointTo,:,1)})
same = n,Set(LOCAL(EndpointTo)=${CUT(EndpointTo,:,2)})
same = n,Verbose(4, "DEVICE_STATE: ${DEVICE_STATE(PJSIP/${EndpointTo})}")
same = n,GotoIf($[${DEVICE_STATE(${Tech}/${EndpointTo})}=UNAVAILABLE]?sendfailedmsg)
same = n,GotoIf($[${DEVICE_STATE(${Tech}/${EndpointTo})}=UNKNOWN]?sendfailedmsg)
same = n,GotoIf($[${DEVICE_STATE(${Tech}/${EndpointTo})}=INVALID]?invalid)
same = n,Verbose(4, "Sending now message, To ${ActualTo}, From ${MESSAGE(from)})
;
same = n,Set(LOCAL(ContactsTo)=${PJSIP_DIAL_CONTACTS(${EndpointTo})})
same = n,While($["${SET(ContactTo=${SHIFT(ContactsTo,&):6})}" != ""])
same = n,Set(LOCAL(ActualTo)=${Tech}:${ContactTo})
same = n,MessageSend(${ActualTo},${MESSAGE(from)})
same = n,Verbose(4, "Send status is ${MESSAGE_SEND_STATUS}")
same = n,EndWhile
same = n,Hangup()
;
same = n(sendfailedmsg),Macro(sms-failed,1)
same = n,Hangup()
same = n(invalid),Macro(sms-invalid,1)
same = n,Hangup()
[macro-sms-failed]
exten => s,1,Verbose(4, "Sending error Unavailable to user, Original: To ${MESSAGE(to)}, From ${MESSAGE(from)}}")
same = n,Set(LOCAL(Msg)=${MESSAGE(body)})
same = n,Set(LOCAL(MsgTime)=${STRFTIME(${EPOCH},,%d%m%Y-%H:%M:%S)})
same = n,Set(MESSAGE(body)=[${MsgTime}] Your message "${Msg}" to ${EndpointTo} has failed. Sending when extension becomes available again)
same = n,Set(LOCAL(EndpointFrom)=${CUT(MESSAGE(from),<,2)})
same = n,Set(LOCAL(EndpointFrom)=${CUT(EndpointFrom,@,1)})
same = n,Set(LOCAL(EndpointFrom)=${CUT(EndpointFrom,:,2)})
same = n,Verbose(4, "DEVICE_STATE: ${DEVICE_STATE(PJSIP/${EndpointFrom})}")
same = n,GotoIf($[${DEVICE_STATE(${Tech}/${EndpointFrom})}=UNAVAILABLE]?hang)
same = n,GotoIf($[${DEVICE_STATE(${Tech}/${EndpointFrom})}=UNKNOWN]?hang)
same = n,GotoIf($[${DEVICE_STATE(${Tech}/${EndpointFrom})}=INVALID]?hang)
same = n,Verbose(4, "Replying: To ${ReplyTo}, From ${ReplyFrom}")
;
same = n,Set(LOCAL(ContactsFrom)=${PJSIP_DIAL_CONTACTS(${EndpointFrom})})
same = n,While($["${SET(ContactFrom=${SHIFT(ContactsFrom,&):6})}" != ""])
same = n,Set(LOCAL(ReplyTo)=${Tech}:${ContactFrom})
same = n,Set(LOCAL(ReplyFrom)=<${MESSAGE(to):2}>)
same = n,MessageSend(${ReplyTo},${ReplyFrom})
same = n,Verbose(4, "Send status is ${MESSAGE_SEND_STATUS}")
same = n,EndWhile
same = n,GotoIf($["${INQUEUE}" != "1"]?startq)
same = n,Hangup()
;
same = n(startq),Macro(sms-queue,1)
same = n,Hangup()
same = n(hang),Hangup()
[macro-sms-queue]
exten => s,1,Verbose(4, "Queueing messaging for offline")
same = n,SYSTEM(/var/lib/asterisk/agi-bin/astqueue.sh -SRC '${MESSAGE(from)}' -DST '${MESSAGE(to)}' -MSG '${Msg}' -TIME '${MsgTime}')
same = n,Hangup()
[macro-sms-invalid]
exten => s,1,Verbose(4, "Sending error Invalid to user, Original: To ${MESSAGE(to)}, From ${MESSAGE(from)}}")
same = n,Set(LOCAL(Msg)=${MESSAGE(body)})
same = n,Set(LOCAL(MsgTime)=${STRFTIME(${EPOCH},,%d%m%Y-%H:%M:%S)})
same = n,Set(MESSAGE(body)=[${MsgTime}] Your message "${Msg}" to ${EndpointTo} has failed. Extension is invalid or does no exist)
same = n,Set(LOCAL(EndpointFrom)=${CUT(MESSAGE(from),<,2)})
same = n,Set(LOCAL(EndpointFrom)=${CUT(EndpointFrom,@,1)})
same = n,Set(LOCAL(EndpointFrom)=${CUT(EndpointFrom,:,2)})
same = n,Verbose(4, "DEVICE_STATE: ${DEVICE_STATE(PJSIP/${EndpointFrom})}")
same = n,GotoIf($[${DEVICE_STATE(${Tech}/${EndpointFrom})}=UNAVAILABLE]?hang)
same = n,GotoIf($[${DEVICE_STATE(${Tech}/${EndpointFrom})}=UNKNOWN]?hang)
same = n,GotoIf($[${DEVICE_STATE(${Tech}/${EndpointFrom})}=INVALID]?hang)
same = n,Verbose(4, "Replying: To ${ReplyTo}, From ${ReplyFrom}")
;
same = n,Set(LOCAL(ContactsFrom)=${PJSIP_DIAL_CONTACTS(${EndpointFrom})})
same = n,While($["${SET(ContactFrom=${SHIFT(ContactsFrom,&):6})}" != ""])
same = n,Set(LOCAL(ReplyTo)=${Tech}:${ContactFrom})
same = n,Set(LOCAL(ReplyFrom)=<${MESSAGE(to):2}>)
same = n,MessageSend(${ReplyTo},${ReplyFrom})
same = n,Verbose(4, "Send status is ${MESSAGE_SEND_STATUS}")
same = n,EndWhile
same = n,Hangup()
Script astqueue.sh:
#!/bin/bash
#VARIABLES
maxretry=100
retryint=30
#
#CONSTANTS
ERRORCODE=0
d_unique=`date +%s`
d_friendly=`date +%T_%D`
astbin=`which asterisk`
myrandom=$[ ( $RANDOM % 1000 ) + 1 ]
#
function bail(){
echo "SMS:[$ERRORCODE] $MSGOUT. Runtime:$d_friendly. UniqueCode:$d_unique"
exit $ERRORCODE
}
while test -n "$1"; do
case "$1" in
-SRC)
source="$2"
shift
;;
-DST)
dest="$2"
shift
;;
-MSG)
message="$2"
shift
;;
-TIME)
originaltime="$2"
shift
;;
esac
shift
done
#
#
if [[ "$source" == "" ]]; then
echo "ERROR: No source. Quitting."
ERRORCODE=1
bail
fi
if [[ "$dest" == "" ]]; then
echo "ERROR: No usable destination. Quitting."
ERRORCODE=1
bail
fi
if [[ "$message" == "" ]]; then
echo "ERROR: No message specified.Quitting."
ERRORCODE=1
bail
fi
#
# generate call file
mydate=`date +%d%m%y`
logdate=`date`
#
# Check to see if extension exists
destexten=`echo $dest | cut -d @ -f1 | cut -d : -f2`
ifexist=`$astbin -rx "pjsip show endpoints" | grep $destexten | grep -c Endpoint`
if [[ "$ifexist" == "0" ]]; then
echo "Destination extension don't exist, exiting.."
ERRORCODE=1
bail
fi
# If that conditions passes, then we will queue the message,
# Todo: write other conditions too to keep the sanity of the looping
filename="$destexten-$originaltime-$d_unique.$myrandom.call"
echo -e "Channel: Local/$destexten@app-fakeanswer
CallerID: $source
Maxretries: $maxretry
RetryTime: $retryint
Context: websms
Extension: $destexten
Priority: 1
Set: MESSAGE(body)=$message
Set: MESSAGE(to)=$dest
Set: MESSAGE(from)=$source
Set: INQUEUE=1 "> /var/spool/asterisk/tmp/$filename
# move files
chown asterisk:asterisk /var/spool/asterisk/tmp/$filename
chmod 777 /var/spool/asterisk/tmp/$filename
sleep 3
mv /var/spool/asterisk/tmp/$filename /var/spool/asterisk/outgoing/
#
ERRORCODE=0
bail
Regards, and thanks again for the good 2 post.