initial: 基本完成的首个版本, 还需要调整一下.

暂时按照当初的计划实现了一个可用版本出来, 发布与否晚些再确定.
This commit is contained in:
2022-01-16 20:21:18 +08:00
parent cbaeb2ce00
commit 37f0d4e6b8
28 changed files with 1513 additions and 0 deletions

View File

@ -0,0 +1,18 @@
plugins {
java
}
repositories {
mavenCentral()
}
dependencies {
compileOnly(project(":scalabot-extension"))
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}
tasks.getByName<Test>("test") {
useJUnitPlatform()
}

View File

@ -0,0 +1,53 @@
package net.lamgc.scalabot.simple;
import org.telegram.abilitybots.api.bot.BaseAbilityBot;
import org.telegram.abilitybots.api.objects.*;
import org.telegram.abilitybots.api.util.AbilityExtension;
public class SayHelloExtension implements AbilityExtension {
/**
* 扩展所属的机器人对象.
*
* <p> 创建 ReplyFlow 时需要使用 Bot 的 DBContext.
*/
private final BaseAbilityBot bot;
public SayHelloExtension(BaseAbilityBot bot) {
this.bot = bot;
}
public Ability sayHello() {
return Ability.builder()
.name("say_hello")
.info("Say hello to you.")
.privacy(Privacy.PUBLIC)
.locality(Locality.ALL)
.action(ctx -> ctx.bot().silent().send("Hello! " + ctx.user().getUserName(), ctx.chatId()))
.build();
}
/**
* 更具特色的 `Say hello`.
*/
public Ability test() {
ReplyFlow botHello = ReplyFlow.builder(bot.db())
.action((bot, upd) -> bot.silent().send("What is u name?", upd.getMessage().getChatId()))
.onlyIf(update -> "hello".equalsIgnoreCase(update.getMessage().getText()))
.next(Reply.of((bot, upd) -> bot.silent()
.send("OK! You name is " + upd.getMessage().getText().substring("my name is ".length()), upd.getMessage().getChatId()),
upd -> upd.getMessage().getText().startsWith("my name is ")))
.build();
return Ability.builder()
.name("hello")
.info("Say hello!")
.locality(Locality.ALL)
.privacy(Privacy.PUBLIC)
.enableStats()
.action(ctx -> ctx.bot().silent().send("Hello!", ctx.chatId()))
.reply(botHello)
.build();
}
}

View File

@ -0,0 +1,16 @@
package net.lamgc.scalabot.simple;
import net.lamgc.scalabot.extension.BotExtensionFactory;
import org.telegram.abilitybots.api.bot.BaseAbilityBot;
import org.telegram.abilitybots.api.util.AbilityExtension;
import java.io.File;
public class SimpleExtensionFactory implements BotExtensionFactory {
@Override
public AbilityExtension createExtensionInstance(BaseAbilityBot bot, File shareDataFolder) {
return new SayHelloExtension(bot);
}
}

View File

@ -0,0 +1 @@
net.lamgc.scalabot.simple.SimpleExtensionFactory