JMA - Java Mastodon API - is an API for developing Mastodon bots in Java and Kotlin
This library is a fork of the Mastodon4j library.
It has never been that easy to programm your own Mastodon bot.
Thanks to the Event-Listener-Approach, it is simple to reply to events.
Java
public static void main(String[] args) {
try {
MastodonClient client = new MastodonClient.Builder("your-instance.example", new OkHttpClient.Builder(), new Gson())
.accessToken("YOUR_TOKEN")
.addEventListener(new ExampleEventListener)
.build();
} catch (Mastodon4jRequestException exception) {
exception.printStackTrace();
}
}Kotlin
fun main(args: Array<String>) {
try {
val client = MastodonClient.Builder("your-instance.example", OkHttpClient.Builder(), Gson())
.accessToken("YOUR_TOKEN")
.addEventListener(ExampleEventListener())
.build()
} catch (exception: Mastodon4jRequestException) {
exception.printStackTrace()
}
}Java
public class FollowNotificationListener implements NotificationListener {
@Override
public void onFollowNotification(NotificationEvent event) {
MastodonClient client = event.getClient();
Notification notification = event.getNotification();
String user = notification.getAccount().getAcct();
String userAsMention = "@" + user;
String replyContent = "Welcome on board " + userAsMention + "!";
try {
new Statuses(client).postStatus(replyContent, null, null, false, null).execute();
} catch (Mastodon4jRequestException exception) {
exception.printStackTrace();
}
}
}All code and assets until and including the commit b7fbb565 are licensed under the MIT License.
The additional code and assets since then are licensed under the CC BY-NC-SA 4.0 License