asterisk-scripts/vmsendstt.php

133 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2022-05-13 12:46:54 -06:00
#!/usr/bin/php7.4
<?php
/*
Asterisk Voicemail Transcribe Wrapper
Version: 0.7.0
Date: 5/20/2022
2022-05-13 12:46:54 -06:00
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
2022-05-16 12:35:43 -06:00
Tested With: PHP 7.4
2022-05-13 12:46:54 -06:00
Written by: Brie Bruns <bruns@2mbit.com>
Uses IBM's Watson Speech to Text API interface. Not pretty with
minimal error checking, but it works. Feel free to send me patches
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";
2022-05-13 12:46:54 -06:00
$emailRaw = stream_get_contents(STDIN);
require_once ("Mail.php");
require_once ("Mail/mime.php");
$mail = mailparse_msg_create();
mailparse_msg_parse($mail, $emailRaw);
$mailData=mailparse_msg_get_part_data($mail);
2022-05-13 12:46:54 -06:00
$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);
}
}
2022-05-13 12:46:54 -06:00
$textMsg = mailparse_msg_extract_part($textPart, $emailRaw, null);
2022-05-13 12:46:54 -06:00
if (isset($wavFile)) {
$textMsg .= "\n\nSpeech To Text (May be inaccurate!):\n\n";
2022-05-13 12:46:54 -06:00
$submitWatsonSTT=curl_init();
curl_setopt_array($submitWatsonSTT, array(
CURLOPT_CONNECTTIMEOUT => '15',
CURLOPT_TIMEOUT => '120',
2022-05-16 12:35:43 -06:00
CURLOPT_URL => $apiURL.$apiURLRecognize,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_CUSTOMREQUEST => 'POST',
2022-05-13 12:46:54 -06:00
CURLOPT_USERPWD => 'apikey:'.$apiKey,
CURLOPT_HTTPHEADER => array( 'Content-Type: audio/wav' ),
CURLOPT_POSTFIELDS => $wavFile
)
);
$curlReturnResults=curl_exec($submitWatsonSTT);
$transcriptResults = json_decode($curlReturnResults, true);
if (isset($transcriptResults['results'][0]['alternatives'][0]['transcript'])) {
foreach($transcriptResults['results'] as $key => $value) {
$textMsg .= $transcriptResults['results'][$key]['alternatives'][0]['transcript']." ... ";
}
$textMsg .="\n\n";
}
}
$headers = array(
"Date" => $mailData['headers']['date'],
"From" => $mailData['headers']['from'],
"To" => $mailData['headers']['to'],
"Subject" => $mailData['headers']['subject'],
"Message-ID" => $mailData['headers']['message-id'],
2022-05-13 12:51:30 -06:00
"X-Asterisk-CallerID" => $mailData['headers']['x-asterisk-callerid'],
"X-Asterisk-CallerIDName" => $mailData['headers']['x-asterisk-calleridname'],
2022-05-13 12:46:54 -06:00
);
2022-05-18 11:05:41 -06:00
if (isset($mailData['headers']['cc'])) {
2022-05-16 12:35:43 -06:00
$headers['Cc'] = $mailData['headers']['cc'];
}
2022-05-18 11:05:41 -06:00
if (isset($mailData['headers']['bcc'])) {
2022-05-16 12:35:43 -06:00
$headers['Bcc'] = $mailData['headers']['bcc'];
}
2022-05-18 11:05:41 -06:00
if (isset($mailData['headers']['reply-to'])) {
2022-05-16 12:35:43 -06:00
$headers['Reply-To'] = $mailData['headers']['reply-to'];
}
2022-05-18 11:05:41 -06:00
if (isset($mailData['headers']['user-agent'])) {
2022-05-16 12:35:43 -06:00
$headers['User-Agent'] = $mailData['headers']['user-agent'];
}
2022-05-18 11:05:41 -06:00
if (isset($mailData['headers']['content-language'])) {
2022-05-16 12:35:43 -06:00
$headers['Content-Language'] = $mailData['headers']['content-language'];
}
2022-05-13 12:46:54 -06:00
$sendMimeMail = new Mail_mime();
$sendMimeMail->setTXTBody($textMsg);
if (isset($wavFile)) {
$sendMimeMail->addAttachment(
$wavFile,
$mimePartHeader['content-type'],
$mimePartHeader['content-name'],
false,
'base64',
'attachment',
null,
null,
null,
null,
null,
$mimePartHeader['content-description'],
null
);
}
2022-05-13 12:46:54 -06:00
$mailBody = $sendMimeMail->get();
$mailHeaders = $sendMimeMail->headers($headers);
$sendMail = Mail::factory("sendmail");
$sendMail->send($mailData['headers']['to'], $mailHeaders, $mailBody);
mailparse_msg_free($mail);
2022-05-16 12:35:43 -06:00
?>