* Изменена логика 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 {
|
public class KeyBoardService {
|
||||||
|
|
||||||
private KeyBoard keyBoardYesNo = generateKeyBoardYesNo();
|
public static KeyBoard keyBoardYesNo() {
|
||||||
|
|
||||||
private KeyBoard generateKeyBoardYesNo() {
|
|
||||||
ButtonKeyBoard yesButton = ButtonKeyBoard.builder().setColor(ColorButton.POSITIVE).setLabel("Да").setPayload("{\"button\": \"yes\"}").build();
|
ButtonKeyBoard yesButton = ButtonKeyBoard.builder().setColor(ColorButton.POSITIVE).setLabel("Да").setPayload("{\"button\": \"yes\"}").build();
|
||||||
ButtonKeyBoard noButton = ButtonKeyBoard.builder().setColor(ColorButton.NEGATIVE).setLabel("Нет").setPayload("{\"button\": \"no\"}").build();
|
ButtonKeyBoard noButton = ButtonKeyBoard.builder().setColor(ColorButton.NEGATIVE).setLabel("Нет").setPayload("{\"button\": \"no\"}").build();
|
||||||
LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build();
|
LineKeyBoard lineKeyBoard = LineKeyBoard.builder().setButtonKeyBoard(yesButton).setButtonKeyBoard(noButton).build();
|
||||||
@ -14,7 +12,7 @@ public class KeyBoardService {
|
|||||||
return keyBoard;
|
return keyBoard;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static KeyBoard verticalMenu(List<String> labelButtons) {
|
public static KeyBoard verticalMenuString(List<String> labelButtons) {
|
||||||
KeyBoard keyBoard = new KeyBoard();
|
KeyBoard keyBoard = new KeyBoard();
|
||||||
for (String labelButton : labelButtons) {
|
for (String labelButton : labelButtons) {
|
||||||
ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().setLabel(labelButton).setType("text").setPayload("{\"button\": \"" + labelButton + "\"}").build();
|
ButtonKeyBoard buttonKeyBoard = ButtonKeyBoard.builder().setLabel(labelButton).setType("text").setPayload("{\"button\": \"" + labelButton + "\"}").build();
|
||||||
@ -23,7 +21,11 @@ public class KeyBoardService {
|
|||||||
return keyBoard;
|
return keyBoard;
|
||||||
}
|
}
|
||||||
|
|
||||||
public KeyBoard getKeyBoardYesNo() {
|
public static KeyBoard verticalMenuButton(List<ButtonKeyBoard> buttonKeyBoards) {
|
||||||
return keyBoardYesNo;
|
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.ApiException;
|
||||||
import com.vk.api.sdk.exceptions.ClientException;
|
import com.vk.api.sdk.exceptions.ClientException;
|
||||||
import com.vk.api.sdk.queries.messages.MessagesSendQuery;
|
import com.vk.api.sdk.queries.messages.MessagesSendQuery;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
import org.sadtech.vkbot.core.VkConnect;
|
import org.sadtech.vkbot.core.VkConnect;
|
||||||
|
|
||||||
public class MailSanderVk implements MailSandler {
|
public class MailSanderVk implements MailSandler {
|
||||||
|
|
||||||
|
public static final Logger log = Logger.getLogger(MailSanderVk.class);
|
||||||
|
|
||||||
private VkApiClient vkApiClient;
|
private VkApiClient vkApiClient;
|
||||||
private GroupActor groupActor;
|
private GroupActor groupActor;
|
||||||
|
|
||||||
@ -16,21 +19,22 @@ public class MailSanderVk implements MailSandler {
|
|||||||
this.vkApiClient = vkConnect.getVkApiClient();
|
this.vkApiClient = vkConnect.getVkApiClient();
|
||||||
this.groupActor = vkConnect.getGroupActor();
|
this.groupActor = vkConnect.getGroupActor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void send(MailSend mailSend) {
|
public void send(MailSend mailSend) {
|
||||||
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient());
|
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient());
|
||||||
if (mailSend.getMessage()!=null) {
|
if (mailSend.getMessage() != null) {
|
||||||
messages.message(mailSend.getMessage());
|
messages.message(mailSend.getMessage());
|
||||||
}
|
}
|
||||||
if (mailSend.getKeyboard()!=null) {
|
if (mailSend.getKeyboard() != null) {
|
||||||
messages.keyboard(mailSend.getKeyboard());
|
messages.keyboard(mailSend.getKeyboard());
|
||||||
} else {
|
} else {
|
||||||
messages.keyboard("{\"buttons\":[],\"one_time\":true}");
|
messages.keyboard("{\"buttons\":[],\"one_time\":true}");
|
||||||
}
|
}
|
||||||
if (mailSend.getLat()!=null && mailSend.getaLong()!=null) {
|
if (mailSend.getLat() != null && mailSend.getaLong() != null) {
|
||||||
messages.lat(mailSend.getLat()).lng(mailSend.getaLong());
|
messages.lat(mailSend.getLat()).lng(mailSend.getaLong());
|
||||||
}
|
}
|
||||||
if (mailSend.getStickerId()!=null) {
|
if (mailSend.getStickerId() != null) {
|
||||||
try {
|
try {
|
||||||
vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient()).stickerId(mailSend.getStickerId()).execute();
|
vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient()).stickerId(mailSend.getStickerId()).execute();
|
||||||
} catch (ApiException | ClientException e) {
|
} catch (ApiException | ClientException e) {
|
||||||
@ -39,6 +43,7 @@ public class MailSanderVk implements MailSandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
log.info(mailSend);
|
||||||
messages.execute();
|
messages.execute();
|
||||||
} catch (ApiException | ClientException e) {
|
} catch (ApiException | ClientException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -60,4 +60,16 @@ public class MailSend {
|
|||||||
public void setStickerId(Integer stickerId) {
|
public void setStickerId(Integer stickerId) {
|
||||||
this.stickerId = 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