This repository has been archived on 2019-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
Maerahn/src/main/java/eu/mikroskeem/bot/maerahn/Utils.java

76 lines
2.9 KiB
Java

package eu.mikroskeem.bot.maerahn;
import org.telegram.telegrambots.TelegramApiException;
import org.telegram.telegrambots.api.methods.GetFile;
import org.telegram.telegrambots.api.objects.Chat;
import org.telegram.telegrambots.api.objects.File;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.User;
public class Utils {
public static String logMessage(Message message){
return getChatText(message);
}
private static String getChatText(Message message){
if(message.hasText()){
if(message.getEditDate() != null){
return String.format("(edited: %s) %s > %s",
message.getEditDate(),
getChatName(message),
message.getText());
} else {
return String.format("%s > %s",
getChatName(message),
message.getText());
}
} else {
return String.format("%s > %s",
getChatName(message),
getMessageType(message));
}
}
private static String getChatName(Message message){
Chat chat = message.getChat();
if(chat.isGroupChat() || chat.isSuperGroupChat() || chat.isChannelChat()){
return String.format("[%s] %s", getSender(message.getFrom()), chat.getTitle());
} else if (chat.isUserChat()){
return getSender(message.getFrom());
}
return "";
}
private static String getSender(User user){
String fn = user.getFirstName();
String ln = user.getLastName();
String un = user.getUserName();
return String.format("%s %s (%s)", fn, ln, un);
}
private static String getMessageType(Message message){
if(message.getSticker() != null){
/* Fetch sticker */
String fileId = message.getSticker().getFileId();
GetFile stickerFile = new GetFile();
stickerFile.setFileId(fileId);
String stickerPath;
try {
File file = Bot.getInstance().getFile(stickerFile);
stickerPath = file.getFilePath();
} catch (TelegramApiException e){
e.printStackTrace();
stickerPath = "<failed to fetch>";
}
return String.format("Sticker (%s)", stickerPath);
} else if(message.getAudio() != null){
return "(Audio file)";
} else if(message.getPhoto() != null){
return "(Picture file)";
} else if(message.getDocument() != null){
return "(Document file)";
} else if(message.getNewChatMember() != null){
return String.format("[New member %s]", getSender(message.getNewChatMember()));
} else if(message.getNewChatTitle() != null){
return String.format("[New chat title '%s']", message.getNewChatTitle());
}
return "(undefined)";
}
}