Last active
June 1, 2021 23:17
-
-
Save multitel/d19de6b09802697c73b55e8f562a17ad to your computer and use it in GitHub Desktop.
asterisk sms (re)delivery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File: /etc/asterisk/extensions.conf (don't use odbc as this becomes db intensive on a large number of messages) | |
[websms] | |
exten => _X.,1,NoOp(SMS receiving dialplan invoked) | |
exten => _X.,n,NoOp(To ${MESSAGE(to)}) | |
exten => _X.,n,NoOp(From ${MESSAGE(from)}) | |
exten => _X.,n,NoOp(Body ${MESSAGE(body)}) | |
exten => _X.,n,Set(ACTUALTO=${CUT(MESSAGE(to),@,1)}) | |
exten => _X.,n,ExecIf($["${ACTUALTO}" != "sip:${EXTEN}"]?Set(ACTUALTO=sip:${EXTEN})) | |
exten => _X.,n,MessageSend(${ACTUALTO},${MESSAGE(from)}) | |
exten => _X.,n,NoOp(Send status is ${MESSAGE_SEND_STATUS}) | |
exten => _X.,n,GotoIf($["${MESSAGE_SEND_STATUS}" != "SUCCESS"]?sendfailedmsg) | |
exten => _X.,n,Hangup() | |
; | |
; Handle failed messaging | |
exten => _X.,n(sendfailedmsg),NoOp(Sending error to user) | |
exten => _X.,n,Set(SRC=${MESSAGE(from)}) | |
exten => _X.,n,Set(DST=${MESSAGE(to)}) | |
exten => _X.,n,Set(MSG=${MESSAGE(body)}) | |
exten => _X.,n,Set(MESSAGE(body)="[${STRFTIME(${EPOCH},,%d%m%Y-%H:%M:%S)}] Your message to ${EXTEN} has failed. Sending when extension becomes available again") | |
exten => _X.,n,Set(ME_1=${CUT(MESSAGE(from),<,2)}) | |
exten => _X.,n,Set(ACTUALFROM=${CUT(ME_1,@,1)}) | |
exten => _X.,n,MessageSend(${ACTUALFROM},${SRC}) | |
exten => _X.,n,GotoIf($["${INQUEUE}" != "1"]?startq) | |
exten => _X.,n,Hangup() | |
; | |
exten => _X.,n(startq),NoOp(Queueing messaging for offline extensions) | |
exten => _X.,n,Set(MSGTIME=${STRFTIME(${EPOCH},,%d%m%Y-%H:%M:%S)}) | |
exten => _X.,n,SYSTEM(/var/lib/asterisk/agi-bin/astqueue.sh -SRC '${SRC}' -DST '${DST}' -MSG '${MSG}') | |
exten => _X.,n,Hangup() | |
[app-fakeanswer] | |
exten => _X.,1,NoCDR | |
exten => _X.,n,Set(DESTDEV=${EXTEN}) | |
exten => _X.,n,Set(THISDEVSTATE=${DEVICE_STATE(SIP/${DESTDEV})}) | |
exten => _X.,n,GotoIf($["${THISDEVSTATE}" = "UNAVAILABLE"]?hang) | |
exten => _X.,n,GotoIf($["${THISDEVSTATE}" = "UNKNOWN"]?hang) | |
exten => _X.,n,Answer | |
exten => _X.,n,Hangup() | |
exten => _X.,n(hang),Hangup() | |
File: /var/lib/asterisk/agi-bin/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 "sip show peers" | grep -c $destexten`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-$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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello
finally I've a file in /var/lib/asterisk/outgoing/1010-1605618984.830.call
inside the file:
Channel: Local/1010@app-fakeanswer
CallerID: "Mark" sip:1011@miodominio.net:5061
Maxretries: 100
RetryTime: 30
Context: sms-home
Extension: 1010
Priority: 1
Set: MESSAGE(body)=Text to try to send......
Set: MESSAGE(to)=sip:1010@miodominio.net:5061
Set: MESSAGE(from)="Mark" sip:1011@miodominio.net:5061
Set: INQUEUE=1
but nothing doing...
I would like to ask you more information about the voice "Channel:" how it's work?
Why "Local/1010@app-fakeanswer"?
one more step...
I've put SIP instead of Local ... now the problem is:
[Nov 17 15:33:16] ERROR[3603]: netsock2.c:303 ast_sockaddr_resolve: getaddrinfo("app-fakeanswer", "(null)", ...): Name or service not known
[Nov 17 15:33:16] WARNING[3603]: chan_sip.c:6396 create_addr: No such host: app-fakeanswer
[Nov 17 15:33:16] NOTICE[3603]: pbx_spool.c:450 attempt_thread: Call failed to go through, reason (0) Call Failure (not BUSY, and not NO_ANSWER, maybe Circuit busy or down?)