5

Is it possible to get and send Instagram Direct Messages programmatically in NodeJS? I couldn't find any recent answers about the subject, so decided to ask here.

Right now I know that there are:

  1. Instagram API
  2. FB Instagram API (Graph API)
  3. instagram-private-api package

First and second don't have api for direct messages as I understand. Third one is a bit doubtful. Hard to say how exactly it works. Is Instagram okay with developers using this lib?

anvoevodin
  • 99
  • 1
  • 1
  • 4
  • The answer is pretty obvious, isn't it? The official APIs don't allow messaging. The license on that sketchy library you linked to has disclaimers that make it pretty clear. https://github.com/dilame/instagram-private-api#end-user-license-agreement-eula You could also just read the terms of service for the official APIs and see that no, you're not allowed to actually try to send direct messages programmatically. – Brad Aug 03 '19 at 17:26

3 Answers3

6

If you use the instagram-private-api you can send direct messages via:

const userId = await ig.user.getIdByUsername('username');
const thread = ig.entity.directThread([userId.toString()]);
await thread.broadcastText('Message from node');

(Source: https://github.com/dilame/instagram-private-api/issues/792)

MarvinJWendt
  • 2,357
  • 1
  • 10
  • 36
0

As of August 16, 2021 you can send IG messages through the official API.

Here is a quote from the official "FACEBOOK for developers" website: "All Instagram business accounts can connect to Messenger API for Instagram as of August 16, 2021." (Instagram Messaging)

Kalo
  • 103
  • 1
  • 7
0

Full Code With NodeJS and instagram-private-api:

require("dotenv").config();
const { IgApiClient } = require("instagram-private-api");

const postToInsta = async (orderDetails) => {
  try {
    //Contect Instagram API Client
    const ig = new IgApiClient();
    ig.state.generateDevice(process.env.IG_USERNAME);
    await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);

    //Set user ID
    const userId = await ig.user.getIdByUsername("said user");
    const thread = ig.entity.directThread([userId.toString()]);

    //Send Mesage
    await thread.broadcastText("Message from node");
    return true;
  } catch (err) {
    return err;
  }
};

const instaPost = postToInsta().then(function (instaPost) {
  if (instaPost == true) {
    console.log(`Message Sent Succesfully`);
  }
});

Just set your environment variables in your .env file using

IG_USERNAME = "<username>"
IG_PASSWORD = "<password>"

To Download

npm i instagram-private-api

Docs:
https://www.npmjs.com/package/instagram-private-api

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Commoq
  • 33
  • 6