Рефакторинг
This commit is contained in:
parent
f74ee0bcbd
commit
3a5b6b0d0a
12
pom.xml
12
pom.xml
@ -47,17 +47,9 @@
|
||||
<version>${bot.core.ver}</version>
|
||||
</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>
|
||||
|
||||
<developers>
|
||||
|
@ -1,22 +1,21 @@
|
||||
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.bot.core.domain.attachment.Attachment;
|
||||
import org.sadtech.bot.core.domain.attachment.AudioMessage;
|
||||
import org.sadtech.vkbot.core.service.distribution.MailService;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
public class MailSubscriber implements EventSubscribe<JsonObject> {
|
||||
@ -46,44 +45,6 @@ public class MailSubscriber implements EventSubscribe<JsonObject> {
|
||||
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<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 (eventDistributionMap.containsKey("chat")) {
|
||||
eventDistributionMap.get("chat").update(userMessage);
|
||||
@ -114,9 +75,19 @@ public class MailSubscriber implements EventSubscribe<JsonObject> {
|
||||
private Mail createMaail(Message message) {
|
||||
Mail mail = new Mail();
|
||||
mail.setMessage(message.getText());
|
||||
mail.setDate(message.getDate());
|
||||
mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId()));
|
||||
mail.setId(message.getId());
|
||||
mail.setPersonId(message.getPeerId());
|
||||
|
||||
List<Attachment> attachments = new ArrayList<>();
|
||||
for (MessageAttachment attachment : message.getAttachments()) {
|
||||
if (MessageAttachmentType.AUDIO_MESSAGE.equals(attachment.getType())) {
|
||||
AudioMessage audioMessage = new AudioMessage();
|
||||
audioMessage.setLinkOdd(attachment.getAudioMessage().getLinkOgg());
|
||||
attachments.add(audioMessage);
|
||||
}
|
||||
}
|
||||
mail.setAttachments(attachments);
|
||||
return mail;
|
||||
}
|
||||
|
||||
|
@ -5,21 +5,16 @@ import com.vk.api.sdk.client.VkApiClient;
|
||||
import com.vk.api.sdk.client.actors.GroupActor;
|
||||
import com.vk.api.sdk.exceptions.ApiException;
|
||||
import com.vk.api.sdk.exceptions.ClientException;
|
||||
import com.vk.api.sdk.objects.messages.*;
|
||||
import com.vk.api.sdk.objects.messages.Keyboard;
|
||||
import com.vk.api.sdk.queries.messages.MessagesSendQuery;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.sadtech.bot.core.domain.BoxAnswer;
|
||||
import org.sadtech.bot.core.domain.keyboard.ButtonColor;
|
||||
import org.sadtech.bot.core.domain.keyboard.KeyBoard;
|
||||
import org.sadtech.bot.core.domain.keyboard.KeyBoardButton;
|
||||
import org.sadtech.bot.core.domain.keyboard.KeyBoardLine;
|
||||
import org.sadtech.bot.core.sender.Sent;
|
||||
import org.sadtech.vkbot.core.VkConnect;
|
||||
import org.sadtech.vkbot.core.VkInsertData;
|
||||
import org.sadtech.vkbot.core.convert.KeyBoardConvert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class MailSenderVk implements Sent {
|
||||
@ -49,7 +44,6 @@ public class MailSenderVk implements Sent {
|
||||
sendMessage(messagesSendQuery);
|
||||
}
|
||||
|
||||
//FIXME: Пофиксить клавиатуры
|
||||
private MessagesSendQuery createMessage(BoxAnswer boxAnswer, Integer peerId) {
|
||||
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(peerId).message(vkInsertData.insertWords(boxAnswer.getMessage(), peerId)).randomId(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE));
|
||||
if (boxAnswer.getKeyboard() != null) {
|
||||
@ -57,6 +51,7 @@ public class MailSenderVk implements Sent {
|
||||
} else {
|
||||
Keyboard keyboard = new Keyboard();
|
||||
keyboard.setOneTime(true);
|
||||
keyboard.setButtons(Collections.EMPTY_LIST);
|
||||
messages.keyboard(keyboard);
|
||||
}
|
||||
if (boxAnswer.getLat() != null && boxAnswer.getaLong() != null) {
|
||||
|
@ -6,6 +6,7 @@ import org.sadtech.bot.core.repository.MailRepository;
|
||||
import org.sadtech.bot.core.repository.impl.MailRepositoryList;
|
||||
import org.sadtech.vkbot.core.service.distribution.MailService;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -33,7 +34,7 @@ public class MailServiceImpl implements MailService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Mail> getFirstEventByTime(Integer timeFrom, Integer timeTo) {
|
||||
public List<Mail> getFirstEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
|
||||
log.info("Запрошены сообщения " + timeFrom + " - " + timeTo);
|
||||
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
|
||||
Set<Integer> people = new HashSet<>();
|
||||
@ -48,7 +49,7 @@ public class MailServiceImpl implements MailService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Mail> getLastEventByTime(Integer timeFrom, Integer timeTo) {
|
||||
public List<Mail> getLastEventByTime(LocalDateTime timeFrom, LocalDateTime timeTo) {
|
||||
List<Mail> mails = mailRepository.getMailByTime(timeFrom, timeTo);
|
||||
Set<Integer> people = new HashSet<>();
|
||||
List<Mail> returnMails = new ArrayList<>();
|
||||
@ -62,7 +63,7 @@ public class MailServiceImpl implements MailService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Mail> getEvent(Integer timeFrom, Integer timeTo) {
|
||||
public List<Mail> getEvent(LocalDateTime timeFrom, LocalDateTime timeTo) {
|
||||
log.info("Запрос на получение сообщений в интервале от " + timeFrom + " до " + timeTo);
|
||||
return mailRepository.getMailByTime(timeFrom, timeTo);
|
||||
}
|
||||
|
Reference in New Issue
Block a user