Nerixyz / instagram_mqtt

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

How can I make this work on nodejs? #85

Closed bayramn closed 3 years ago

bayramn commented 3 years ago

I really can't seem to be making this work on nodejs?

const { IgApiClient } = require("instagram-private-api");`
const { withFbnsAndRealtime, withFbns, withRealtime} = require("instagram_mqtt");
const ig = withFbnsAndRealtime(new IgApiClient());
const igR = withRealtime(new IgApiClient());

ig.state.generateDevice(process.env.INSTAGRAM_USER);
await ig.simulate.preLoginFlow();
const loggedInUser = await ig.account.login(
process.env.INSTAGRAM_USER,
process.env.INSTAGRAM_PASSWORD);

igR.realtime.on("message", (messages) => console.log(messages));
Nerixyz commented 3 years ago

Take a look at the examples, you're missing a call to connect. You should also only use the client with realtime added.

bayramn commented 3 years ago

I have added a call to connect now, but this line where is the issue is

const ig: IgApiClientRealtime = withRealtime(new IgApiClient());

I have looked at examples but I am new to coding and Nodejs, and I neither know GraphQl and TypeScript.

Do I need add them through npm? Is there a way to use it without TypeScript/GraphQl?

Nerixyz commented 3 years ago

Typescript trans-/compiles to Javascript.

You can use regular javascript and use this library. In fact you can use regular javascript and use any typescript library on npm.

If you did the installation on npm, you can just run a js-file.

GQL isn't relevant here. It's just some name of a topic.

bayramn commented 3 years ago
const ig: IgApiClientRealtime = withRealtime(new IgApiClient());
      ^^
SyntaxError: Missing initializer in const declaration

This is what I get when I run in it in js-file.

Nerixyz commented 3 years ago

This is what I get when I run in it in js-file.

You're trying to run TypeScript code in JavaScript. The : IgApiClientRealtime is a type annotation.

bayramn commented 3 years ago

This is what I get when I run in it in js-file.

You're trying to run TypeScript code in JavaScript. The : IgApiClientRealtime is a type annotation.

How can I write it properly in JavaScript.

Nerixyz commented 3 years ago

You need to learn JavaScript first before using this library. Else you won't get anything done.

This is the example in JavaScript:

Realtime ```js const { IgApiClientRealtime, withRealtime, GraphQLSubscriptions } = require('instagram_mqtt'); const { IgApiClient } = require('instagram-private-api'); (async () => { // this extends the IgApiClient with realtime features const ig = withRealtime(new IgApiClient(), /* you may pass mixins in here */); // regular login // loginToInstagram() // now `ig` is a client with a valid session // whenever something gets sent and has no event, this is called ig.realtime.on('receive', (topic, messages) => console.log('receive', topic, messages)); // this is called with a wrapper use {message} to only get the "actual" message from the wrapper ig.realtime.on('message', logEvent('messageWrapper')); // a thread is updated, e.g. admins/members added/removed ig.realtime.on('threadUpdate', logEvent('threadUpdateWrapper')); // other direct messages - no messages ig.realtime.on('direct', logEvent('direct')); // whenever something gets sent to /ig_realtime_sub and has no event, this is called ig.realtime.on('realtimeSub', logEvent('realtimeSub')); // whenever the client has a fatal error ig.realtime.on('error', console.error); ig.realtime.on('close', () => console.error('RealtimeClient closed')); // connect // this will resolve once all initial subscriptions have been sent await ig.realtime.connect({ // optional graphQlSubs: [ // these are some subscriptions GraphQLSubscriptions.getAppPresenceSubscription(), GraphQLSubscriptions.getZeroProvisionSubscription(ig.state.phoneId), GraphQLSubscriptions.getDirectStatusSubscription(), GraphQLSubscriptions.getDirectTypingSubscription(ig.state.cookieUserId), GraphQLSubscriptions.getAsyncAdSubscription(ig.state.cookieUserId), ], // optional skywalkerSubs: [ SkywalkerSubscriptions.directSub(ig.state.cookieUserId), SkywalkerSubscriptions.liveSub(ig.state.cookieUserId), ], // optional // this enables you to get direct messages irisData: await ig.feed.directInbox().request(), // optional // in here you can change connect options // available are all properties defined in MQTToTConnectionClientInfo connectOverrides: {}, // optional // use this proxy socksOptions: { type: 5, port: 12345, host: '...' } }); // simulate turning the device off after 2s and turning it back on after another 2s setTimeout(() => { console.log('Device off'); // from now on, you won't receive any realtime-data as you "aren't in the app" // the keepAliveTimeout is somehow a 'constant' by instagram ig.realtime.direct.sendForegroundState({ inForegroundApp: false, inForegroundDevice: false, keepAliveTimeout: 900, }); }, 2000); setTimeout(() => { console.log('In App'); ig.realtime.direct.sendForegroundState({ inForegroundApp: true, inForegroundDevice: true, keepAliveTimeout: 60, }); }, 4000); // an example on how to subscribe to live comments // you can add other GraphQL subs using .subscribe await ig.realtime.graphQlSubscribe(GraphQLSubscriptions.getLiveRealtimeCommentsSubscription('')); })(); /** * A wrapper function to log to the console * @param name * @returns {(data) => void} */ function logEvent(name) { return (data) => console.log(name, data); } ```