Рефракторинг и новый Контроллер

Рефракторинг класса слушателя сообщений вк
Новый контроллер для запросов к вк без токена
This commit is contained in:
Mark Struchkov 2018-12-23 13:54:40 +03:00
parent 6c6a4338c6
commit 92e8a893a7
4 changed files with 103 additions and 86 deletions

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}
}