Добавил поддержку голосовых сообщений

This commit is contained in:
Mark Struchkov 2019-05-14 01:33:47 +03:00
parent 0a960d6972
commit 9026f50020
2 changed files with 73 additions and 4 deletions

12
pom.xml
View File

@ -46,6 +46,18 @@
<artifactId>bot-core</artifactId> <artifactId>bot-core</artifactId>
<version>${bot.core.ver}</version> <version>${bot.core.ver}</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-speech</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.73.0</version>
</dependency>
</dependencies> </dependencies>
<developers> <developers>

View File

@ -1,16 +1,23 @@
package org.sadtech.vkbot.core.distribution; package org.sadtech.vkbot.core.distribution;
import com.google.cloud.speech.v1.*;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonObject; 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.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.apache.log4j.Logger;
import org.sadtech.bot.core.domain.Mail; import org.sadtech.bot.core.domain.Mail;
import org.sadtech.vkbot.core.service.distribution.MailService; import org.sadtech.vkbot.core.service.distribution.MailService;
import java.util.HashMap; import java.io.*;
import java.util.HashSet; import java.nio.file.Files;
import java.util.Map; import java.nio.file.Path;
import java.util.Set; import java.nio.file.Paths;
import java.util.*;
public class MailSubscriber implements EventSubscribe<JsonObject> { public class MailSubscriber implements EventSubscribe<JsonObject> {
@ -39,6 +46,44 @@ public class MailSubscriber implements EventSubscribe<JsonObject> {
Message userMessage = gson.fromJson(object, Message.class); Message userMessage = gson.fromJson(object, Message.class);
log.info(userMessage); 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<SpeechRecognitionResult> 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 (userMessage.getPeerId() > 2000000000) {
if (eventDistributionMap.containsKey("chat")) { if (eventDistributionMap.containsKey("chat")) {
eventDistributionMap.get("chat").update(userMessage); eventDistributionMap.get("chat").update(userMessage);
@ -54,6 +99,18 @@ public class MailSubscriber implements EventSubscribe<JsonObject> {
} }
} }
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) { private Mail createMaail(Message message) {
Mail mail = new Mail(); Mail mail = new Mail();
mail.setMessage(message.getText()); mail.setMessage(message.getText());