Skip to content

Instantly share code, notes, and snippets.

@multitel
Last active June 1, 2021 23:17
Show Gist options
  • Save multitel/d19de6b09802697c73b55e8f562a17ad to your computer and use it in GitHub Desktop.
Save multitel/d19de6b09802697c73b55e8f562a17ad to your computer and use it in GitHub Desktop.
asterisk sms (re)delivery
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
@linuxacademyir
Copy link

Hello i am trying to make this work, on asterisk 13
when i send msg from extension 100 to 106
i get this msg from on extension 100: "[06092020-01:35:32] Your message to 106 has failed. Sending when extension becomes available again"
but when user 106 goes online, nothing happens,
this is asterisk log
Executing [106@sms:1] NoOp("Message/ast_msg_queue", "SMS receiving dialplan invoked") in new stack
-- Executing [106@sms:2] NoOp("Message/ast_msg_queue", "To sip:106@192.168.1.150") in new stack
-- Executing [106@sms:3] NoOp("Message/ast_msg_queue", "From sip:100@192.168.1.150") in new stack
-- Executing [106@sms:4] NoOp("Message/ast_msg_queue", "Body Hello") in new stack
-- Executing [106@sms:5] Set("Message/ast_msg_queue", "ACTUALTO=sip:106") in new stack
-- Executing [106@sms:6] ExecIf("Message/ast_msg_queue", "0?Set(ACTUALTO=sip:106)") in new stack
-- Executing [106@sms:7] MessageSend("Message/ast_msg_queue", "sip:106,sip:100@192.168.1.150") in new stack
-- Executing [106@sms:8] NoOp("Message/ast_msg_queue", "Send status is FAILURE") in new stack
-- Executing [106@sms:9] GotoIf("Message/ast_msg_queue", "1?sendfailedmsg") in new stack
-- Goto (sms,106,11)
-- Executing [106@sms:11] NoOp("Message/ast_msg_queue", "Sending error to user") in new stack
-- Executing [106@sms:12] Set("Message/ast_msg_queue", "SRC=sip:100@192.168.1.150") in new stack
-- Executing [106@sms:13] Set("Message/ast_msg_queue", "DST=sip:106@192.168.1.150") in new stack
-- Executing [106@sms:14] Set("Message/ast_msg_queue", "MSG=Hello") in new stack
-- Executing [106@sms:15] Set("Message/ast_msg_queue", "MESSAGE(body)="[06092020-01:39:33] Your message to 106 has failed. Sending when extension becomes available again"") in new stack
-- Executing [106@sms:16] Set("Message/ast_msg_queue", "ME_1=sip:100@192.168.1.150>") in new stack
-- Executing [106@sms:17] Set("Message/ast_msg_queue", "ACTUALFROM=sip:100") in new stack
-- Executing [106@sms:18] MessageSend("Message/ast_msg_queue", "sip:100,sip:100@192.168.1.150") in new stack
-- Executing [106@sms:19] GotoIf("Message/ast_msg_queue", "1?startq") in new stack
-- Goto (sms,106,21)
-- Executing [106@sms:21] NoOp("Message/ast_msg_queue", "Queueing messaging for offline extensions") in new stack
-- Executing [106@sms:22] Set("Message/ast_msg_queue", "MSGTIME=06092020-01:39:33") in new stack
-- Executing [106@sms:23] System("Message/ast_msg_queue", "/var/lib/asterisk/agi-bin/astqueue.sh -SRC 'sip:100@192.168.1.150' -DST 'sip:106@192.168.1.150' -MSG 'Hello'") in new stack
-- Executing [106@sms:24] Hangup("Message/ast_msg_queue", "") in new stack
== Spawn extension (sms, 106, 24) exited non-zero on 'Message/ast_msg_queue'

@multitel
Copy link
Author

multitel commented Sep 7, 2020 via email

@linuxacademyir
Copy link

Hello
thank you for your reply
i tried to run astqueue.sh
here is the results:
ERROR: No source. Quitting.
SMS:[1] . Runtime:15:34:12_09/17/20. UniqueCode:1600340652

@brunopiras
Copy link

brunopiras commented Nov 17, 2020

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?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment