diff --git a/.gitignore b/.gitignore index 8f8104e..1c7043d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.iml .idea/ -**/target/** \ No newline at end of file +**/target/** +.DS_Store +/vk-bot/src/main/resources/config.properties diff --git a/core/pom.xml b/core/pom.xml new file mode 100644 index 0000000..6f96fe4 --- /dev/null +++ b/core/pom.xml @@ -0,0 +1,15 @@ + + + + consultant + org.sadtech + 1.0-SNAPSHOT + + 4.0.0 + + consultant-core + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 62c59bf..494110a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,12 @@ org.sadtech consultant + pom 0.1-SNAPSHOT + + vk-bot + core + \ No newline at end of file diff --git a/vk-bot/pom.xml b/vk-bot/pom.xml new file mode 100644 index 0000000..7c82c9f --- /dev/null +++ b/vk-bot/pom.xml @@ -0,0 +1,91 @@ + + + + time-table + org.sadtech + 1.0-SNAPSHOT + + 4.0.0 + + vk-bot + 0.1-SNAPSHOT + war + + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + + + + 0.5.12 + 5.1.3.RELEASE + + + + + com.vk.api + sdk + ${vk.api.ver} + + + + org.springframework + spring-core + ${spring.ver} + + + + org.springframework + spring-context + ${spring.ver} + + + + org.springframework + spring-context-support + ${spring.ver} + + + org.springframework + spring-web + ${spring.ver} + + + javax.servlet + javax.servlet-api + 3.1.0 + + + org.springframework + spring-webmvc + 5.1.3.RELEASE + + + org.springframework + spring-core + 5.1.3.RELEASE + + + org.mortbay.jetty + jetty-util + 6.1.25 + + + + org.json + json + 20180813 + + + + + \ No newline at end of file diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/Observable.java b/vk-bot/src/main/java/org/sadtech/vkbot/Observable.java new file mode 100644 index 0000000..b9a0acc --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/Observable.java @@ -0,0 +1,7 @@ +package org.sadtech.vkbot; + +public interface Observable { + void registerObserver(org.sadtech.vkbot.Observer o); +// void removeObserver(Observer o); + void notifyObservers(); +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/Observer.java b/vk-bot/src/main/java/org/sadtech/vkbot/Observer.java new file mode 100644 index 0000000..8cca6ce --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/Observer.java @@ -0,0 +1,9 @@ +package org.sadtech.vkbot; + +import com.google.gson.JsonObject; + +import java.util.List; + +public interface Observer { + void update(List jsonObjects); +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/ResponseData.java b/vk-bot/src/main/java/org/sadtech/vkbot/ResponseData.java new file mode 100644 index 0000000..e6008b2 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/ResponseData.java @@ -0,0 +1,29 @@ +package org.sadtech.vkbot; + +import com.google.gson.JsonObject; + +import java.util.ArrayList; +import java.util.List; + +public class ResponseData implements Observable { + + private List observers = new ArrayList(); + private List jsonObjects = new ArrayList(); + + @Override + public void registerObserver(Observer o) { + observers.add(o); + } + + @Override + public void notifyObservers() { + for (Observer observer:observers) { + observer.update(jsonObjects); + } + } + + public void setJsonObjects(List jsonObjects) { + this.jsonObjects = jsonObjects; + notifyObservers(); + } +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/TestLogic.java b/vk-bot/src/main/java/org/sadtech/vkbot/TestLogic.java new file mode 100644 index 0000000..8e8ed9e --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/TestLogic.java @@ -0,0 +1,20 @@ +package org.sadtech.vkbot; + +import com.google.gson.JsonObject; + +import java.util.List; + +public class TestLogic implements Observer{ + + private ResponseData responseData; + + public TestLogic(ResponseData responseData) { + this.responseData = responseData; + responseData.registerObserver(this); + } + + @Override + public void update(List jsonObjects) { + System.out.println(jsonObjects); + } +} 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 new file mode 100644 index 0000000..fe02b9e --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/config/SpringConfig.java @@ -0,0 +1,87 @@ +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.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.consultant.controller.Test; +import org.sadtech.vkbot.TestLogic; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +@Configuration +@PropertySource("classpath:config.properties") +public class SpringConfig { + + @Value("${vk.groupID}") + private String groupId; + + @Value("${vk.appID}") + private String appID; + + @Value("${vk.secretKey}") + private String secretKey; + + @Value("${vk.redirectURL}") + private String redirectURL; + + @Value("${vk.code}") + private String code; + + @Value("${vk.token}") + private String token; + + @Bean + public TransportClient transportClient() { + return HttpTransportClient.getInstance(); + } + + @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 GetLongPollServerResponse GroupsGetLongPollServerQuery() { + GetLongPollServerResponse getLongPollServerResponse = null; + try { + getLongPollServerResponse = vkApiClient().groups().getLongPollServer(groupActor()).execute(); + } catch (ApiException e) { + e.printStackTrace(); + } catch (ClientException e) { + e.printStackTrace(); + } +// System.out.println("server: "+getLongPollServerResponse); + return getLongPollServerResponse; + } + + @Bean(initMethod = "start") + public Test test() { + return new Test(); + } + + @Bean + public ResponseData responseData() { + return new ResponseData(); + } + + @Bean + public TestLogic testLogic() { + return new TestLogic(responseData()); + } +} diff --git a/vk-bot/src/main/java/org/sadtech/vkbot/config/WebConfig.java b/vk-bot/src/main/java/org/sadtech/vkbot/config/WebConfig.java new file mode 100644 index 0000000..f7b4685 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/config/WebConfig.java @@ -0,0 +1,27 @@ +package org.sadtech.vkbot.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; + +@Configuration +@ComponentScan("org.sadtech.vkbot.controller") +public class WebConfig implements WebApplicationInitializer { + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.register(SpringConfig.class, WebConfig.class); + context.setServletContext(servletContext); + + ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); + dispatcher.setLoadOnStartup(1); + dispatcher.addMapping("/"); + } +} 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 new file mode 100644 index 0000000..849c466 --- /dev/null +++ b/vk-bot/src/main/java/org/sadtech/vkbot/controller/Test.java @@ -0,0 +1,66 @@ +package org.sadtech.consultant.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 org.json.JSONObject; +import org.sadtech.vkbot.ResponseData; +import org.springframework.beans.factory.annotation.Autowired; + +public class Test extends Thread { + + @Autowired + private VkApiClient vk; + + @Autowired + private GroupActor actor; + + @Autowired + private ResponseData responseData; + + 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(5); +// Gson gson = new Gson(); + + while (true) { + try { + getLongPollEventsResponse = getLongPollEventsQuery.execute(); + } catch (ApiException e) { + e.printStackTrace(); + } catch (ClientException e) { + e.printStackTrace(); + } +// System.out.println(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(5); + } + } + +}