mikeisfly

astqueue.sh modified for freepbx

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