Skip to content

Instantly share code, notes, and snippets.

@M1n007
Last active July 26, 2020 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M1n007/d97bf7d0bb0eb66ee887af1d6e2efccd to your computer and use it in GitHub Desktop.
Save M1n007/d97bf7d0bb0eb66ee887af1d6e2efccd to your computer and use it in GitHub Desktop.
const {existsSync, writeFileSync, readFileSync} = require('fs');
const {IgApiClient}= require('instagram-private-api');
const {withFbns} = require('instagram_mqtt');
const IG_USERNAME = 'yourusername';
const IG_PASSWORD = 'yourpassword';
(async () => {
const ig = withFbns(new IgApiClient());
ig.state.generateDevice(IG_USERNAME);
// this will set the auth and the cookies for instagram
await readState(ig);
// this logs the client in
await loginToInstagram(ig);
// you received a notification
ig.fbns.push$.subscribe(async(data) => {
console.log(data) //nangkep datanya disini ya mas mas
});
// the client received auth data
// 'error' is emitted whenever the client experiences a fatal error
ig.fbns.error$.subscribe(logEvent('error'));
// 'warning' is emitted whenever the client errors but the connection isn't affected
ig.fbns.warning$.subscribe(logEvent('warning'));
// this sends the connect packet to the server and starts the connection
// the promise will resolve once the client is fully connected (once /push/register/ is received)
await ig.fbns.connect();
})();
async function saveState(ig) {
return writeFileSync('state.json', await ig.exportState(), { encoding: 'utf8' });
}
async function readState(ig) {
if (!await existsSync('state.json'))
return;
await ig.importState(await readFileSync('state.json', {encoding: 'utf8'}));
}
async function loginToInstagram(ig) {
ig.request.end$.subscribe(() => saveState(ig));
await ig.account.login(IG_USERNAME, IG_PASSWORD);
}
function logEvent(name) {
return (data) => console.log(name, data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment