* Изменилась логика возвращения клавиатуры

* Добавил родителбскую реализацию юнита с клавиатурой, от нее будут наследоваться все остальные
* Добавил простую реализацию юнита для тестовых ответов
* Добавил новую реализацию юнита для сохранения ответов пользователя
* Добавил дефолтную реализацию Saver
* Переписал action, теперь для активации юнита еще отправляется Mail (нужно было для юнита, который сохраняет ответы пользователя)
This commit is contained in:
Mark Struchkov 2019-01-26 15:11:47 +03:00
parent 04062ba8b1
commit e0befdf62a
5 changed files with 39 additions and 19 deletions

View File

@ -33,7 +33,6 @@
<vksdk.ver>0.5.13-SNAPSHOT</vksdk.ver> <vksdk.ver>0.5.13-SNAPSHOT</vksdk.ver>
<log4j.ver>1.2.17</log4j.ver> <log4j.ver>1.2.17</log4j.ver>
<json.ver>20180813</json.ver> <json.ver>20180813</json.ver>
<autoresponder.ver>1.1.2-SNAPHOT</autoresponder.ver>
</properties> </properties>
<dependencies> <dependencies>
@ -57,11 +56,6 @@
<artifactId>json</artifactId> <artifactId>json</artifactId>
<version>${json.ver}</version> <version>${json.ver}</version>
</dependency> </dependency>
<dependency>
<groupId>org.sadtech.autoresponder</groupId>
<artifactId>autoresponder</artifactId>
<version>${autoresponder.ver}</version>
</dependency>
</dependencies> </dependencies>

View File

@ -9,18 +9,24 @@ import java.util.List;
public class KeyBoard { public class KeyBoard {
private List<LineKeyBoard> lineKeyBoards = new ArrayList<>(); private List<LineKeyBoard> lineKeyBoards = new ArrayList<>();
private boolean oneTime;
public KeyBoard() { public KeyBoard() {
} }
public KeyBoard(List<LineKeyBoard> lineKeyBoards) { public KeyBoard(List<LineKeyBoard> lineKeyBoards, boolean oneTime) {
this.lineKeyBoards = lineKeyBoards; this.lineKeyBoards = lineKeyBoards;
this.oneTime = oneTime;
} }
public JSONObject getKeyboard(Boolean one_time) { public void setOneTime(boolean oneTime) {
this.oneTime = oneTime;
}
public JSONObject getKeyboard() {
JSONObject keyboard = new JSONObject(); JSONObject keyboard = new JSONObject();
keyboard.put("one_time", one_time); keyboard.put("one_time", oneTime);
JSONArray menuLine = new JSONArray(); JSONArray menuLine = new JSONArray();
for (LineKeyBoard lineKeyboard : lineKeyBoards) { for (LineKeyBoard lineKeyboard : lineKeyBoards) {
@ -50,6 +56,11 @@ public class KeyBoard {
return this; return this;
} }
public Builder setOneTime(boolean oneTime) {
KeyBoard.this.oneTime = oneTime;
return this;
}
public KeyBoard build() { public KeyBoard build() {
return KeyBoard.this; return KeyBoard.this;
} }

View File

@ -1,31 +1,29 @@
package org.sadtech.vkbot.core.keyboard; package org.sadtech.vkbot.core.keyboard;
import org.json.JSONObject;
import java.util.List; import java.util.List;
public class KeyBoardService { public class KeyBoardService {
private JSONObject keyBoardYesNo = generateKeyBoardYesNo(); private KeyBoard keyBoardYesNo = generateKeyBoardYesNo();
private JSONObject generateKeyBoardYesNo() { 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();
KeyBoard keyBoard = KeyBoard.builder().setLineKeyBoard(lineKeyBoard).build(); KeyBoard keyBoard = KeyBoard.builder().setLineKeyBoard(lineKeyBoard).build();
return keyBoard.getKeyboard(true); return keyBoard;
} }
public static JSONObject verticalMenu(List<String> labelButtons) { public static KeyBoard verticalMenu(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();
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build()); keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
} }
return keyBoard.getKeyboard(true); return keyBoard;
} }
public JSONObject getKeyBoardYesNo() { public KeyBoard getKeyBoardYesNo() {
return keyBoardYesNo; return keyBoardYesNo;
} }
} }

View File

@ -10,6 +10,8 @@ import org.sadtech.vkbot.core.entity.Person;
public class MailSanderVk implements MailSandler { public class MailSanderVk implements MailSandler {
private Person person; private Person person;
private Integer idRecipient;
private VkApiClient vkApiClient; private VkApiClient vkApiClient;
private GroupActor groupActor; private GroupActor groupActor;
@ -24,14 +26,27 @@ public class MailSanderVk implements MailSandler {
this.groupActor = vkConnect.getGroupActor(); this.groupActor = vkConnect.getGroupActor();
} }
public void setIdRecipient(Integer idRecipient) {
this.idRecipient = idRecipient;
}
public void setPerson(Person person) { public void setPerson(Person person) {
this.person = person; idRecipient = person.getId();
}
@Override
public void send(String text, String keyBoard) {
try {
vkApiClient.messages().send(groupActor).peerId(idRecipient).keyboard(keyBoard).message(text).execute();
} catch (ApiException | ClientException e) {
e.printStackTrace();
}
} }
@Override @Override
public void send(String text) { public void send(String text) {
try { try {
vkApiClient.messages().send(groupActor).userId(person.getId()).message(text).execute(); vkApiClient.messages().send(groupActor).userId(idRecipient).message(text).execute();
} catch (ApiException | ClientException e) { } catch (ApiException | ClientException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -8,4 +8,6 @@ public interface MailSandler {
void setPerson(Person person); void setPerson(Person person);
void send(String text, String keyBoard);
} }