创建 src/main/kotlin/Main.kt:
import io.ktor.http.Url
import love.forte.simbot.application.listeners
import love.forte.simbot.component.onebot.v11.core.useOneBot11
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotConfiguration
import love.forte.simbot.component.onebot.v11.core.bot.firstOneBotBotManager
import love.forte.simbot.core.application.launchSimpleApplication
import love.forte.simbot.event.ChatGroupMessageEvent
import love.forte.simbot.event.process
suspend fun main() {
val app = launchSimpleApplication { useOneBot11() }
try {
app.listeners {
process<ChatGroupMessageEvent> { event ->
if (event.messageContent.plainText?.trim() == "你好") {
println("收到群消息:你好")
event.reply("你也好")
}
}
}
val config = OneBotBotConfiguration().apply {
botUniqueId = System.getenv("ONEBOT_BOT_ID") ?: error("请设置 ONEBOT_BOT_ID")
apiServerHost = Url(System.getenv("ONEBOT_HTTP_URL") ?: error("请设置 ONEBOT_HTTP_URL"))
eventServerHost = Url(System.getenv("ONEBOT_WS_URL") ?: error("请设置 ONEBOT_WS_URL"))
accessToken(System.getenv("ONEBOT_ACCESS_TOKEN"))
}
val bot = app.botManagers.firstOneBotBotManager().register(config)
bot.start()
println("Bot 已启动,等待群消息")
app.join()
} finally {
app.cancel()
}
}
创建 src/main/java/Main.java 。在页面顶部选择 Java API 风格。示例统一以阻塞方式创建应用,使用所选风格启动 Bot、处理事件并等待应用结束:
import love.forte.simbot.application.Applications;
import love.forte.simbot.core.application.Simple;
import love.forte.simbot.event.EventListeners;
import love.forte.simbot.event.EventResult;
import love.forte.simbot.component.onebot.v11.core.component.OneBot11Component;
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotManagerUsageKt;
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotManager;
import love.forte.simbot.event.ChatGroupMessageEvent;
import io.ktor.http.URLUtilsKt;
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotConfiguration;
public class Main {
private static String required(String name) {
String value = System.getenv(name);
if (value == null || value.isBlank()) throw new IllegalArgumentException("请设置 " + name);
return value;
}
public static void main(String[] args) {
var app = Applications.launchApplicationBlocking(Simple.INSTANCE, config -> {
config.install(OneBot11Component.Factory);
config.install(OneBotBotManager.Factory);
});
try {
app.getEventDispatcher().register(EventListeners.block(ChatGroupMessageEvent.class, (context, event) -> {
var text = event.getMessageContent().getPlainText();
if (text == null || !text.trim().equals("你好")) {
return EventResult.empty();
}
System.out.println("收到群消息:你好");
event.replyBlocking("你也好");
return EventResult.empty();
}));
var config = new OneBotBotConfiguration();
config.setBotUniqueId(required("ONEBOT_BOT_ID"));
config.setApiServerHost(URLUtilsKt.Url(required("ONEBOT_HTTP_URL")));
config.setEventServerHost(URLUtilsKt.Url(required("ONEBOT_WS_URL")));
config.accessToken(System.getenv("ONEBOT_ACCESS_TOKEN"));
var bot = OneBotBotManagerUsageKt.firstOneBotBotManager(app.getBotManagers()).register(config);
bot.startBlocking();
System.out.println("Bot 已启动,等待群消息");
app.joinBlocking();
} finally {
app.cancel();
}
}
}
import love.forte.simbot.application.Applications;
import love.forte.simbot.core.application.Simple;
import love.forte.simbot.event.EventListeners;
import love.forte.simbot.event.EventResult;
import love.forte.simbot.component.onebot.v11.core.component.OneBot11Component;
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotManagerUsageKt;
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotManager;
import love.forte.simbot.event.ChatGroupMessageEvent;
import io.ktor.http.URLUtilsKt;
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotConfiguration;
import java.util.concurrent.CompletableFuture;
public class Main {
private static String required(String name) {
String value = System.getenv(name);
if (value == null || value.isBlank()) throw new IllegalArgumentException("请设置 " + name);
return value;
}
public static void main(String[] args) {
var app = Applications.launchApplicationBlocking(Simple.INSTANCE, config -> {
config.install(OneBot11Component.Factory);
config.install(OneBotBotManager.Factory);
});
try {
app.getEventDispatcher().register(EventListeners.async(ChatGroupMessageEvent.class, (context, event) -> {
var text = event.getMessageContent().getPlainText();
if (text == null || !text.trim().equals("你好")) {
return CompletableFuture.completedFuture(EventResult.empty());
}
System.out.println("收到群消息:你好");
return event.replyAsync("你也好").thenApply(ignored -> EventResult.empty());
}));
var config = new OneBotBotConfiguration();
config.setBotUniqueId(required("ONEBOT_BOT_ID"));
config.setApiServerHost(URLUtilsKt.Url(required("ONEBOT_HTTP_URL")));
config.setEventServerHost(URLUtilsKt.Url(required("ONEBOT_WS_URL")));
config.accessToken(System.getenv("ONEBOT_ACCESS_TOKEN"));
var bot = OneBotBotManagerUsageKt.firstOneBotBotManager(app.getBotManagers()).register(config);
bot.startAsync()
.thenRun(() -> System.out.println("Bot 已启动,等待群消息"))
.thenCompose(ignored -> app.asFuture())
.join();
} finally {
app.cancel();
}
}
}
import love.forte.simbot.application.Applications;
import love.forte.simbot.core.application.Simple;
import love.forte.simbot.event.EventListeners;
import love.forte.simbot.event.EventResult;
import love.forte.simbot.component.onebot.v11.core.component.OneBot11Component;
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotManagerUsageKt;
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotManager;
import love.forte.simbot.event.ChatGroupMessageEvent;
import io.ktor.http.URLUtilsKt;
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBotConfiguration;
import reactor.core.publisher.Mono;
public class Main {
private static String required(String name) {
String value = System.getenv(name);
if (value == null || value.isBlank()) throw new IllegalArgumentException("请设置 " + name);
return value;
}
public static void main(String[] args) {
var app = Applications.launchApplicationBlocking(Simple.INSTANCE, config -> {
config.install(OneBot11Component.Factory);
config.install(OneBotBotManager.Factory);
});
try {
app.getEventDispatcher().register(EventListeners.nonBlock(ChatGroupMessageEvent.class, (context, event) -> {
var text = event.getMessageContent().getPlainText();
if (text == null || !text.trim().equals("你好")) {
return EventResult.empty();
}
System.out.println("收到群消息:你好");
return EventResult.of(Mono.from(event.replyReactive("你也好")));
}));
var config = new OneBotBotConfiguration();
config.setBotUniqueId(required("ONEBOT_BOT_ID"));
config.setApiServerHost(URLUtilsKt.Url(required("ONEBOT_HTTP_URL")));
config.setEventServerHost(URLUtilsKt.Url(required("ONEBOT_WS_URL")));
config.accessToken(System.getenv("ONEBOT_ACCESS_TOKEN"));
var bot = OneBotBotManagerUsageKt.firstOneBotBotManager(app.getBotManagers()).register(config);
Mono.from(bot.startReactive())
.doOnSuccess(ignored -> System.out.println("Bot 已启动,等待群消息"))
.then(Mono.from(app.joinReactive()))
.block();
} finally {
app.cancel();
}
}
}