Skip to content
Rahil khan edited this page May 2, 2022 · 4 revisions

EasyInsta library guide

After this guide, you will never be required to see the documentation of this library. This guide will explain everything step-by-step with examples. So let's begin with the index

Table of contents

It would be best if you see JavaDocs first before diving into the guide. Anyway, let's get started

Login

There are 2 ways of logging in. One using credentials and another using cache

Login with credentials

This will always create a new login session (means that it will attempt to login every time the code run)

Instagram instagram = Instagram.login("username","password");

Login using cache

You would not like to log in every time users run your program. After login, you can cache that Instagram instance and can use it every time you create an Instagram object. See this example:

Instagram instagram = Instagram.loginOrCache(new File("path/to/cache/directory"),"username","password");

This method will take care of the cache. If it does not exist, it first logs in using the username and password and then creates a cache. If already exist, return the old Instagram instance without performing a login. There is also clearCache() method to delete the old login sessions.

Basic actions

All the basic actions methods are present in the Instagram.Actions class. Let's see how to use them.

Instagram.Actions actions = instagram.actions();
actions.follow("x__coder__x")
        .setOnSuccessCallback((v) -> System.out.println("Followed successfully"))
        .setOnErrorCallback((e) -> System.out.println("Failed to follow"));
        
actions.unfollow("x__coder__x")
        .setOnSuccessCallback((v) -> System.out.println("Unfollowed successfully"))
        .setOnErrorCallback((e) -> System.out.println("Failed to Unfollow"));

Direct actions

All the basic action methods are present in the Instagram.Direct class. Let's discover some of them.

To get last 50 messages of the chat

direct.getChatMessages("x__coder__x", 50, System.out::println)
        .setOnSuccessCallback(result -> {
            // Here are the messages
        })
        .setOnErrorCallback(e -> {
            // Failed, check the error
        });

The 3rd argument is an interface for tracking progress.

To get messages from a particular message.

This will get all the messages from the message "Hello":

direct.getChatMessagesFrom("x__coder__x", "Hello",3).setOnCompleteCallback(call -> {
    if (call.isSuccessful) {
        List<String> messages = call.result;
    } else
        call.exception.printStackTrace();
});

Here, 3 is the number of times the word 'Hello' occurs. It will retrieve messages from that occurrence. If you want to fetch messages from the last occurrence, use Instagram.FREQUENCY_FIRST or Instagram.FREQUENCY_ALL for the first occurrence. If you only want to retrieve messages from a particular occurrence, pass that no. as an argument as we did here.

To delete last 10 messages of the chat

actions.deleteMessages("x__coder__x",10, new OnMessageActionCallback() {
    @Override
    public void onSuccess(String message) {
        System.out.println(message + "Deleted");
    }
    @Override
    public void onFailed(Exception exception) {
        exception.printStackTrace();
    }
    @Override
    public void onProgress(int percentage) {
        System.out.println("Progress is " + percentage);
    }
});

Tip: Pass 0 to delete all the messages. The onProgress will not be called in this case

To send a direct message to a user or a group

AsyncTask<Void> directMessage = actions.directMessage("username_here","Hey there !");
AsyncTask<Void> groupMessage = actions.groupMessage("Hey there !","username_of_admin");

Here, 2nd line 2nd argument is the admin of the group in which we want to send messages.

Listen for new messages

direct.attachNotificationListener(message -> {
    // A new message has been received, lets reply to it
    message.reply("Hello, I'm a bot");
});

Use direct.detectNotificationListener() for stop listening the new messages. If called without attching the listener, NullPointerException will be thrown.

Feed actions

All the feed action methods are present in the Instagram.Feed class. Let's discover some of them

Instagram.Feed actions = instagram.getFeed();

AsyncTask<Void> addStory = actions.addStory(new File("post.png"));
AsyncTask<Void> postTask = actions.postPhoto(new File("post.png"),"Posted using easyinsta library");

That's it

You have successfully learned to use Instagram programmatically. Now it's time to build amazing apps or bots using this library.