#!/bin/bash # sendmail-apenai # # Installation instructions # https://github.com/VitalPBX/vitalpbx-voicemail-transcription-openai/blob/main/README.md # # Special thanks to: # Original source created by N. Bernaerts: https://github.com/NicolasBernaerts/debian-scripts/tree/master/asterisk # modified per: https://jrklein.com/2015/08/17/asterisk-voicemail-transcription-via-ibm-bluemix-speech-to-text-api/ # modified per: https://gist.github.com/lgaetz/2cd9c54fb1714e0d509f5f8215b3f5e6 # modified per: https://gist.github.com/tony722/7c6d86be2e74fa10a1f344a4c2b093ea # # License: There are no explicit license terms on the original script or on the blog post with modifications # I'm assumig GNU/GPL2+ unless notified otherwise by copyright holder(s) # Version History: # 2023-05-04 Adapts to work with VitalPBX 4 and OpenAI # API_KEY="" API_URL="https://api.openai.com/v1/audio/transcriptions" # set PATH PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" # save the current directory pushd . # create a temporary directory and cd to it TMPDIR=$(mktemp -d) cd $TMPDIR # dump the stream to a temporary file cat >> stream.org # get the boundary BOUNDARY=$(grep "boundary=" stream.org | cut -d'"' -f 2) # if mail has no boundaries, assume no attachment if [ "$BOUNDARY" = "" ] then # send the original stream mv stream.org stream.new else # cut the original stream into parts # stream.part - header before the boundary # stream.part1 - header after the bounday # stream.part2 - body of the message # stream.part3 - attachment in base64 (WAV file) # stream.part4 - footer of the message awk '/'$BOUNDARY'/{i++}{print > "stream.part"i}' stream.org # cut the attachment into parts # stream.part3.head - header of attachment # stream.part3.wav.base64 - wav file of attachment (encoded base64) sed '7,$d' stream.part3 > stream.part3.wav.head sed '1,6d' stream.part3 > stream.part3.wav.base64 # convert the base64 file to a wav file dos2unix -o stream.part3.wav.base64 base64 -di stream.part3.wav.base64 > stream.part3.wav # convert to MP3 sox stream.part3.wav stream.part3-pcm.wav lame -m m -b 24 stream.part3-pcm.wav stream.part3.mp3 base64 stream.part3.mp3 > stream.part3.mp3.base64 # create mp3 mail part sed 's/x-[wW][aA][vV]/mpeg/g' stream.part3.wav.head | sed 's/.[wW][aA][vV]/.mp3/g' > stream.part3.new dos2unix -o stream.part3.new unix2dos -o stream.part3.mp3.base64 cat stream.part3.mp3.base64 >> stream.part3.new # convert audio to text and get result RESULT=`curl --request POST --url $API_URL --header "Authorization: Bearer $API_KEY" --header 'Content-Type: multipart/form-data' --form file=@stream.part3.mp3 --form response_format=text --form model=whisper-1` # generate first part of mail body, converting it to LF only mv stream.part stream.new cat stream.part1 >> stream.new cat stream.part2 >> stream.new # beginning of transcription section echo "" >> stream.new echo "--- OpenAI Transcription ---" >> stream.new # append result of transcription if [ -z "$RESULT" ] then echo "--- OpenAI was unable to recognize any speech in audio data ---" >> stream.new else echo "$RESULT" >> stream.new fi # add converted attachment cat stream.part3.new >> stream.new # append end of mail body, converting it to LF only echo "" >> stream.tmp echo "" >> stream.tmp cat stream.part4 >> stream.tmp dos2unix -o stream.tmp cat stream.tmp >> stream.new fi # send the mail thru sendmail cat stream.new | sendmail -t # go back to original directory popd # remove all temporary files and temporary directory rm -Rf $TMPDIR