diff --git a/pom.xml b/pom.xml
index fbb1acf..8be0fe9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -46,6 +46,18 @@
bot-core
${bot.core.ver}
+
+
+ com.google.cloud
+ google-cloud-speech
+ 1.3.0
+
+
+
+ com.google.cloud
+ google-cloud-storage
+ 1.73.0
+
diff --git a/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java b/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java
index 1681cd6..fda8c42 100644
--- a/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java
+++ b/src/main/java/org/sadtech/vkbot/core/distribution/MailSubscriber.java
@@ -1,16 +1,23 @@
package org.sadtech.vkbot.core.distribution;
+import com.google.cloud.speech.v1.*;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
+import com.google.protobuf.ByteString;
import com.vk.api.sdk.objects.messages.Message;
+import com.vk.api.sdk.objects.messages.MessageAttachment;
+import com.vk.api.sdk.objects.messages.MessageAttachmentType;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.ArrayUtils;
import org.apache.log4j.Logger;
import org.sadtech.bot.core.domain.Mail;
import org.sadtech.vkbot.core.service.distribution.MailService;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.*;
public class MailSubscriber implements EventSubscribe {
@@ -39,6 +46,44 @@ public class MailSubscriber implements EventSubscribe {
Message userMessage = gson.fromJson(object, Message.class);
log.info(userMessage);
+ if (userMessage.getAttachments()!=null) {
+ for (MessageAttachment attachment : userMessage.getAttachments()) {
+ if (MessageAttachmentType.AUDIO_MESSAGE.equals(attachment.getType())) {
+ try (SpeechClient speechClient = SpeechClient.create()) {
+
+ byte[] data = IOUtils.toByteArray(userMessage.getAttachments().get(0).getAudioMessage().getLinkOgg().openStream());
+ ByteString audioBytes = ByteString.copyFrom(data);
+
+ // Builds the sync recognize request
+ RecognitionConfig config = RecognitionConfig.newBuilder()
+ .setEncoding(RecognitionConfig.AudioEncoding.OGG_OPUS)
+ .setSampleRateHertz(16000)
+ .setLanguageCode("ru-RU")
+ .build();
+ RecognitionAudio audio = RecognitionAudio.newBuilder()
+ .setContent(audioBytes)
+ .build();
+
+ // Performs speech recognition on the audio file
+ RecognizeResponse response = speechClient.recognize(config, audio);
+ List results = response.getResultsList();
+
+ for (SpeechRecognitionResult result : results) {
+ // There can be several alternative transcripts for a given chunk of speech. Just use the
+ // first (most likely) one here.
+ SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
+ userMessage.setText(alternative.getTranscript());
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+
+
+
if (userMessage.getPeerId() > 2000000000) {
if (eventDistributionMap.containsKey("chat")) {
eventDistributionMap.get("chat").update(userMessage);
@@ -54,6 +99,18 @@ public class MailSubscriber implements EventSubscribe {
}
}
+ public byte[] getBytes(InputStream inputStream) throws IOException {
+ ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
+ int bufferSize = 1024;
+ byte[] buffer = new byte[bufferSize];
+
+ int len = 0;
+ while ((len = inputStream.read(buffer)) != -1) {
+ byteBuffer.write(buffer, 0, len);
+ }
+ return byteBuffer.toByteArray();
+ }
+
private Mail createMaail(Message message) {
Mail mail = new Mail();
mail.setMessage(message.getText());