sendmail-gcloud

From Sergio Lobera, 10 Months ago, written in Plain Text, viewed 3 times.
URL https://pastebin.freepbx.org/view/f532b036 Embed
Download Paste or View Raw
  1. #!/bin/sh
  2.  
  3. # sendmail-gcloud
  4. #
  5. # Installation instructions
  6. # Copy the content of this file to /usr/sbin/sendmail-gcloud
  7. #
  8. # Google Account
  9. # ---------------
  10. # Create an account, if you don't have yet, with Google Cloud. Free trials can be made at https://console.cloud.google.com/freetrial
  11. # Within console.cloud.google.com search for Cloud Speech-to-Text API and enable it
  12. #
  13. # From the Linux command line on the FreePBX machine
  14. # -------------------------------------------
  15. # Follow steps 1 and 2 of the instructions on Google Cloud https://cloud.google.com/sdk/docs/downloads-yum
  16. # Run the following commands on FreePBX;
  17. #    cd /usr/sbin/
  18. #    chown asterisk:asterisk sendmail-gcloud
  19. #    chmod 744 sendmail-gcloud
  20. #    chmod 777 /usr/bin/dos2unix
  21. #
  22. # Verify that you have the following (by simply running the command) and if not use yum install;
  23. #    jq
  24. #    sox
  25. #    flac
  26. #    dos2unix -V
  27. # Ensure dos2unix is executable by the asterisk user (chmod 777 /usr/bin/dos2unix)
  28. #
  29. # Connect FreePBX to Google Cloud
  30. #     su asterisk
  31. #     gcloud auth login
  32. # CLI will provide you a url. Copy that and paste it into your browser. Google will give you a verification code to copy. Paste it into the cli waiting for a verification code.
  33. #
  34. # Open FreePBX web interface
  35. #     Go to Settings > Voicemail Admin > Settings > Email Config
  36. #     Change Mail Command to: /usr/sbin/sendmail-gcloud
  37. #     Submit and apply changes
  38. #
  39. # Original source created by N. Bernaerts: https://github.com/NicolasBernaerts/debian-scripts/tree/master/asterisk
  40. # modified per: https://jrklein.com/2015/08/17/asterisk-voicemail-transcription-via-ibm-bluemix-speech-to-text-api/
  41. # modified per: https://gist.github.com/lgaetz/2cd9c54fb1714e0d509f5f8215b3f5e6
  42. # current version: https://gist.github.com/tony722/7c6d86be2e74fa10a1f344a4c2b093ea
  43. #
  44. # Notes: This is a script modified from the original to work with a FreePBX Distro PBX so that email notifications sent from
  45. #        Asterisk voicemail contain a a speech to text transcription provided by Google Cloud Speech API
  46. #
  47. # License: There are no explicit license terms on the original script or on the blog post with modifications
  48. #          I'm assumig GNU/GPL2+ unless notified otherwise by copyright holder(s)
  49. #
  50. # Version History:
  51. # 2020-05-27  Add instructions from sr10952
  52. #             Add export fix by levishores
  53. # 2019-02-27  Initial commit by tony722
  54.  
  55. # set PATH
  56. PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  57.  
  58. # save the current directory
  59. pushd .
  60.  
  61. # create a temporary directory and cd to it
  62. TMPDIR=$(mktemp -d)
  63. cd $TMPDIR
  64.  
  65. # dump the stream to a temporary file
  66. cat >> stream.org
  67.  
  68. # get the boundary
  69. BOUNDARY=$(grep "boundary=" stream.org | cut -d'"' -f 2)
  70.  
  71. # if mail has no boundaries, assume no attachment
  72. if [ "$BOUNDARY" = "" ]
  73.        
  74. then
  75.         # send the original stream
  76.         mv stream.org stream.new
  77. else
  78.         # cut the original stream into parts
  79.         # stream.part  - header before the boundary
  80.         # stream.part1 - header after the bounday
  81.         # stream.part2 - body of the message
  82.         # stream.part3 - attachment in base64 (WAV file)
  83.         # stream.part4 - footer of the message
  84.         awk '/'$BOUNDARY'/{i++}{print > "stream.part"i}' stream.org
  85.  
  86.         # cut the attachment into parts
  87.         # stream.part3.head - header of attachment
  88.         # stream.part3.wav.base64 - wav file of attachment (encoded base64)
  89.         sed '7,$d' stream.part3 > stream.part3.wav.head
  90.         sed '1,6d' stream.part3 > stream.part3.wav.base64
  91.  
  92.         # convert the base64 file to a wav file
  93.         dos2unix -o stream.part3.wav.base64
  94.         base64 -di stream.part3.wav.base64 > stream.part3.wav
  95.  
  96.         # convert the wav file to FLAC
  97.         sox -G stream.part3.wav --channels=1 --bits=16 --rate=8000 stream.part3.flac
  98.  
  99.         # convert to MP3
  100.         sox stream.part3.wav stream.part3-pcm.wav
  101.         lame -m m -b 24 stream.part3-pcm.wav stream.part3.mp3
  102.         base64 stream.part3.mp3 > stream.part3.mp3.base64
  103.  
  104.         # create mp3 mail part
  105.         sed 's/x-[wW][aA][vV]/mpeg/g' stream.part3.wav.head | sed 's/.[wW][aA][vV]/.mp3/g' > stream.part3.new
  106.         dos2unix -o stream.part3.new
  107.         unix2dos -o stream.part3.mp3.base64
  108.         cat stream.part3.mp3.base64 >> stream.part3.new
  109.  
  110.         # save voicemail in tmp folder in case of trouble
  111.         # TMPMP3=$(mktemp -u /tmp/msg_XXXXXXXX.mp3)
  112.         # cp "stream.part3.mp3" "$TMPMP3"
  113.  
  114.         export CLOUDSDK_CONFIG=/home/asterisk/.config/gcloud
  115.         RESULT=`gcloud ml speech recognize stream.part3.flac --language-code='en-US'`
  116.  
  117.         FILTERED=`echo "$RESULT" | jq -r '.results[].alternatives[].transcript'`
  118.                    
  119.         # generate first part of mail body, converting it to LF only
  120.         mv stream.part stream.new
  121.         cat stream.part1 >> stream.new
  122.         sed '$d' < stream.part2 >> stream.new
  123.  
  124.         # beginning of transcription section
  125.         echo "" >> stream.new
  126.         echo "--- Google transcription result ---" >> stream.new
  127.  
  128.         # append result of transcription
  129.         if [ -z "$FILTERED"     ]
  130.         then
  131.           echo "(Google was unable to recognize any speech in audio data.)" >> stream.new
  132.         else
  133.           echo "$FILTERED" >> stream.new
  134.         fi
  135.  
  136.         # end of message body
  137.         tail -1 stream.part2 >> stream.new
  138.  
  139.         # add converted attachment  
  140.         cat stream.part3.new >> stream.new
  141.  
  142.         # append end of mail body, converting it to LF only
  143.         echo "" >> stream.tmp
  144.         echo "" >> stream.tmp
  145.         cat stream.part4 >> stream.tmp
  146.         dos2unix -o stream.tmp
  147.         cat stream.tmp >> stream.new
  148.  
  149. fi
  150.  
  151. # send the mail thru sendmail
  152. cat stream.new | sendmail -t
  153.  
  154. # go back to original directory
  155. popd
  156.  
  157. # remove all temporary files and temporary directory
  158. rm -Rf $TMPDIR

Reply to "sendmail-gcloud"

Here you can reply to the paste above