* Изменена логика KeyBoardService. Теперь все методы статичные
* Добавлен новый класс для вставки слов из List в строку * Изменена стругтура Unit. Теперь они не хранят MailSend, они хранят BoxAnswer, который может быть преобразован в MailSend (возможно стоит вернуть MailSend) * Добавленны методы, которые позваляют преобразовать BoxAnswer в MailSend * Добавлен функциональный интерфейс
This commit is contained in:
parent
d727e37bd8
commit
ac50183423
36
src/main/java/org/sadtech/vkbot/core/insert/InsertWords.java
Normal file
36
src/main/java/org/sadtech/vkbot/core/insert/InsertWords.java
Normal file
@ -0,0 +1,36 @@
|
||||
package org.sadtech.vkbot.core.insert;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class InsertWords {
|
||||
|
||||
private String inText;
|
||||
private String outText;
|
||||
|
||||
public void insert(List<String> words) {
|
||||
Pattern pattern = Pattern.compile("\\{(\\d+)}"); // Задаем шаблон
|
||||
Matcher m = pattern.matcher(inText); // Инициализация Matcher
|
||||
StringBuffer result = new StringBuffer(); // Буфер для конечного значения
|
||||
while (m.find()) { // Проверка на совпадение
|
||||
if (Integer.parseInt(m.group(1)) < words.size()) {
|
||||
m.appendReplacement(result, words.get(Integer.parseInt(m.group(1)))); // Подставляем значение из HashMap
|
||||
} else {
|
||||
m.appendReplacement(result, m.group(0));
|
||||
}
|
||||
}
|
||||
m.appendTail(result); // Добавить остаток строки
|
||||
outText = result.toString();
|
||||
}
|
||||
|
||||
public void setInText(String inText) {
|
||||
this.inText = inText;
|
||||
}
|
||||
|
||||
public String getOutText() {
|
||||
return outText;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,9 +4,7 @@ import java.util.List;
|
||||
|
||||
public class KeyBoardService {
|
||||
|
||||
private KeyBoard keyBoardYesNo = generateKeyBoardYesNo();
|
||||
|
||||
private KeyBoard generateKeyBoardYesNo() {
|
||||
public static KeyBoard keyBoardYesNo() {
|
||||
ButtonKeyBoard yesButton = ButtonKeyBoard.builder().setColor(ColorButton.POSITIVE).setLabel("Да").setPayload("{\"button\": \"yes\"}").build();
|
||||
ButtonKeyBoard noButton = ButtonKeyBoard.builder().setColor(ColorButton.NEGATIVE).setLabel("Нет").setPayload("{\"button\": \"no\"}").build();
|
||||
LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build();
|
||||
@ -14,7 +12,7 @@ public class KeyBoardService {
|
||||
return keyBoard;
|
||||
}
|
||||
|
||||
public static KeyBoard verticalMenu(List<String> labelButtons) {
|
||||
public static KeyBoard verticalMenuString(List<String> labelButtons) {
|
||||
KeyBoard keyBoard = new KeyBoard();
|
||||
for (String labelButton : labelButtons) {
|
||||
ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().setLabel(labelButton).setType("text").setPayload("{\"button\": \"" + labelButton + "\"}").build();
|
||||
@ -23,7 +21,11 @@ public class KeyBoardService {
|
||||
return keyBoard;
|
||||
}
|
||||
|
||||
public KeyBoard getKeyBoardYesNo() {
|
||||
return keyBoardYesNo;
|
||||
public static KeyBoard verticalMenuButton(List<ButtonKeyBoard> buttonKeyBoards) {
|
||||
KeyBoard keyBoard = new KeyBoard();
|
||||
for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) {
|
||||
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
|
||||
}
|
||||
return keyBoard;
|
||||
}
|
||||
}
|
||||
|
@ -5,10 +5,13 @@ 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.queries.messages.MessagesSendQuery;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.sadtech.vkbot.core.VkConnect;
|
||||
|
||||
public class MailSanderVk implements MailSandler {
|
||||
|
||||
public static final Logger log = Logger.getLogger(MailSanderVk.class);
|
||||
|
||||
private VkApiClient vkApiClient;
|
||||
private GroupActor groupActor;
|
||||
|
||||
@ -16,6 +19,7 @@ public class MailSanderVk implements MailSandler {
|
||||
this.vkApiClient = vkConnect.getVkApiClient();
|
||||
this.groupActor = vkConnect.getGroupActor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(MailSend mailSend) {
|
||||
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient());
|
||||
@ -39,6 +43,7 @@ public class MailSanderVk implements MailSandler {
|
||||
}
|
||||
|
||||
try {
|
||||
log.info(mailSend);
|
||||
messages.execute();
|
||||
} catch (ApiException | ClientException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -60,4 +60,16 @@ public class MailSend {
|
||||
public void setStickerId(Integer stickerId) {
|
||||
this.stickerId = stickerId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MailSend{" +
|
||||
"message='" + message + '\'' +
|
||||
", keyboard='" + keyboard + '\'' +
|
||||
", idRecipient=" + idRecipient +
|
||||
", lat=" + lat +
|
||||
", aLong=" + aLong +
|
||||
", stickerId=" + stickerId +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user