Skip to content

Commands

Michi Palazzo edited this page May 19, 2019 · 9 revisions

Commands

Commands in Telejam are events, too.

Here there is an example:

import io.github.ageofwar.telejam.LongPollingBot;
import io.github.ageofwar.telejam.Bot;

public class HelloBot extends LongPollingBot {
  public HelloBot(Bot bot) {
    super(bot);
    events.registerCommand(
      new HelloCommand(bot), "hello", "helloworld"
    );
  }
}
import io.github.ageofwar.telejam.Bot;
import io.github.ageofwar.telejam.methods.SendMessage;
import io.github.ageofwar.telejam.messages.TextMessage;
import io.github.ageofwar.telejam.commands.Command;
import io.github.ageofwar.telejam.commands.CommandHandler;

public class HelloCommand implements CommandHandler {
  private final Bot bot;
  
  public HelloCommand(Bot bot) {
    this.bot = bot;
  }
  
  @Override
  public void onCommand(Command command, TextMessage message) throws Throwable {
    SendMessage sendMessage = new SendMessage()
        .replyToMessage(message)
        .text("Hello world!");
    bot.execute(sendMessage);
  }
}

A new HelloCommand is registered in events, with names "hello" and "helloworld". Now when an user sends /hello or /helloworld, the bot will reply Hello World!!

Clone this wiki locally