Новый тип юнитов - таймеры
Таймер срабатывает не сразу а через какое-то время, заставляет сработать любой другой тип юнита
This commit is contained in:
parent
3027f7d5cf
commit
ccdc44d689
@ -51,4 +51,16 @@ public class VkApi {
|
|||||||
}
|
}
|
||||||
return temp != null ? temp.get(0).getUniversities().get(0).getName() : null;
|
return temp != null ? temp.get(0).getUniversities().get(0).getName() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getUserCity(Integer id) {
|
||||||
|
List<UserXtrCounters> temp = null;
|
||||||
|
try {
|
||||||
|
temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(UserField.CITY).execute();
|
||||||
|
} catch (ApiException | ClientException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return temp != null ? temp.get(0).getCity().getTitle() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,38 @@
|
|||||||
package org.sadtech.vkbot.core;
|
package org.sadtech.vkbot.core;
|
||||||
|
|
||||||
|
import org.sadtech.vkbot.core.entity.MailSend;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class VkInsertData {
|
public class VkInsertData {
|
||||||
|
|
||||||
private VkApi vkApi;
|
private VkApi vkApi;
|
||||||
private Integer userId;
|
|
||||||
|
|
||||||
public VkInsertData(VkConnect vkConnect) {
|
public VkInsertData(VkConnect vkConnect) {
|
||||||
this.vkApi = new VkApi(vkConnect);
|
this.vkApi = new VkApi(vkConnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getUserId() {
|
public String insertWords(MailSend mailSend, Integer idUser) {
|
||||||
return userId;
|
Pattern pattern = Pattern.compile("%(\\w+)%");
|
||||||
|
Matcher m = pattern.matcher(mailSend.getMessage());
|
||||||
|
StringBuffer result = new StringBuffer();
|
||||||
|
while (m.find()) {
|
||||||
|
m.appendReplacement(result, insert(m.group(0), idUser));
|
||||||
|
}
|
||||||
|
m.appendTail(result);
|
||||||
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUserId(Integer userId) {
|
private String insert(String key, Integer userId) {
|
||||||
this.userId = userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String insert(String key) {
|
|
||||||
String string = "";
|
String string = "";
|
||||||
if (key.toLowerCase().equals("%firstname%")) {
|
switch (key) {
|
||||||
|
case "%firstname%":
|
||||||
string = vkApi.getUserMini(userId).getFirstName();
|
string = vkApi.getUserMini(userId).getFirstName();
|
||||||
}
|
break;
|
||||||
if (key.toLowerCase().equals("%lastname%")) {
|
case "%lastname%":
|
||||||
string = vkApi.getUserMini(userId).getLastName();
|
string = vkApi.getUserMini(userId).getLastName();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ public class MailSend {
|
|||||||
|
|
||||||
private String message;
|
private String message;
|
||||||
private String keyboard;
|
private String keyboard;
|
||||||
private Integer idRecipient;
|
|
||||||
private Float lat;
|
private Float lat;
|
||||||
private Float aLong;
|
private Float aLong;
|
||||||
private Integer stickerId;
|
private Integer stickerId;
|
||||||
@ -29,14 +28,6 @@ public class MailSend {
|
|||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getIdRecipient() {
|
|
||||||
return idRecipient;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setIdRecipient(Integer idRecipient) {
|
|
||||||
this.idRecipient = idRecipient;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Float getLat() {
|
public Float getLat() {
|
||||||
return lat;
|
return lat;
|
||||||
}
|
}
|
||||||
@ -66,7 +57,6 @@ public class MailSend {
|
|||||||
return "MailSend{" +
|
return "MailSend{" +
|
||||||
"message='" + message + '\'' +
|
"message='" + message + '\'' +
|
||||||
", keyboard='" + keyboard + '\'' +
|
", keyboard='" + keyboard + '\'' +
|
||||||
", idRecipient=" + idRecipient +
|
|
||||||
", lat=" + lat +
|
", lat=" + lat +
|
||||||
", aLong=" + aLong +
|
", aLong=" + aLong +
|
||||||
", stickerId=" + stickerId +
|
", stickerId=" + stickerId +
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package org.sadtech.vkbot.core.keyboard;
|
|
||||||
|
|
||||||
public interface ButtonKeyBoardService {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -9,6 +9,7 @@ public class KeyBoardService {
|
|||||||
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();
|
||||||
|
keyBoard.setOneTime(true);
|
||||||
return keyBoard;
|
return keyBoard;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,6 +19,7 @@ public class KeyBoardService {
|
|||||||
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());
|
||||||
}
|
}
|
||||||
|
keyBoard.setOneTime(true);
|
||||||
return keyBoard;
|
return keyBoard;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,6 +28,7 @@ public class KeyBoardService {
|
|||||||
for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) {
|
for (ButtonKeyBoard buttonKeyBoard : buttonKeyBoards) {
|
||||||
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
|
keyBoard.addLine(LineKeyBoard.builder().setButtonKeyBoard(buttonKeyBoard).build());
|
||||||
}
|
}
|
||||||
|
keyBoard.setOneTime(true);
|
||||||
return keyBoard;
|
return keyBoard;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
package org.sadtech.vkbot.core.keyboard;
|
|
||||||
|
|
||||||
public interface LineKeyBoardService {
|
|
||||||
}
|
|
@ -9,9 +9,9 @@ import org.apache.log4j.Logger;
|
|||||||
import org.sadtech.vkbot.core.VkConnect;
|
import org.sadtech.vkbot.core.VkConnect;
|
||||||
import org.sadtech.vkbot.core.VkInsertData;
|
import org.sadtech.vkbot.core.VkInsertData;
|
||||||
import org.sadtech.vkbot.core.entity.MailSend;
|
import org.sadtech.vkbot.core.entity.MailSend;
|
||||||
|
import org.sadtech.vkbot.core.insert.InsertWords;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class MailSanderVk implements MailSandler {
|
public class MailSanderVk implements MailSandler {
|
||||||
|
|
||||||
@ -28,12 +28,8 @@ public class MailSanderVk implements MailSandler {
|
|||||||
this.vkInsertData = new VkInsertData(vkConnect);
|
this.vkInsertData = new VkInsertData(vkConnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private MessagesSendQuery createMessage (MailSend mailSend, Integer idUser) {
|
||||||
public void send(MailSend mailSend) {
|
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(idUser);
|
||||||
MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient());
|
|
||||||
if (mailSend.getMessage() != null) {
|
|
||||||
messages.message(insertWords(mailSend));
|
|
||||||
}
|
|
||||||
if (mailSend.getKeyboard() != null) {
|
if (mailSend.getKeyboard() != null) {
|
||||||
messages.keyboard(mailSend.getKeyboard());
|
messages.keyboard(mailSend.getKeyboard());
|
||||||
} else {
|
} else {
|
||||||
@ -44,30 +40,39 @@ public class MailSanderVk implements MailSandler {
|
|||||||
}
|
}
|
||||||
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(idUser).stickerId(mailSend.getStickerId()).execute();
|
||||||
} catch (ApiException | ClientException e) {
|
} catch (ApiException | ClientException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void send(MailSend mailSend, Integer idUser) {
|
||||||
|
MessagesSendQuery messages = createMessage(mailSend, idUser);
|
||||||
|
if (mailSend.getMessage() != null) {
|
||||||
|
messages.message(vkInsertData.insertWords(mailSend, idUser));
|
||||||
|
}
|
||||||
|
sendMessage(messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(MailSend mailSend, Integer idUser, List<String> insertWords) {
|
||||||
|
MessagesSendQuery messages = createMessage(mailSend, idUser);
|
||||||
|
if (mailSend.getMessage() != null) {
|
||||||
|
InsertWords insert = new InsertWords();
|
||||||
|
insert.setInText(vkInsertData.insertWords(mailSend, idUser));
|
||||||
|
insert.insert(insertWords);
|
||||||
|
messages.message(insert.getOutText());
|
||||||
|
}
|
||||||
|
sendMessage(messages);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMessage(MessagesSendQuery messages) {
|
||||||
try {
|
try {
|
||||||
log.info(mailSend);
|
|
||||||
messages.execute();
|
messages.execute();
|
||||||
} catch (ApiException | ClientException e) {
|
} catch (ApiException | ClientException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String insertWords(MailSend mailSend) {
|
|
||||||
vkInsertData.setUserId(mailSend.getIdRecipient());
|
|
||||||
|
|
||||||
Pattern pattern = Pattern.compile("\\%(\\w+)%");
|
|
||||||
Matcher m = pattern.matcher(mailSend.getMessage());
|
|
||||||
StringBuffer result = new StringBuffer();
|
|
||||||
while (m.find()) {
|
|
||||||
m.appendReplacement(result, vkInsertData.insert(m.group(0)));
|
|
||||||
}
|
|
||||||
m.appendTail(result);
|
|
||||||
return result.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,12 @@ package org.sadtech.vkbot.core.sender;
|
|||||||
|
|
||||||
import org.sadtech.vkbot.core.entity.MailSend;
|
import org.sadtech.vkbot.core.entity.MailSend;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface MailSandler {
|
public interface MailSandler {
|
||||||
|
|
||||||
void send(MailSend mailSend);
|
void send(MailSend mailSend, Integer idUser);
|
||||||
|
|
||||||
|
void send(MailSend mailSend, Integer idUser, List<String> insertWords);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user