From 9026f500208f9a4175304809ccabcbffbf1bca1f Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Tue, 14 May 2019 01:33:47 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D1=83=20=D0=B3?= =?UTF-8?q?=D0=BE=D0=BB=D0=BE=D1=81=D0=BE=D0=B2=D1=8B=D1=85=20=D1=81=D0=BE?= =?UTF-8?q?=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 12 ++++ .../core/distribution/MailSubscriber.java | 65 +++++++++++++++++-- 2 files changed, 73 insertions(+), 4 deletions(-) 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());