-
Notifications
You must be signed in to change notification settings - Fork 0
Add EventHandlers
pharastar.dev edited this page Nov 24, 2022
·
2 revisions
The error event runs if there is an error on making requests in polling or webhooks
import tech.digitaldojo.jtba.TelegramBot;
import tech.digitaldojo.jtba.events.TelegramEvents;
class Showcase {
private TelegramBot bot;
private void anything() {
bot.on(TelegramEvents.error, System.out::println);
}
}
This event runs if the bot is able to make API requests.
import tech.digitaldojo.jtba.TelegramBot;
import tech.digitaldojo.jtba.events.TelegramEvents;
class Showcase {
private TelegramBot bot;
private void anything() {
bot.on(TelegramEvents.ready, (user) -> {
System.out.println("Bot Ready: " + user);
});
}
}
The text event runs if the message has any text set to the message object
import tech.digitaldojo.jtba.TelegramBot;
import tech.digitaldojo.jtba.events.MessageEvents;
class Showcase {
private TelegramBot bot;
private void anything() {
bot.on(MessageEvents.text, (message) -> {
System.out.println("Received Text: " + user);
String txt = message.text;
if (txt.startsWith("-echo ")) {
txt = txt.replaceFirst("-echo ", "");
message.chat.send(txt);
message.replyMarkdown("```txt\n" + txt + "\n```");
}
});
}
}
import tech.digitaldojo.jtba.TelegramBot;
import tech.digitaldojo.jtba.data.types.Message;
import tech.digitaldojo.jtba.events.MessageEvents;
import tech.digitaldojo.jtba.events.TelegramEvents;
import java.util.function.Consumer;
class Showcase {
private TelegramBot bot;
private void anything() {
bot.on(TelegramEvents.message, Showcase::onMessage);
bot.on(MessageEvents.text, new MyMessageConsumer(bot));
}
public void onMessage(Message message) {
// whatever
}
class MyMessageConsumer extends Consumer<Message> {
private final TelegramBot bot;
public MyMessageConsumer(TelegramBot bot) {
this.bot = bot;
}
@Override
public void accept(final Message message) {
System.out.println("Message: " + message);
}
}
}