Browse Source

first init

master
gsd 2 years ago
commit
913b2d6964
  1. 33
      .gitignore
  2. 50
      pom.xml
  3. 13
      src/main/java/marusia/MarusiaApplication.java
  4. 33
      src/main/java/marusia/controllers/FileController.java
  5. 32
      src/main/java/marusia/controllers/SkillsController.java
  6. 30
      src/main/java/marusia/entities/BaseRequest.java
  7. 43
      src/main/java/marusia/entities/MarusiaRequest.java
  8. 5
      src/main/java/marusia/enums/AudioType.java
  9. 2
      src/main/resources/application.yaml
  10. BIN
      src/main/resources/files/i_za_moe_ochko.mp3
  11. 13
      src/test/java/marusia/MarusiaApplicationTests.java

33
.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/

50
pom.xml

@ -0,0 +1,50 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>marusia</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>marusia</name>
<description>marusia</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

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

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

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

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

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

5
src/main/java/marusia/enums/AudioType.java

@ -0,0 +1,5 @@
package marusia.enums;
public enum AudioType {
vk, url
}

2
src/main/resources/application.yaml

@ -0,0 +1,2 @@
server:
port: 8889

BIN
src/main/resources/files/i_za_moe_ochko.mp3

Binary file not shown.

13
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() {
}
}
Loading…
Cancel
Save