From 92e8a893a7415de2a6bb150fb1249abb9817b9bc Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sun, 23 Dec 2018 13:54:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D1=80=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=BD=D0=B3=20=D0=B8=20=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D1=8B=D0=B9=20=D0=9A=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB?= =?UTF-8?q?=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Рефракторинг класса слушателя сообщений вк Новый контроллер для запросов к вк без токена --- .../java/org/sadtech/vkbot/VkOpenMethod.java | 22 +++++ .../sadtech/vkbot/config/SpringConfig.java | 25 ++++-- .../vkbot/controller/MessagesListener.java | 62 ++++++++++++++ .../org/sadtech/vkbot/controller/Test.java | 80 ------------------- 4 files changed, 103 insertions(+), 86 deletions(-) create mode 100644 vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java create mode 100644 vk-bot/src/main/java/org/sadtech/vkbot/controller/MessagesListener.java delete mode 100644 vk-bot/src/main/java/org/sadtech/vkbot/controller/Test.java diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java b/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java new file mode 100644 index 0000000..87ee357 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/VkOpenMethod.java @@ -0,0 +1,22 @@ +package org.sadtech.vkbot; + +import com.vk.api.sdk.client.VkApiClient; +import com.vk.api.sdk.client.actors.GroupActor; +import lombok.extern.log4j.Log4j; +import org.springframework.stereotype.Component; + +@Component +@Log4j +public class VkOpenMethod { + + private VkApiClient vk; + + private GroupActor actor; + + public VkOpenMethod(VkApiClient vk, GroupActor actor) { + this.vk = vk; + this.actor = actor; + } + + +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfig.java b/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfig.java index 20c0c81..1162f1d 100644 --- a/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfig.java +++ b/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfig.java @@ -3,13 +3,15 @@ package org.sadtech.vkbot.config; import com.vk.api.sdk.client.TransportClient; import com.vk.api.sdk.client.VkApiClient; import com.vk.api.sdk.client.actors.GroupActor; +import com.vk.api.sdk.client.actors.ServiceActor; import com.vk.api.sdk.exceptions.ApiException; import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.httpclient.HttpTransportClient; import com.vk.api.sdk.objects.groups.responses.GetLongPollServerResponse; import org.sadtech.vkbot.ResponseData; import org.sadtech.vkbot.TestLogic; -import org.sadtech.vkbot.controller.Test; +import org.sadtech.vkbot.VkOpenMethod; +import org.sadtech.vkbot.controller.MessagesListener; import org.sadtech.vkbot.service.UserService; import org.sadtech.vkbot.service.impl.UserServiceImpl; import org.springframework.beans.factory.annotation.Value; @@ -17,7 +19,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @Configuration @PropertySource("classpath:config.properties") @@ -42,6 +43,9 @@ public class SpringConfig { @Value("${vk.token}") private String token; + @Value("${vk.service.token}") + private String serviceToken; + @Bean public TransportClient transportClient() { return HttpTransportClient.getInstance(); @@ -50,14 +54,18 @@ public class SpringConfig { @Bean public VkApiClient vkApiClient() { VkApiClient vk = new VkApiClient(transportClient()); -// System.out.println("vk: " + vk); return vk; } @Bean public GroupActor groupActor() { GroupActor actor = new GroupActor(new Integer(groupId), token); -// System.out.println("actor: " + actor); + return actor; + } + + @Bean + public ServiceActor serviceActor() { + ServiceActor actor = new ServiceActor(new Integer(appID), serviceToken); return actor; } @@ -76,8 +84,8 @@ public class SpringConfig { } @Bean(initMethod = "start") - public Test test() { - return new Test(); + public MessagesListener messagesListener() { + return new MessagesListener(); } @Bean @@ -94,4 +102,9 @@ public class SpringConfig { public TestLogic testLogic() { return new TestLogic(responseData()); } + + @Bean + public VkOpenMethod vkOpenMethod() { + return new VkOpenMethod(vkApiClient(), groupActor()); + } } diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/controller/MessagesListener.java b/vk-bot/src/main/java/org/sadtech/vkbot/controller/MessagesListener.java new file mode 100644 index 0000000..76ee76b --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/controller/MessagesListener.java @@ -0,0 +1,62 @@ +package org.sadtech.vkbot.controller; + +import com.vk.api.sdk.actions.LongPoll; +import com.vk.api.sdk.callback.longpoll.queries.GetLongPollEventsQuery; +import com.vk.api.sdk.callback.longpoll.responses.GetLongPollEventsResponse; +import com.vk.api.sdk.client.VkApiClient; +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.objects.groups.responses.GetLongPollServerResponse; +import lombok.extern.log4j.Log4j; +import org.sadtech.vkbot.ResponseData; +import org.springframework.beans.factory.annotation.Autowired; + +@Log4j +public class MessagesListener extends Thread{ + + @Autowired + private VkApiClient vk; + + @Autowired + private GroupActor actor; + + @Autowired + private ResponseData responseData; + + public void run() { + GetLongPollServerResponse serverResponse = null; + try { + serverResponse = vk.groups().getLongPollServer(actor).execute(); + } catch (ApiException e) { + e.printStackTrace(); + } catch (ClientException e) { + e.printStackTrace(); + } + + String key = serverResponse.getServer(); + String server = serverResponse.getKey(); + Integer ts = serverResponse.getTs(); + + LongPoll longPoll = new LongPoll(vk); + GetLongPollEventsQuery longPollEventsQuery = longPoll.getEvents(server, key, ts).waitTime(20); + + GetLongPollEventsResponse eventsResponse = null; + do { + try { + eventsResponse = longPollEventsQuery.execute(); + } catch (ApiException e) { + e.printStackTrace(); + } catch (ClientException e) { + e.printStackTrace(); + } + if (eventsResponse.getUpdates().toArray().length != 0) { + + responseData.setJsonObjects(eventsResponse.getUpdates()); + + } + longPollEventsQuery = longPoll.getEvents(server, key, eventsResponse.getTs()).waitTime(20); + } while (true); + } + +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/controller/Test.java b/vk-bot/src/main/java/org/sadtech/vkbot/controller/Test.java deleted file mode 100644 index 2ccf655..0000000 --- a/vk-bot/src/main/java/org/sadtech/vkbot/controller/Test.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.sadtech.vkbot.controller; - -import com.vk.api.sdk.actions.LongPoll; -import com.vk.api.sdk.callback.longpoll.queries.GetLongPollEventsQuery; -import com.vk.api.sdk.callback.longpoll.responses.GetLongPollEventsResponse; -import com.vk.api.sdk.client.VkApiClient; -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.objects.groups.responses.GetLongPollServerResponse; -import lombok.extern.log4j.Log4j; -import org.json.JSONObject; -import org.sadtech.vkbot.ResponseData; -import org.sadtech.vkbot.entity.User; -import org.sadtech.vkbot.service.UserService; -import org.springframework.beans.factory.annotation.Autowired; - -import javax.annotation.Resource; - -@Log4j - -public class Test extends Thread { - - @Autowired - private VkApiClient vk; - - @Autowired - private GroupActor actor; - - @Autowired - private ResponseData responseData; - - @Resource - private UserService userService; - - private GetLongPollEventsResponse getLongPollEventsResponse = null; - - public void run() { - GetLongPollServerResponse getResponse = null; - try { - getResponse = vk.groups().getLongPollServer(actor).execute(); - } catch (ApiException e) { - e.printStackTrace(); - } catch (ClientException e) { - e.printStackTrace(); - } - JSONObject jObject = new JSONObject(getResponse); // json -// System.out.println(getResponse); - String key = jObject.getString("key"); // get the name from data. - String server = jObject.getString("server"); - Integer ts = jObject.getInt("ts"); - LongPoll longPoll = new LongPoll(vk); - GetLongPollEventsQuery getLongPollEventsQuery = longPoll.getEvents(server, key, ts).waitTime(20); -// Gson gson = new Gson(); - - User user = new User(); - user.setId(1); - user.setName("Test"); - userService.addUser(user); - - while (true) { - try { - getLongPollEventsResponse = getLongPollEventsQuery.execute(); - } catch (ApiException e) { - e.printStackTrace(); - } catch (ClientException e) { - e.printStackTrace(); - } - log.info(getLongPollEventsResponse); - if (getLongPollEventsResponse.getUpdates().toArray().length != 0) { - responseData.setJsonObjects(getLongPollEventsResponse.getUpdates()); -// JsonObject updates = getLongPollEventsResponse.getUpdates().get(0); -// JsonObject object = updates.getAsJsonObject("object"); -// Message messages = gson.fromJson(object, Message.class); - } - getLongPollEventsQuery = longPoll.getEvents(server, key, getLongPollEventsResponse.getTs()).waitTime(20); - } - } - -}