Skip to content

Getting Started

Michi Palazzo edited this page May 1, 2020 · 16 revisions

Obtain a Bot Token

In order to create your bot, you need to talk to @BotFather on Telegram and follow the given instructions. Once you've created a bot you'll receive your authorization token, it looks like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11.

Add the library

Now that you have the token you need to get the library and add it to your project. Import the library .jar with-dependencies direclty to your project, you can find it here. You can also use maven or gradle:

Maven

<repository>
  <id>jitpack.io</id>
  <url>https://jitpack.io</url>
</repository>

<dependency> 
  <groupId>com.github.AgeOfWar</groupId> 
  <artifactId>Telejam</artifactId>
  <version>v7.15</version>
</dependency>

Gradle

repositories {
  maven { url 'https://jitpack.io' }
}

dependencies {
  compile 'com.github.AgeOfWar:Telejam:v7.15'
}

First Bot

Now that we have the library, we can start to write the code. First of all, we need to create a class that extends LongPollingBot:

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

public class MyFirstBot extends LongPollingBot {
  public MyFirstBot(Bot bot) {
    super(bot);
  }
}

To handle updates received from the bot you need to register an event by calling one of the register methods of the events field of LongPollingBot:

public class MyFirstBot extends LongPollingBot {
  public MyFirstBot(Bot bot) {
    super(bot);
    events.registerUpdateHandler(...);
  }
}

In the example above we register an event that accepts a TextMessage. Now let's write the event!

import io.github.ageofwar.telejam.Bot;
import io.github.ageofwar.telejam.messages.TextMessage;
import io.github.ageofwar.telejam.messages.TextMessageHandler;

public class MessageReceiver implements TextMessageHandler {
  private final Bot bot;

  public MessageReceiver(Bot bot) {
    this.bot = bot;
  }

  @Override
  public void onTextMessage(TextMessage message) throws Throwable {
    SendMessage sendMessage = new SendMessage()
        .replyToMessage(message)
        .text(Text.bold("Message received!"));
    bot.execute(sendMessage);
  }
}
public class MyFirstBot extends LongPollingBot {
  public MyFirstBot(Bot bot) {
    super(bot);
    events.registerUpdateHandler(new MessageReceiver(bot));
  }
}

When a text message is received the bot will reply "Message received!".

The bot will only receive messages from private chats, commands and messages mentioning your bot directly. Disable the privacy mode via @BotFather to receive all messages.

Now let's write the main!

public static void main(String[] args) throws IOException {
  // It's preferable to store the token in a more
  // secure way, in a config file for example.
  Bot bot = Bot.fromToken("<bot token>");
  MyFirstBot firstBot = new MyFirstBot(bot);
  firstBot.run();
}

That's all, this is the result:

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

public class MyFirstBot extends LongPollingBot {
  public static void main(String[] args) throws IOException {
    // It's preferable to store the token in a more
    // secure way, in a config file for example.
    Bot bot = Bot.fromToken("<bot token>");
    MyFirstBot firstBot = new MyFirstBot(bot);
    firstBot.run();
  }

  public MyFirstBot(Bot bot) {
    super(bot);
    events.registerUpdateHandler(new MessageReceiver(bot));
  }
}
import io.github.ageofwar.telejam.Bot;
import io.github.ageofwar.telejam.messages.TextMessage;
import io.github.ageofwar.telejam.messages.TextMessageHandler;

public class MessageReceiver implements TextMessageHandler {
  private final Bot bot;

  public MessageReceiver(Bot bot) {
    this.bot = bot;
  }

  @Override
  public void onTextMessage(TextMessage message) throws Throwable {
    SendMessage sendMessage = new SendMessage()
        .replyToMessage(message)
        .text(Text.bold("Message received!"));
    bot.execute(sendMessage);
  }
}

Clone this wiki locally