From e8f90dc8b2ff0cbfa9086a49d143f6c15f3ad6d0 Mon Sep 17 00:00:00 2001 From: Brielle Date: Fri, 20 May 2022 10:38:02 -0600 Subject: [PATCH] Some minor error checking, needs way more work, but I'm a little happier with its response to things not being like it expects --- vmsendstt.php | 60 ++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/vmsendstt.php b/vmsendstt.php index 78dd8c5..3d9b6fe 100755 --- a/vmsendstt.php +++ b/vmsendstt.php @@ -4,8 +4,8 @@ /* Asterisk Voicemail Transcribe Wrapper -Version: 0.6.1 -Date: 5/18/2022 +Version: 0.7.0 +Date: 5/20/2022 License: This work is licensed under CC BY-SA 4.0 URL: https://git.sosdg.org/brielle/asterisk-scripts Requires: PHP 5 or 7, Mail and Mail_Mime libraries from Pear @@ -20,13 +20,12 @@ to improve. */ -$apiKey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; -$apiURL="https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/xxxxxxxxxxxxxxxxxxxxxxxxx"; -$apiURLRecognize="/v1/recognize?model=en-US_Telephony"; +$apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; +$apiURL = "https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/xxxxxxxxxxxxxxxxxxxxxxxxx"; +$apiURLRecognize = "/v1/recognize?model=en-US_Telephony"; $emailRaw = stream_get_contents(STDIN); - require_once ("Mail.php"); require_once ("Mail/mime.php"); @@ -34,16 +33,21 @@ $mail = mailparse_msg_create(); mailparse_msg_parse($mail, $emailRaw); $mailData=mailparse_msg_get_part_data($mail); -$textPart = mailparse_msg_get_part($mail, "1.1"); -$mimePart = mailparse_msg_get_part($mail, "1.2"); -$mimePartHeader = mailparse_msg_get_part_data($mimePart); -$wavFile = mailparse_msg_extract_part($mimePart, $emailRaw, null); +$textPart = mailparse_msg_get_part($mail, "1.1"); + +if (mailparse_msg_get_structure($mail)[2] == "1.2") { + $mimePart = mailparse_msg_get_part($mail, "1.2"); + if (mailparse_msg_get_part_data($mimePart)['content-type'] == "audio/x-wav") { + $mimePartHeader = mailparse_msg_get_part_data($mimePart); + $wavFile = mailparse_msg_extract_part($mimePart, $emailRaw, null); + } +} + $textMsg = mailparse_msg_extract_part($textPart, $emailRaw, null); -$textMsg .= "\n\nSpeech To Text (May be inaccurate!):\n\n"; - if (isset($wavFile)) { + $textMsg .= "\n\nSpeech To Text (May be inaccurate!):\n\n"; $submitWatsonSTT=curl_init(); curl_setopt_array($submitWatsonSTT, array( CURLOPT_CONNECTTIMEOUT => '15', @@ -100,21 +104,23 @@ if (isset($mailData['headers']['content-language'])) { $sendMimeMail = new Mail_mime(); $sendMimeMail->setTXTBody($textMsg); -$sendMimeMail->addAttachment( - $wavFile, - $mimePartHeader['content-type'], - $mimePartHeader['content-name'], - false, - 'base64', - 'attachment', - null, - null, - null, - null, - null, - $mimePartHeader['content-description'], - null -); +if (isset($wavFile)) { + $sendMimeMail->addAttachment( + $wavFile, + $mimePartHeader['content-type'], + $mimePartHeader['content-name'], + false, + 'base64', + 'attachment', + null, + null, + null, + null, + null, + $mimePartHeader['content-description'], + null + ); +} $mailBody = $sendMimeMail->get(); $mailHeaders = $sendMimeMail->headers($headers); $sendMail = Mail::factory("sendmail");