Initial commit

This commit is contained in:
Mark Vainomaa 2016-09-24 21:00:30 +03:00
commit 0f49dc2228
9 changed files with 353 additions and 0 deletions

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
# ---> Java
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# IntelliJ IDEA
*.iml
.idea/
# Maven
target/
dependency-reduced-pom.xml

6
README.md Normal file
View File

@ -0,0 +1,6 @@
# Mäerahn
Telegram bot written in Java. See https://telegram.me/maerahnu\_bot
## Configuration
Put bot token and it's username into corresponding environment variables `TG_TOKEN` and `TG_BOTNAME`

106
pom.xml Normal file
View File

@ -0,0 +1,106 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>eu.mikroskeem.bot</groupId>
<artifactId>maerahn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<jdk.version>1.8</jdk.version>
<jar.mainclass>eu.mikroskeem.bot.maerahn.Bootstrap</jar.mainclass>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<!-- It's uber jar! -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<!-- It's executable jar! -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>${jar.mainclass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
<defaultGoal>clean compile package</defaultGoal>
</build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
<scope>provided</scope>
</dependency>
<!-- Telegram Bot base -->
<dependency>
<groupId>com.github.rubenlagus</groupId>
<artifactId>TelegramBots</artifactId>
<version>v2.3.5</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,22 @@
package eu.mikroskeem.bot.maerahn;
import org.telegram.telegrambots.TelegramApiException;
import org.telegram.telegrambots.TelegramBotsApi;
public class Bootstrap {
private TelegramBotsApi telegramBotsApi;
private Bootstrap(){
/* Initialize bot base */
telegramBotsApi = new TelegramBotsApi();
try {
telegramBotsApi.registerBot(new Bot());
} catch (TelegramApiException e){
e.printStackTrace();
}
}
public static void main(String... args){
new Bootstrap();
}
}

View File

@ -0,0 +1,42 @@
package eu.mikroskeem.bot.maerahn;
import eu.mikroskeem.bot.maerahn.commands.LennyCommand;
import eu.mikroskeem.bot.maerahn.commands.ShrugCommand;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingCommandBot;
public class Bot extends TelegramLongPollingCommandBot {
public Logger logger;
@Getter private static Bot instance;
Bot(){
instance = this;
this.logger = LoggerFactory.getLogger("Mäerahn");
/* Register commands */
//register(new FacepalmCommand(this));
register(new ShrugCommand(this));
register(new LennyCommand(this));
registerDefaultAction((sender, message)->{
logger.info(Utils.logMessage(message));
});
logger.info("Bot is ready");
}
@Override public void processNonCommandUpdate(Update update) {
if(update.hasMessage()){
logger.info(Utils.logMessage(update.getMessage()));
} else if(update.hasEditedMessage()){
logger.info(Utils.logMessage(update.getEditedMessage()));
}
}
@Override public String getBotToken() {
return System.getenv("TG_TOKEN");
}
@Override public String getBotUsername() {
return System.getenv("TG_BOTNAME");
}
}

View File

@ -0,0 +1,76 @@
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)";
}
}

View File

@ -0,0 +1,21 @@
package eu.mikroskeem.bot.maerahn.commands;
import eu.mikroskeem.bot.maerahn.Bot;
import org.slf4j.Logger;
import org.telegram.telegrambots.api.methods.send.SendSticker;
import org.telegram.telegrambots.api.objects.Chat;
import org.telegram.telegrambots.api.objects.User;
import org.telegram.telegrambots.bots.AbsSender;
import org.telegram.telegrambots.bots.commands.BotCommand;
public class FacepalmCommand extends BotCommand {
Logger logger;
public FacepalmCommand(Bot bot){
super("facepalm", "Send facepalm sticker");
logger = bot.logger;
}
@Override public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
//SendSticker sticker = new SendSticker();
//sticker.set
}
}

View File

@ -0,0 +1,30 @@
package eu.mikroskeem.bot.maerahn.commands;
import eu.mikroskeem.bot.maerahn.Bot;
import org.slf4j.Logger;
import org.telegram.telegrambots.TelegramApiException;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Chat;
import org.telegram.telegrambots.api.objects.User;
import org.telegram.telegrambots.bots.AbsSender;
import org.telegram.telegrambots.bots.commands.BotCommand;
public class LennyCommand extends BotCommand {
Logger logger;
public LennyCommand(Bot bot){
super("lenny", "Send Lenny Face");
logger = bot.logger;
}
@Override public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
SendMessage message = new SendMessage();
message.setChatId(chat.getId().toString());
message.setText("( ͡° ͜ʖ ͡°)");
try {
absSender.sendMessage(message);
} catch (TelegramApiException e){
logger.error("Failed to send reply for /lenny command!");
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,29 @@
package eu.mikroskeem.bot.maerahn.commands;
import eu.mikroskeem.bot.maerahn.Bot;
import org.slf4j.Logger;
import org.telegram.telegrambots.TelegramApiException;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Chat;
import org.telegram.telegrambots.api.objects.User;
import org.telegram.telegrambots.bots.AbsSender;
import org.telegram.telegrambots.bots.commands.BotCommand;
public class ShrugCommand extends BotCommand {
Logger logger;
public ShrugCommand(Bot bot){
super("shrug", "Send shrug");
logger = bot.logger;
}
@Override public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
SendMessage message = new SendMessage();
message.setChatId(chat.getId().toString());
message.setText("¯\\_(ツ)_/¯");
try {
absSender.sendMessage(message);
} catch (TelegramApiException e){
logger.error("Failed to send reply for /shrug command!");
e.printStackTrace();
}
}
}