did a lot of stuff

This commit is contained in:
Julian Brammer 2024-07-02 18:02:02 +02:00
parent f61e9205ec
commit 56825a5c3e
8 changed files with 79 additions and 24 deletions

View File

@ -19,14 +19,27 @@ public class TelegramBotController {
private final TelegramService telegramService; private final TelegramService telegramService;
private final TelegramBot telegramBot; private final TelegramBot telegramBot;
@Operation(summary = "sends a message") @Operation(summary = "sends a message debug")
@PostMapping(value = "/sendMessage/{userId}", produces = "application/json") @PostMapping(value = "/debugSendMessage/{userId}", produces = "application/json")
public void sendMessage(@PathVariable(name = "userId") long userId, public void debugSendMessage(@PathVariable(name = "userId") long userId,
@Parameter(name = "message", description = "message to send", example = "Some Message :)") @RequestBody String message) { @Parameter(name = "message", description = "message to send", example = "Some Message :)") @RequestBody String message) {
log.info("{}", userId); log.info("{}", userId);
telegramBot.sendMessage(userId, message); telegramBot.sendMessage(userId, message);
} }
@Operation(summary = "sends a message")
@PostMapping(value = "/sendMessage/{userId}", produces = "application/json")
public void sendMessage(@PathVariable(name = "userId") long userId,
@Parameter(name = "message", description = "message to send", example = "Some Message :)") @RequestBody String message,
@Parameter(name = "accessToken", description = "Required token for sending messages to this user", example = "abcde12345") @RequestParam String accessToken) {
log.info("{}", userId);
if (accessToken.equals(telegramService.getTokenByUserId(userId))) {
telegramBot.sendMessage(userId, message);
}
}
@Operation(summary = "For debugging") @Operation(summary = "For debugging")
@PostMapping(value = "/admin", produces = "application/json") @PostMapping(value = "/admin", produces = "application/json")
public String admin() { public String admin() {

View File

@ -22,6 +22,7 @@ public class Tutel {
private final long telegramUserId; private final long telegramUserId;
private final String label; private final String label;
private int fuelLevel; private int fuelLevel;
private String task;
private JSONObject inventory; private JSONObject inventory;
@ -33,5 +34,7 @@ public class Tutel {
this.label = label; this.label = label;
this.id = Math.abs(this.hashCode()); this.id = Math.abs(this.hashCode());
this.telegramUserId = telegramUserId; this.telegramUserId = telegramUserId;
this.fuelLevel = 0;
this.task = "";
} }
} }

View File

@ -0,0 +1,4 @@
package com.brulijam.minecraftapi.porter;
public class exporter {
}

View File

@ -0,0 +1,4 @@
package com.brulijam.minecraftapi.porter;
public class importer {
}

View File

@ -2,6 +2,7 @@ package com.brulijam.minecraftapi.service;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;
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;
@ -14,17 +15,18 @@ public class TelegramService {
private final List<User> telegramUsers = new ArrayList<>(); private final List<User> telegramUsers = new ArrayList<>();
// Maps userId to token // Maps userId to token
private final Map<Long, String> tokens = new HashMap<>(); private final Map<Long, String> accessTokens = new HashMap<>();
public void addUserToList(User user) { public void addUserToList(User user) {
telegramUsers.add(user); telegramUsers.add(user);
String randomUUID = UUID.randomUUID().toString().replaceAll("_", ""); // String randomString = UUID.randomUUID().toString().replaceAll("_", "").replace("-", "");
tokens.put(user.getId(), randomUUID); String randomString = RandomStringUtils.randomAlphanumeric(32);
accessTokens.put(user.getId(), randomString);
} }
public String getTokenByUserId(Long userId) { public String getTokenByUserId(Long userId) {
return tokens.get(userId); return accessTokens.get(userId);
} }
public User getUserByUserId(long userId) { public User getUserByUserId(long userId) {
@ -42,6 +44,6 @@ public class TelegramService {
} }
public String getTokenByUserId(long userId) { public String getTokenByUserId(long userId) {
return tokens.get(userId); return accessTokens.get(userId);
} }
} }

View File

@ -1,6 +1,7 @@
package com.brulijam.minecraftapi.service; package com.brulijam.minecraftapi.service;
import com.brulijam.minecraftapi.model.Tutel; import com.brulijam.minecraftapi.model.Tutel;
import com.brulijam.minecraftapi.telegrambot.TelegramBot;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.json.JSONArray; import org.json.JSONArray;
@ -16,9 +17,13 @@ import java.util.List;
public class TutelService { public class TutelService {
private final List<Tutel> tutelList = new ArrayList<>(); private final List<Tutel> tutelList = new ArrayList<>();
private final TelegramBot telegramBot; // cycle :(
public void addToList(Tutel tutel) { public void addToList(Tutel tutel) {
if (!tutelList.contains(tutel)) {
tutelList.add(tutel); tutelList.add(tutel);
telegramBot.sendMessage(tutel.getTelegramUserId(), tutel.getLabel().replace("-", "\\-"));
}
} }
public void removeById(long id) { public void removeById(long id) {

View File

@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update; 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;
@ -18,9 +19,11 @@ import java.util.List;
/* /*
Commands Commands
help - Displays help help - Displays help
list_tutels - Lists all registered Tutels listtutels - Lists all registered Tutels
get_tutel - Get Tutel by label gettutel - Get Tutel by label
stop_tutel - Stop Tutel by label stoptutel - Stop Tutel by label
listchests - Get all placed Chests
getchest - Get Chest by ID
*/ */
@Component @Component
@ -30,6 +33,7 @@ public class TelegramBot extends TelegramLongPollingBot {
private final TutelService tutelService; private final TutelService tutelService;
private final TelegramService telegramService; private final TelegramService telegramService;
private final TelegramFormatter telegramFormatter;
@Value("${bot.name}") @Value("${bot.name}")
private String botUsername; private String botUsername;
@ -55,29 +59,33 @@ public class TelegramBot extends TelegramLongPollingBot {
Long userId = fromUser.getId(); Long userId = fromUser.getId();
if (receivedMessage.equals("/start")) { if (receivedMessage.equals("/start")) {
//check if user is already known if (!(telegramService.getAccessTokens().containsKey(userId))) {
if (telegramService.getTokens().containsKey(userId)) {
sendMessage(userId, "You are already registered.");
} else {
telegramService.addUserToList(fromUser); telegramService.addUserToList(fromUser);
String userToken = telegramService.getTokenByUserId(userId).replace("-", "");
sendMessage(userId, "Your Telegram userId is " + userId + " and your access token is " + userToken);
} }
String accessToken = telegramService.getTokenByUserId(userId);
sendMessage(userId, "Your Telegram UserID is `" + userId + "` and your accessToken is ||" + accessToken + "||");
} else if (receivedMessage.equals("/help")) { } else if (receivedMessage.equals("/help")) {
sendMessage(userId, "TODO List of commands"); sendMessage(userId, "/start \\- Description\n/help -\\ This help\n/list_tutels \\- Description\n/get_tutel_by_label \\- Description\n/placeholder \\- Description\n/placeholder \\- Description\n");
} else if (receivedMessage.equals("/list_tutels")) { } else if (receivedMessage.equals("/listtutels")) {
List<Tutel> tutels = tutelService.getTutelsForTelegramUser(userId); List<Tutel> tutels = tutelService.getTutelsForTelegramUser(userId);
sendMessage(userId, tutelService.tutelsToJson(tutels).toString()); tutels.forEach(tutel -> sendMessage(userId, telegramFormatter.formatTutel(tutel)));
} else if (receivedMessage.startsWith("/get_tutel_by_label ")) { // sendMessage(userId, tutelService.tutelsToJson(tutels).toString());
String label = receivedMessage.substring(receivedMessage.indexOf(" "));
} else if (receivedMessage.startsWith("/gettutel ")) {
String label = receivedMessage.substring(receivedMessage.indexOf(" ")+1);
System.out.println(label);
Tutel tutel = tutelService.getTutelList().stream() Tutel tutel = tutelService.getTutelList().stream()
.filter(Tutel -> Tutel.getLabel().equals(label)) .filter(Tutel -> Tutel.getLabel().equals(label))
.findFirst() .findFirst()
.orElse(null); .orElse(null);
if (tutel != null) {
sendMessage(userId, telegramFormatter.formatTutel(tutel));
} else {
sendMessage(userId, "Tutel not found");
}
} else { } else {
sendMessage(userId, "I have no Idea what you want lol"); sendMessage(userId, "I have no Idea what you want lol");
} }
@ -96,6 +104,7 @@ public class TelegramBot extends TelegramLongPollingBot {
public void sendMessage(Long chatId, String messageToSend) { public void sendMessage(Long chatId, String messageToSend) {
SendMessage sendMessage = SendMessage.builder() SendMessage sendMessage = SendMessage.builder()
.chatId(chatId.toString()) .chatId(chatId.toString())
.parseMode("MarkdownV2")
.text(messageToSend) .text(messageToSend)
.build(); .build();

View File

@ -0,0 +1,15 @@
package com.brulijam.minecraftapi.telegrambot;
import com.brulijam.minecraftapi.model.Tutel;
import org.springframework.stereotype.Component;
@Component
public class TelegramFormatter {
public String formatTutel(Tutel tutel) {
return "__`" + tutel.getLabel().replace("-", "\\-") + "`__" + "\n"
+ "ID: `" + tutel.getId() + "`\n"
+ "Fuel: " + tutel.getFuelLevel() + "\n"
+ "Task: " + tutel.getTask() + "\n";
}
}