Skip to content

Commit fc0875a

Browse files
committed
Message callback done
0 parents  commit fc0875a

11 files changed

+284
-0
lines changed

.mvn/wrapper/maven-wrapper.properties

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
wrapperVersion=3.3.2
18+
distributionType=only-script
19+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.7/apache-maven-3.9.7-bin.zip

pom.xml

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.3.2</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>ru.mika</groupId>
12+
<artifactId>VkPingPong</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>VkPingPong</name>
15+
<description>Just AI Vk bot</description>
16+
<url/>
17+
<licenses>
18+
<license/>
19+
</licenses>
20+
<developers>
21+
<developer/>
22+
</developers>
23+
<scm>
24+
<connection/>
25+
<developerConnection/>
26+
<tag/>
27+
<url/>
28+
</scm>
29+
<properties>
30+
<java.version>17</java.version>
31+
</properties>
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
37+
38+
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp/okhttp -->
39+
<dependency>
40+
<groupId>com.squareup.okhttp</groupId>
41+
<artifactId>okhttp</artifactId>
42+
<version>2.7.5</version>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.projectlombok</groupId>
52+
<artifactId>lombok</artifactId>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-maven-plugin</artifactId>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ru.mika.vkpingpong;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class BotApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(BotApplication.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ru.mika.vkpingpong.DTO;
2+
3+
public class CallbackAPIConfirmationDTO {
4+
private String type;
5+
private int group_id;
6+
7+
CallbackAPIConfirmationDTO(String type, int group_id) {
8+
this.type = type;
9+
this.group_id = group_id;
10+
}
11+
12+
CallbackAPIConfirmationDTO() {}
13+
14+
public String getType() {
15+
return type;
16+
}
17+
18+
public int getGroup_id(){
19+
return group_id;
20+
}
21+
22+
public void setType(String type) {
23+
this.type = type;
24+
}
25+
26+
public void setGroup_id(int group_id) {
27+
this.group_id = group_id;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ru.mika.vkpingpong.DTO;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Getter;
8+
import lombok.NoArgsConstructor;
9+
import java.util.HashMap;
10+
11+
12+
@Builder
13+
@Getter
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
@JsonIgnoreProperties(ignoreUnknown = true)
17+
public class CallbackAPIMessageDTO {
18+
public enum MessageType {
19+
message_new, confirmation
20+
}
21+
22+
private MessageType type;
23+
private HashMap<String, Object> object;
24+
@JsonProperty(value = "group_id")
25+
private Long groupId;
26+
private String secret;
27+
@JsonProperty(value = "event_id")
28+
private String eventId;
29+
private String v;
30+
31+
32+
/*
33+
private String secret;
34+
private Message object;
35+
36+
37+
public CallbackAPIMessageDTO(String secret, Message object) {
38+
this.object = object;
39+
this.secret = secret;
40+
}
41+
42+
public String getSecret() {
43+
return secret;
44+
}
45+
46+
public Message getObject() {
47+
return object;
48+
}
49+
50+
*/
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ru.mika.vkpingpong.DTO;
2+
3+
public class Message {
4+
private String from_id;
5+
private String text;
6+
private String accessToken;
7+
8+
Message (String from_id, String text, String accessToken) {
9+
this.from_id = from_id;
10+
this.text = text;
11+
this.accessToken = accessToken;
12+
}
13+
14+
public String getFrom_id() {
15+
return from_id;
16+
}
17+
18+
public String getText() {
19+
return text;
20+
}
21+
22+
public String getAccessToken() {
23+
return accessToken;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ru.mika.vkpingpong.config;
2+
3+
public class Config {
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ru.mika.vkpingpong.controller;
2+
3+
//import lombok.RequiredArgsConstructor;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.*;
7+
import ru.mika.vkpingpong.DTO.CallbackAPIConfirmationDTO;
8+
import ru.mika.vkpingpong.DTO.CallbackAPIMessageDTO;
9+
import ru.mika.vkpingpong.helper.CallbackConfirmationHelper;
10+
import ru.mika.vkpingpong.helper.CallbackMessageHelper;
11+
12+
import java.io.IOException;
13+
14+
@RestController
15+
@RequestMapping("/callback")
16+
public class CallbackAPIController {
17+
18+
@PostMapping("/confirmation")
19+
public ResponseEntity<String> handleConfirmationCallback(@RequestBody CallbackAPIConfirmationDTO callbackDTO) {
20+
return new ResponseEntity<>(CallbackConfirmationHelper.confirmationHandler(callbackDTO), HttpStatus.OK);
21+
}
22+
23+
@PostMapping("/message")
24+
public ResponseEntity<String> handleMessageCallback(@RequestBody CallbackAPIMessageDTO callbackDTO) throws IOException {
25+
return new ResponseEntity<>(CallbackMessageHelper.messageHandler(callbackDTO), HttpStatus.OK);
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ru.mika.vkpingpong.helper;
2+
3+
import ru.mika.vkpingpong.DTO.CallbackAPIConfirmationDTO;
4+
import ru.mika.vkpingpong.config.SecretConfig;
5+
6+
public class CallbackConfirmationHelper {
7+
8+
public static String confirmationHandler(CallbackAPIConfirmationDTO callbackDTO) {
9+
if (callbackDTO.getType().equals("confirmation") && callbackDTO.getGroup_id() == SecretConfig.getGroupId())
10+
return SecretConfig.getAcceptString();
11+
return null;
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ru.mika.vkpingpong.helper;
2+
3+
import com.squareup.okhttp.OkHttpClient;
4+
import com.squareup.okhttp.Request;
5+
import com.squareup.okhttp.Response;
6+
import org.springframework.web.util.UriComponentsBuilder;
7+
import ru.mika.vkpingpong.DTO.CallbackAPIMessageDTO;
8+
import ru.mika.vkpingpong.config.SecretConfig;
9+
10+
import java.io.IOException;
11+
import java.net.URI;
12+
import java.util.LinkedHashMap;
13+
14+
public class CallbackMessageHelper {
15+
public static String messageHandler(CallbackAPIMessageDTO callbackDTO) throws IOException {
16+
LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) callbackDTO.getObject().get("message");
17+
long random_id = Long.parseLong(map.get("date").toString());
18+
URI uri = UriComponentsBuilder.fromHttpUrl("https://api.vk.com/method/messages.send")
19+
.queryParam("access_token", SecretConfig.getAccessToken())
20+
.queryParam("user_id", map.get("from_id").toString())
21+
.queryParam("random_id", random_id)
22+
.queryParam("message", "Вы сказали: " + map.get("text"))
23+
.queryParam("v", callbackDTO.getV())
24+
.build()
25+
.toUri();
26+
27+
OkHttpClient client = new OkHttpClient();
28+
Request request = new Request.Builder().url(uri.toURL()).build();
29+
Response response = client.newCall(request).execute();
30+
31+
System.out.println(request.urlString());
32+
return response.message();
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.application.name=VkPingPong

0 commit comments

Comments
 (0)