Advertisement
mikeisfly

astqueue.sh (with message ordering)

Jun 12th, 2014
884
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 1 0
  1. !/bin/bash
  2. ##############################################################################
  3. # v0.2 #
  4. # copyleft Sanjay Willie [email protected] #
  5. # SCRIPT PURPOSE: GENERATE SMS OFFLINE QUEUE #
  6. # GEN INFO: Change variables sections #
  7. ##############################################################################
  8. # This script was edit by Michael A. Gates #
  9. # because it didn't work in freepbx 5.11 #
  10. # I am by no means a Linux guy or a Asterisk #
  11. # guy. Without Sanjay Willie's work I could #
  12. # not have done this. #
  13. # #
  14. #Contact:[email protected] #
  15. #added message ordering from #
  16. #http://www.irishvoip.com/w/knowledgebase.php?action=displayarticle&id=13 #
  17. ##############################################################################
  18.  
  19. #VARIABLES
  20. maxretry=10000 #Number of Atempts for sending the sms
  21. retryint=60 #Number of Seconds between Retries
  22. #CONSTANTS
  23. ERRORCODE=0
  24. d_unique=`date +%s`
  25. d_friendly=`date +%T_%D`
  26. astbin=`which asterisk`
  27. myrandom=$[ ( $RANDOM % 1000 ) + 1 ]
  28. #
  29.  
  30. function bail()
  31. {
  32. echo "SMS:[$ERRORCODE] $MSGOUT. Runtime:$d_friendly. UniqueCode:$d_unique"
  33. exit $ERRORCODE
  34. }
  35. function gencallfile(){
  36.  
  37. filename=$1
  38. destexten=$2
  39. source=$3
  40. dest=$4
  41. message=$5
  42. mydate=`date +%d%m%y`
  43. logdate=`date`
  44. #dest=echo $dest | grep -d
  45. #
  46. echo -e "Channel: Local/$destexten@app-fakeanswer
  47. CallerID: $source
  48. Maxretries: $maxretry
  49. RetryTime: $retryint
  50. Context: astsms
  51. Extension: $destexten
  52. Priority: 1
  53. Set: MESSAGE(body)=$message
  54. Set: MESSAGE(to)=$dest
  55. Set: MESSAGE(from)=$source
  56. Set: INQUEUE=1 "> /var/spool/asterisk/temp/$filename
  57.  
  58. # move files
  59. chown asterisk:asterisk /var/spool/asterisk/temp/$filename
  60. chmod 777 /var/spool/asterisk/temp/$filename
  61. sleep 3
  62. #
  63. # Check to see if there is already a message for this extension queued
  64. # if so then move to the hold folder and let the cron job astcron.sh check for delivery of the queued message
  65. # and only then deliver the hold messages. This will make sure the messages are delivered in order
  66. #
  67. ifexist=`ls /var/spool/asterisk/outgoing/*.call | grep -c $destexten`
  68. if [[ "$ifexist" == "0" ]]; then
  69. #
  70. # move file to outgoing folder
  71. #
  72. mv /var/spool/asterisk/temp/$filename /var/spool/asterisk/outgoing/
  73. else
  74. #
  75. # move file to hold folder
  76. #
  77. mv /var/spool/asterisk/temp/$filename /var/spool/asterisk/hold/
  78.  
  79. fi
  80. #
  81. #exit $ERRORCODE
  82. bail
  83. }
  84.  
  85. while test -n "$1"; do
  86. case "$1" in
  87. -SRC)
  88. source="$2"
  89. echo $source
  90. shift
  91. ;;
  92. -DST)
  93. dest="$2"
  94. echo $dest
  95. shift
  96. ;;
  97. -MSG)
  98. message="$2"
  99. echo $message
  100. shift
  101. ;;
  102. -TIME)
  103. originaltime="$2"
  104. echo $originaltime
  105. shift
  106. ;;
  107. esac
  108. shift
  109. done
  110.  
  111. #[checking for appropriate arguments]
  112. if [[ "$source" == "" ]]; then
  113. echo "ERROR: No source. Quitting."
  114. ERRORCODE=1
  115. bail
  116. fi
  117.  
  118. if [[ "$dest" == "" ]]; then
  119. echo "ERROR: No usable destination. Quitting."
  120. ERRORCODE=1
  121. bail
  122. fi
  123.  
  124. if [[ "$message" == "" ]]; then
  125. echo "ERROR: No message specified.Quitting."
  126. ERRORCODE=1
  127. bail
  128. fi
  129. #[End Argument checking]
  130.  
  131. # Check to see if extension exist
  132.  
  133. destexten=`echo $dest | cut -d\@ -f1 | cut -d\: -f2`
  134. ifexist=`$astbin -rx "sip show peers" | grep -c $destexten`
  135.  
  136. if [[ "$ifexist" == "0" ]]; then
  137. echo "Destination extension don't exist, exiting.."
  138. ERRORCODE=1
  139. baduser=$destexten
  140. destexten=`echo $source | cut -d\@ -f1 | cut -d\: -f2`
  141. temp=$source
  142. source=$dest
  143. dest=$temp
  144. message="The user $baduser does not exist, please try your message again using a different recipient.:("
  145. filename="$destexten-$d_unique.$myrandom.NoSuchUser.call"
  146. gencallfile "$filename" "$destexten" "$source" "$dest" "$message"
  147. bail
  148. fi
  149. #End of Check
  150.  
  151.  
  152. # If that conditions pass, then we will queue,
  153. # you can write other conditions too to keep the sanity of the looping
  154. destexten=`echo $dest | cut -d\@ -f1 | cut -d\: -f2`
  155. filename="$destexten-$d_unique.$myrandom.call"
  156. gencallfile "$filename" "$destexten" "$source" "$dest" "$message"
  157. bail
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement