Generic Bot framework for the Stack Exchange chat written in Java.
repositories {
mavenCentral()
maven {
url 'https://raw.githubusercontent.com/Floern/generic-bot-frame/repo/'
}
}
dependencies {
compile 'com.floern:genericbot-frame:0.9.8'
}
Configuration file of the bot.
# bot account data
chat.usermail = [email protected]
chat.userpass = correcthorsebatterystaple
chat.username = My Bot
# chat room ID for development
chat.devroomid = 12345
# other chat rooms to connect to (comma separated)
chat.roomids = 12345
# bot admins, user ID (comma separated)
bot.admins = 123456
# redunda support
redunda.enabled = false
redunda.apikey = myapikeyifenabled
# bot's location (format: OwnerName/ComputerName)
info.location = MyName/RaspberryPi
The main class of the program.
public class Main {
public static void main(String[] rawrgs) throws InterruptedException {
new ExampleBot().start().waitForTermination();
System.exit(0);
}
}
A simple bot that loads new comments from Stack Overflow and posts them to chat if they match a pattern.
public class ExampleBot extends GenericBot {
// comment service
private CommentsLoaderService commentsLoader;
public GenericBot() {
super(ProgramProperties.load("data/bot.properties"));
StatusCommand statusCommand = new StatusCommand();
statusCommand
.registerStatusRecordCallback(new StatusCommand.UptimeStatusRecordCallback())
.registerStatusRecordCallback(new StatusCommand.LocationStatusRecordCallback(getProgramProperties()));
registerCommand(new AliveCommand());
registerCommand(new CommandsCommand());
registerCommand(new TerminateCommand());
registerCommand(new TheTrainCommand());
registerCommand(statusCommand);
// setup service to load new comments from Stack Overflow
commentsLoader = new CommentsLoaderService(this);
// provide API quota information to the status command
statusCommand.registerStatusRecordCallback(() ->
MapUtil.createSingle("api quota", ApiLoader.getQuotaRemaining() + " (max " + ApiLoader.getQuotaMax() + ")"));
// setup comment listener
commentsLoader.addOnCommentLoadedListener(new CommentsLoaderService.OnCommentLoadedListener() {
public void onCommentLoaded(Comment comment) {
getChatManager().getDevChatRoom()
.send("https://stackoverflow.com/posts/comments/" + comment.getCommentId());
}
});
}
@Override
protected void onAllConnected() {
// send a message
getChatManager().getDevChatRoom().send("hello world!");
// start the comment service
ScheduledTaskExecutor.scheduleTask(commentsLoader::start, 0);
}
@Override
protected void onShutdown() {
// stop the comment service
commentsLoader.stop();
ScheduledTaskExecutor.cancelAll();
}
}