Научился принимать сообщения из вк

* Все данные для настройки соединения и взаимодествия с вк в отдельном файле
* Научился отлавливать новые сообщения в сообществе и отправлять ответ
* Добавил паттерн Наблюдатель
This commit is contained in:
Mark Struchkov 2018-12-20 00:45:34 +03:00
parent 3b1c6bfe06
commit 6793efcc55
11 changed files with 359 additions and 1 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
*.iml
.idea/
**/target/**
.DS_Store
/vk-bot/src/main/resources/config.properties

15
core/pom.xml Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>consultant</artifactId>
<groupId>org.sadtech</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consultant-core</artifactId>
</project>

View File

@ -6,7 +6,12 @@
<groupId>org.sadtech</groupId>
<artifactId>consultant</artifactId>
<packaging>pom</packaging>
<version>0.1-SNAPSHOT</version>
<modules>
<module>vk-bot</module>
<module>core</module>
</modules>
</project>

91
vk-bot/pom.xml Normal file
View File

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>time-table</artifactId>
<groupId>org.sadtech</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>vk-bot</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<vk.api.ver>0.5.12</vk.api.ver>
<spring.ver>5.1.3.RELEASE</spring.ver>
</properties>
<dependencies>
<dependency>
<groupId>com.vk.api</groupId>
<artifactId>sdk</artifactId>
<version>${vk.api.ver}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.ver}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.ver}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.ver}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.ver}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>6.1.25</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,7 @@
package org.sadtech.vkbot;
public interface Observable {
void registerObserver(org.sadtech.vkbot.Observer o);
// void removeObserver(Observer o);
void notifyObservers();
}

View File

@ -0,0 +1,9 @@
package org.sadtech.vkbot;
import com.google.gson.JsonObject;
import java.util.List;
public interface Observer {
void update(List<JsonObject> jsonObjects);
}

View File

@ -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<Observer> observers = new ArrayList<Observer>();
private List<JsonObject> jsonObjects = new ArrayList<JsonObject>();
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
@Override
public void notifyObservers() {
for (Observer observer:observers) {
observer.update(jsonObjects);
}
}
public void setJsonObjects(List<JsonObject> jsonObjects) {
this.jsonObjects = jsonObjects;
notifyObservers();
}
}

View File

@ -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<JsonObject> jsonObjects) {
System.out.println(jsonObjects);
}
}

View File

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

View File

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

View File

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