ScalaBot/scalabot-ext-example/src/main/java/net/lamgc/scalabot/simple/SayHelloExtension.java
LamGC 3e99d7d033
fix(example): 修复 Reply 判断条件不充分的问题.
由于条件判断不充分, 导致运行时可能会出现 NPE 的问题.
2022-04-20 16:35:02 +08:00

64 lines
2.3 KiB
Java

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 -> {
String msg = "Hello! " + ctx.user().getUserName() +
" ( " + ctx.user().getId() + " ) [ " + ctx.user().getLanguageCode() + " ]" + "\n" +
"Current Chat ID: " + ctx.chatId();
ctx.bot().silent().send(msg, ctx.chatId());
})
.build();
}
/**
* 更具特色的 `Say hello`.
*/
public Ability test() {
ReplyFlow botHello = ReplyFlow.builder(bot.db())
.enableStats("say_hello")
.action((bot, upd) -> bot.silent().send("What is u name?", upd.getMessage().getChatId()))
.onlyIf(update -> update.hasMessage()
&& update.getMessage().hasText()
&& "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.hasMessage()
&& upd.getMessage().hasText()
&& 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();
}
}