Skip to content

Developer API

MasterCake edited this page May 4, 2021 · 3 revisions

Since version 1.0.9.9, TelegramChat offers an API for sending messages from Minecraft to Telegram and vice versa.

Maven

<repositories>
	<repository>
		<id>spaceio-repo</id>
		<url>https://repo.spaceio.xyz/repository/maven-public/</url>
	</repository>
</repositories>
<dependencies>
	<dependency>
		<groupId>xyz.spaceio</groupId>
		<artifactId>telegramchat</artifactId>
		<version>latest</version>
	</dependency>
</dependencies>

Sending messages

Before using the API of TelegramChat, you need to obtain the Telegram hook:

Telegram telegram = de.Linus122.TelegramChat.API.getTelegramHook();

In order to send a message, you need to create a chat instance:

de.Linus122.TelegramComponents.Chat chat = new de.Linus122.TelegramComponents.Chat();
chat.text = "Hello Telegram!";
chat.parse_mode = "Markdown"; // default value

// sending a message to all Telegram users
telegram.sendAll(chat);

de.Linus122.TelegramComponents.Chat chat2 = new de.Linus122.TelegramComponents.Chat();
chat2.text = "Hello Telegram!";
chat2.parse_mode = "Markdown";
chat2.chat_id = 12;

// sending a message to a certain chat (ID 12)
telegram.sendMsg(chat2);

Listing to messages and manipulate them

TelegramChat offers a build in event listener, that could be used using the following example class:

import de.Linus122.TelegramChat.TelegramActionListener;
import de.Linus122.TelegramComponents.Chat;
import de.Linus122.TelegramComponents.ChatMessageToMc;

public class TelegramListener implements TelegramActionListener {

   @Override
   public void onSendToMinecraft(ChatMessageToMc msg) {
       if (msg.text.contains("swearword")) {
            msg.setCancelled();
       }
   }

   @Override
   public void onSendToTelegram(ChatMessageToTelegram msg) {
       if (msg.content.contains("swearword")) {
            msg.setCancelled();
       }  
   }

}

Your class TelegramListener needs to be registered before using:

telegram.addListener(new TelegramListener());

The instances of Chat and ChatMessageToMc are mutable.