Nerixyz / instagram_mqtt

Realtime and Push Notification (FBNS) support for the instagram-private-api
MIT License
244 stars 49 forks source link

Listen for new messages in cli script - patch event #70

Closed realrecordzLab closed 3 years ago

realrecordzLab commented 3 years ago

I'm using this code into a cli script to listen for new messages:

const { IgApiClient } = require('instagram-private-api');
const { withRealtime } = require('instagram_mqtt');
const chalk = require('chalk');
const ora = require('ora');

const ig = withRealtime(new IgApiClient());
ig.state.generateDevice(process.env.IGUSER);

( async () => {
    await ig.simulate.preLoginFlow();
    await ig.account.login(process.env.IGUSER, process.env.IGPASS);

    ora({
        color: 'magenta', 
        text: chalk.magenta(`Listening for incoming messages\n`)
    }).start();

    await ig.realtime.connect({ irisData: await ig.feed.directInbox().request() });
// using setInterval to mantain the script running
    setInterval( () => {}, 1000);

    ig.realtime.on('message', (message) => console.log(message) );

})();

I'm not suer if I'm doing it in the right way. I have two question about this library. 1) Is necessary to call the ig.realtime.connect({ irisData: await ig.feed.directInbox().request() }); method to listen for new messages?

2) Is this the right method to get new messages from DM inbox?

 ig.realtime.on('message', (message) => console.log(message) );

In my console log I've found this log about an event, but it's not a new message., why it will be logged from the on('message') event listener?

 {
  event: 'patch',
  message_type: 1,
  seq_id: 3016,
  mutation_token: '6779103590224992045',
  realtime: true,
  message: {
    path: '/direct_v2/threads/340282366841710300949128156071872279562/participants/7285039631/has_seen',
    op: 'replace',
    thread_id: '340282366841710300949128156071872279562',
    item_id: '29814791532328000641606257215537152',
    client_context: '',
    timestamp: 1616264245837932,
    created_at: 1616263087577622,
    shh_seen_state: {}
  }
Nerixyz commented 3 years ago
  1. Is necessary to call the ig.realtime.connect({ irisData: await ig.feed.directInbox().request() }); method to listen for new messages?

It is necessary because you need to open a TLS connection to Instagram.

  1. Is this the right method to get new messages from DM inbox?

It is.

In my console log I've found this log about an event, but it's not a new message.

It logs the update of a message in this case (event: patch, op: replace, /has_seen).

Btw, I don't think you need to use setInterval to prevent Node from exiting. The open client should already prevent it.

realrecordzLab commented 3 years ago

It logs the update of a message in this case (event: patch, op: replace, /has_seen).

But I don't have new messages if I open instagram inbox , it's an event related to the messages reading form the user who receive it?

Btw, I don't think you need to use setInterval to prevent Node from exiting. The open client should already prevent it.

I'm using set interval because I'm calling a child script. I've tried to remove it but the script will terminate

UPDATE Tested also without setInterval and the script seems to continue runnig :)

Nerixyz commented 3 years ago

it's an event related to the messages reading form the user who receive it?

Right. It notifies you that the user has seen the message.

realrecordzLab commented 3 years ago

I suppose that when a patch event is logged I can get the username or user full name by using instagram private api?

Anyway works like a charm and it's more quick to implement to get DM than the instagram-private-api library. I'm reading the code about messages object structure, Is there a way to get messages reactions or images/vocal messages?

Nerixyz commented 3 years ago

I can get the username or user full name by using instagram private api?

Right.

Is there a way to get messages reactions or images/vocal messages?

Should come with the message event.

realrecordzLab commented 3 years ago

I can get the username or user full name by using instagram private api?

Right.

Is there a way to get messages reactions or images/vocal messages?

Should come with the message event.

I've seen that an object will be logged in case of messages that contains images or audio. I will do some research to implement these two features in my script. Thank you for the help.