Skip to content

wyattwas/JMA

 
 

Repository files navigation

JMA

JMA - Java Mastodon API - is an API for developing Mastodon bots in Java and Kotlin

This library is a fork of the Mastodon4j library.

Usage

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.

Installation

Creating a bot

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()
    }
}

Creating a Listener

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();
        }
    }
}

License

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

About

mastodon bot client for Java and Kotlin

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Kotlin 100.0%