From 3027f7d5cf3d8f4d2a4dd2f29cf29743841be12f Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Thu, 31 Jan 2019 15:06:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=BE=D0=BA=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=B4=D0=BB=D1=8F=20=D0=B4=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=83=D0=BF=D0=B0=20=D0=BA=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D0=BC=20=D0=B2=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Что это значит? Теперь разработчику не нужно самому лесть в vk api. для того, чтобы допустим получить имя пользователя и вставить его в сообщение. Достаточно в теле сообщения указать %firstname% * Новый юнит, который позволяет получить какие-то данные за пользователя. Например это позволяет пользователю не ввыодить город, а запрашивать его у вк и уточнять у пользователя актуальность. --- .../java/org/sadtech/vkbot/core/VkApi.java | 13 +++++++- .../org/sadtech/vkbot/core/VkInsertData.java | 31 +++++++++++++++++++ .../vkbot/core/sender/MailSanderVk.java | 22 ++++++++++++- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/sadtech/vkbot/core/VkInsertData.java diff --git a/src/main/java/org/sadtech/vkbot/core/VkApi.java b/src/main/java/org/sadtech/vkbot/core/VkApi.java index cf0a0e9..3f7e219 100644 --- a/src/main/java/org/sadtech/vkbot/core/VkApi.java +++ b/src/main/java/org/sadtech/vkbot/core/VkApi.java @@ -9,12 +9,14 @@ import com.vk.api.sdk.exceptions.ApiException; import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.objects.users.UserMin; import com.vk.api.sdk.objects.users.UserXtrCounters; +import com.vk.api.sdk.queries.users.UserField; +import org.apache.log4j.Logger; import java.util.List; public class VkApi { -// public static final Logger log = Logger.getLogger(String.valueOf(VkApi.class)); + public static final Logger log = Logger.getLogger(String.valueOf(VkApi.class)); private VkApiClient vk; private GroupActor actor; @@ -40,4 +42,13 @@ public class VkApi { return userMin; } + public String getUserUniver(Integer id) { + List temp = null; + try { + temp = vk.users().get(actor).userIds(String.valueOf(id)).fields(UserField.UNIVERSITIES).execute(); + } catch (ApiException | ClientException e) { + e.printStackTrace(); + } + return temp != null ? temp.get(0).getUniversities().get(0).getName() : null; + } } diff --git a/src/main/java/org/sadtech/vkbot/core/VkInsertData.java b/src/main/java/org/sadtech/vkbot/core/VkInsertData.java new file mode 100644 index 0000000..fe11744 --- /dev/null +++ b/src/main/java/org/sadtech/vkbot/core/VkInsertData.java @@ -0,0 +1,31 @@ +package org.sadtech.vkbot.core; + +public class VkInsertData { + + private VkApi vkApi; + private Integer userId; + + public VkInsertData(VkConnect vkConnect) { + this.vkApi = new VkApi(vkConnect); + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String insert(String key) { + String string = ""; + if (key.toLowerCase().equals("%firstname%")) { + string = vkApi.getUserMini(userId).getFirstName(); + } + if (key.toLowerCase().equals("%lastname%")) { + string = vkApi.getUserMini(userId).getLastName(); + } + return string; + } + +} diff --git a/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java b/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java index 72978d0..b484b22 100644 --- a/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java +++ b/src/main/java/org/sadtech/vkbot/core/sender/MailSanderVk.java @@ -7,8 +7,12 @@ 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; +import org.sadtech.vkbot.core.VkInsertData; import org.sadtech.vkbot.core.entity.MailSend; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class MailSanderVk implements MailSandler { public static final Logger log = Logger.getLogger(MailSanderVk.class); @@ -16,16 +20,19 @@ public class MailSanderVk implements MailSandler { private VkApiClient vkApiClient; private GroupActor groupActor; + private VkInsertData vkInsertData; + public MailSanderVk(VkConnect vkConnect) { this.vkApiClient = vkConnect.getVkApiClient(); this.groupActor = vkConnect.getGroupActor(); + this.vkInsertData = new VkInsertData(vkConnect); } @Override public void send(MailSend mailSend) { MessagesSendQuery messages = vkApiClient.messages().send(groupActor).peerId(mailSend.getIdRecipient()); if (mailSend.getMessage() != null) { - messages.message(mailSend.getMessage()); + messages.message(insertWords(mailSend)); } if (mailSend.getKeyboard() != null) { messages.keyboard(mailSend.getKeyboard()); @@ -50,4 +57,17 @@ public class MailSanderVk implements MailSandler { 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(); + } }