did a lot of stuff

This commit is contained in:
Julian Brammer 2024-06-29 17:48:07 +02:00
parent 4d057ca562
commit f61e9205ec
10 changed files with 98 additions and 18 deletions

0
.mvn/wrapper/maven-wrapper.properties vendored Normal file → Executable file
View File

0
mvnw.cmd vendored Normal file → Executable file
View File

View File

@ -1,5 +1,6 @@
package com.brulijam.minecraftapi.controller; package com.brulijam.minecraftapi.controller;
import com.brulijam.minecraftapi.service.TelegramService;
import com.brulijam.minecraftapi.telegrambot.TelegramBot; import com.brulijam.minecraftapi.telegrambot.TelegramBot;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.*;
@RequiredArgsConstructor @RequiredArgsConstructor
public class TelegramBotController { public class TelegramBotController {
private final TelegramService telegramService;
private final TelegramBot telegramBot; private final TelegramBot telegramBot;
@Operation(summary = "sends a message") @Operation(summary = "sends a message")
@ -24,4 +26,12 @@ public class TelegramBotController {
log.info("{}", userId); log.info("{}", userId);
telegramBot.sendMessage(userId, message); telegramBot.sendMessage(userId, message);
} }
@Operation(summary = "For debugging")
@PostMapping(value = "/admin", produces = "application/json")
public String admin() {
return telegramService.getTelegramUsers().toString();
}
} }

View File

@ -31,7 +31,7 @@ public class TutelController {
@Operation(summary = "adds a tutel") @Operation(summary = "adds a tutel")
@PostMapping(value = "/add") @PostMapping(value = "/add")
public Tutel addTutel(@Parameter(name = "label", description = "label of the tutel") @RequestParam String label, public Tutel addTutel(@Parameter(name = "label", description = "label of the tutel") @RequestParam String label,
@Parameter(name = "telegramUserId", description = "Telegram ID of the user the Turtle belongs to") @RequestParam String telegramUserId) { @Parameter(name = "telegramUserId", description = "Telegram ID of the user the Turtle belongs to") @RequestParam long telegramUserId) {
Tutel tutel = new Tutel(label, telegramUserId); Tutel tutel = new Tutel(label, telegramUserId);
tutelService.addToList(tutel); tutelService.addToList(tutel);
return tutel; return tutel;

View File

@ -19,13 +19,13 @@ import java.util.Objects;
public class Tutel { public class Tutel {
private final long id; private final long id;
private final String telegramUserId; private final long telegramUserId;
private final String label; private final String label;
private int fuelLevel; private int fuelLevel;
private JSONObject inventory; private JSONObject inventory;
public Tutel(String label, String telegramUserId) { public Tutel(String label, long telegramUserId) {
if (label.endsWith("=")) { if (label.endsWith("=")) {
label = label.substring(0, label.length() - 1); label = label.substring(0, label.length() - 1);
} }
@ -34,8 +34,4 @@ public class Tutel {
this.id = Math.abs(this.hashCode()); this.id = Math.abs(this.hashCode());
this.telegramUserId = telegramUserId; this.telegramUserId = telegramUserId;
} }
} }

View File

@ -1,5 +1,6 @@
package com.brulijam.minecraftapi.service; package com.brulijam.minecraftapi.service;
import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.objects.User; import org.telegram.telegrambots.meta.api.objects.User;
@ -8,6 +9,7 @@ import java.util.*;
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
@Getter
public class TelegramService { public class TelegramService {
private final List<User> telegramUsers = new ArrayList<>(); private final List<User> telegramUsers = new ArrayList<>();
@ -19,7 +21,10 @@ public class TelegramService {
String randomUUID = UUID.randomUUID().toString().replaceAll("_", ""); String randomUUID = UUID.randomUUID().toString().replaceAll("_", "");
tokens.put(user.getId(), randomUUID); tokens.put(user.getId(), randomUUID);
}
public String getTokenByUserId(Long userId) {
return tokens.get(userId);
} }
public User getUserByUserId(long userId) { public User getUserByUserId(long userId) {

View File

@ -3,6 +3,8 @@ package com.brulijam.minecraftapi.service;
import com.brulijam.minecraftapi.model.Tutel; import com.brulijam.minecraftapi.model.Tutel;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
@ -40,4 +42,31 @@ public class TutelService {
.findFirst() .findFirst()
.orElse(null); .orElse(null);
} }
public List<Tutel> getTutelsForTelegramUser(long telegramUserId) {
return tutelList.stream()
.filter(Tutel -> Tutel.getTelegramUserId() == telegramUserId)
.toList();
}
public JSONObject tutelToJson(Tutel tutel) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", tutel.getId());
jsonObject.put("label", tutel.getLabel());
jsonObject.put("telegramUserId", tutel.getTelegramUserId());
jsonObject.put("fuelLevel", tutel.getFuelLevel());
jsonObject.put("inventory", tutel.getInventory());
return jsonObject;
}
public JSONArray tutelsToJson(List<Tutel> tutels) {
JSONArray jsonArray = new JSONArray();
for (Tutel tutel : tutels) {
jsonArray.put(tutelToJson(tutel));
}
return jsonArray;
}
} }

View File

@ -1,5 +1,6 @@
package com.brulijam.minecraftapi.telegrambot; package com.brulijam.minecraftapi.telegrambot;
import com.brulijam.minecraftapi.model.Tutel;
import com.brulijam.minecraftapi.service.TelegramService; import com.brulijam.minecraftapi.service.TelegramService;
import com.brulijam.minecraftapi.service.TutelService; import com.brulijam.minecraftapi.service.TutelService;
import lombok.Getter; import lombok.Getter;
@ -12,6 +13,16 @@ import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User; import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.util.List;
/*
Commands
help - Displays help
list_tutels - Lists all registered Tutels
get_tutel - Get Tutel by label
stop_tutel - Stop Tutel by label
*/
@Component @Component
@RequiredArgsConstructor @RequiredArgsConstructor
@Getter @Getter
@ -33,27 +44,42 @@ public class TelegramBot extends TelegramLongPollingBot {
String receivedMessage = update.getMessage().getText(); String receivedMessage = update.getMessage().getText();
handeleMessage(fromUser, receivedMessage);
handleMessage(fromUser, receivedMessage);
} }
} }
private void handeleMessage(User fromUser, String receivedMessage) { private void handleMessage(User fromUser, String receivedMessage) {
Long userId = fromUser.getId(); Long userId = fromUser.getId();
if (receivedMessage.equals("/start")) { if (receivedMessage.equals("/start")) {
sendMessage(userId, "Your Telegram userId is " + userId); //check if user is already known
} else if (receivedMessage.equals("/list_tutels")) { if (telegramService.getTokens().containsKey(userId)) {
if (userId == 5334468232L) { sendMessage(userId, "You are already registered.");
sendMessage(userId, "user is Brulijam");
// send all tutels
sendMessage(userId, tutelService.getTutelList().toString());
} else { } else {
sendMessage(userId, "who are you?"); telegramService.addUserToList(fromUser);
String userToken = telegramService.getTokenByUserId(userId).replace("-", "");
sendMessage(userId, "Your Telegram userId is " + userId + " and your access token is " + userToken);
} }
} else if (receivedMessage.equals("/help")) {
sendMessage(userId, "TODO List of commands");
} else if (receivedMessage.equals("/list_tutels")) {
List<Tutel> tutels = tutelService.getTutelsForTelegramUser(userId);
sendMessage(userId, tutelService.tutelsToJson(tutels).toString());
} else if (receivedMessage.startsWith("/get_tutel_by_label ")) {
String label = receivedMessage.substring(receivedMessage.indexOf(" "));
Tutel tutel = tutelService.getTutelList().stream()
.filter(Tutel -> Tutel.getLabel().equals(label))
.findFirst()
.orElse(null);
} else { } else {
sendMessage(userId, "huh?"); sendMessage(userId, "I have no Idea what you want lol");
} }
} }

View File

@ -1 +0,0 @@
spring.application.name=minecraftapi

View File

@ -0,0 +1,15 @@
spring:
application:
name: minecraftapi
server:
port: 42030
servlet:
context-path: "/minecraftapi"
springdoc:
swagger-ui:
path: "/swagger-ui"
bot:
name: "Minecraft Bot"
token: ${TELEGRAM_BOT_TOKEN}