commit 913b2d6964a29abb00876a900ec86b5274f0426d Author: gsd Date: Mon Feb 27 19:50:35 2023 +0300 first init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7c41a99 --- /dev/null +++ b/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.0.3 + + + com.example + marusia + 0.0.1-SNAPSHOT + marusia + marusia + + 17 + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + 3.0.2 + + + com.fasterxml.jackson.core + jackson-core + 2.14.2 + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/marusia/MarusiaApplication.java b/src/main/java/marusia/MarusiaApplication.java new file mode 100644 index 0000000..1095ca8 --- /dev/null +++ b/src/main/java/marusia/MarusiaApplication.java @@ -0,0 +1,13 @@ +package marusia; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MarusiaApplication { + + public static void main(String[] args) { + SpringApplication.run(MarusiaApplication.class, args); + } + +} diff --git a/src/main/java/marusia/controllers/FileController.java b/src/main/java/marusia/controllers/FileController.java new file mode 100644 index 0000000..7520d48 --- /dev/null +++ b/src/main/java/marusia/controllers/FileController.java @@ -0,0 +1,33 @@ +package marusia.controllers; + +import org.apache.tomcat.util.http.fileupload.IOUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.InputStreamResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; + +@RequestMapping("api/file") +@RestController +public class FileController { + + //бля дяд ну ты дурачек сюда инжектить + @Autowired + ResourceLoader resourceLoader; + + @GetMapping("{filename}") + public ResponseEntity getFile(@PathVariable("filename") String filename) throws IOException { + Resource resource = resourceLoader.getResource("classpath:files/%s".formatted(filename)); + return ResponseEntity + .ok() + .header(HttpHeaders.CONTENT_TYPE, "audio/mpeg") + .body(new InputStreamResource(resource.getInputStream())); + } +} diff --git a/src/main/java/marusia/controllers/SkillsController.java b/src/main/java/marusia/controllers/SkillsController.java new file mode 100644 index 0000000..da6220c --- /dev/null +++ b/src/main/java/marusia/controllers/SkillsController.java @@ -0,0 +1,32 @@ +package marusia.controllers; + +import marusia.entities.MarusiaRequest; +import marusia.enums.AudioType; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +//https://github.com/nonstandardmail/marusia-skill-echo-python/blob/master/handler.py +//https://skill-debugger.marusia.mail.ru/ +//https://dev.vk.com/marusia/api + +@RequestMapping("/api/skills") +@RestController +public class SkillsController { + @PostMapping + public ResponseEntity handler(@RequestBody MarusiaRequest request_json){ + System.out.println(request_json); + System.out.println(request_json.get("command")); + switch (request_json.getCommand()) { + case ("кайф"): + return new ResponseEntity<>(request_json.createTextResponse("всем привет"), HttpStatus.OK); + case ("за ваше здоровье"): + return new ResponseEntity<>(request_json.createAudioResponse(AudioType.vk, "27919760_456239588"), HttpStatus.OK); + default: + return new ResponseEntity<>(request_json.createTextResponse("мммммм"), HttpStatus.OK); + } + } +} diff --git a/src/main/java/marusia/entities/BaseRequest.java b/src/main/java/marusia/entities/BaseRequest.java new file mode 100644 index 0000000..27272da --- /dev/null +++ b/src/main/java/marusia/entities/BaseRequest.java @@ -0,0 +1,30 @@ +package marusia.entities; + +import java.util.HashMap; +import java.util.Map; + +public class BaseRequest extends HashMap { + public Map getSessionInfo() { + return Map.of( + "session_id", ((HashMap) this.get("session")).get("session_id"), + "user_id", ((HashMap) this.get("session")).get("user_id"), + "message_id", ((HashMap) this.get("session")).get("message_id") + ); + } + + public HashMap getRequest() { + return (HashMap) this.get("request"); + } + + public Object getVersion() { + return this.get("version"); + } + + public String getCommand() { + return (String) getRequest().get("command"); + } + + public boolean checkCommand(String text) { + return getCommand().equals(text); + } +} diff --git a/src/main/java/marusia/entities/MarusiaRequest.java b/src/main/java/marusia/entities/MarusiaRequest.java new file mode 100644 index 0000000..14b608d --- /dev/null +++ b/src/main/java/marusia/entities/MarusiaRequest.java @@ -0,0 +1,43 @@ +package marusia.entities; + +import marusia.enums.AudioType; + +import java.util.List; +import java.util.Map; + +public class MarusiaRequest extends BaseRequest { + public Map createTextResponse(String text, String tts) { + return Map.of( + "response", Map.of( + "text", text, + "tts", tts, + "end_session", false + ), + "session", getSessionInfo(), + "version", getVersion() + ); + } + + public Map createTextResponse(String text) { + return createTextResponse(text, text); + } + + public Map createAudioResponse(AudioType audioType, String source) { + return Map.of( + "session", getSessionInfo(), + "version", getVersion(), + "response", Map.of("audio_player", Map.of( + "seek_track", 0, + "seek_second", 0, + "playlist", List.of( + Map.of("stream", Map.of( + "source_type", audioType.name(), + "source", source, + "track_id", source + )) + ) + ), "end_session", false + ) + ); + } +} diff --git a/src/main/java/marusia/enums/AudioType.java b/src/main/java/marusia/enums/AudioType.java new file mode 100644 index 0000000..ae5c109 --- /dev/null +++ b/src/main/java/marusia/enums/AudioType.java @@ -0,0 +1,5 @@ +package marusia.enums; + +public enum AudioType { + vk, url +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml new file mode 100644 index 0000000..5217df1 --- /dev/null +++ b/src/main/resources/application.yaml @@ -0,0 +1,2 @@ +server: + port: 8889 \ No newline at end of file diff --git a/src/main/resources/files/i_za_moe_ochko.mp3 b/src/main/resources/files/i_za_moe_ochko.mp3 new file mode 100644 index 0000000..2e1f4c0 Binary files /dev/null and b/src/main/resources/files/i_za_moe_ochko.mp3 differ diff --git a/src/test/java/marusia/MarusiaApplicationTests.java b/src/test/java/marusia/MarusiaApplicationTests.java new file mode 100644 index 0000000..4077da1 --- /dev/null +++ b/src/test/java/marusia/MarusiaApplicationTests.java @@ -0,0 +1,13 @@ +package marusia; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class MarusiaApplicationTests { + + @Test + void contextLoads() { + } + +}