Skip to content

Commit

Permalink
Added pushbullet notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliasyoussef47 committed Aug 21, 2020
1 parent 506a765 commit b421724
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ IG_USERNAME=
IG_PASSWORD=
TRACKED_USERS_USERNAMES="eminem, logic"
COMMENTS_ARRAY="Comment 1, Comment 2, Comment 3"
PUSHBULLET_NOTIFICATION=true
PUSHBULLET_ACCESS_TOKEN=
1 change: 1 addition & 0 deletions .idea/dictionaries/d_s_a.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions InstaAutoComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export class InstaAutoComment {

try {
commentResult = await mediaRepo.comment(comment);
console.log('\n');
console.log(chalk.green("Commented on " + "https://www.instagram.com/p/" + instagramIdToUrlSegment(commentResult.media_id) + " with the comment " + commentResult.text));
} catch (err) {
if (err instanceof IgActionSpamError) {
Expand Down Expand Up @@ -300,10 +301,11 @@ export class InstaAutoComment {
/**
* A wrapper function to log to the console
* @param name
* @returns {(data) => void}
* @param data
* @returns void
*/
public logEvent(name: string) {
return (data: any) => console.log(chalk.blue(name), chalk.blue(data));
public logEvent(name: string, data?) {
console.log(chalk.blue(name), chalk.blue(data));
}

public async usernamesToPks(usernames: string[]){
Expand Down
96 changes: 62 additions & 34 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {InstaAutoComment} from "./InstaAutoComment";
import {DoComment} from "./InstaAutoComment";
import * as util from "util";

require('dotenv').config();
const chalk = require('chalk');
const ora = require('ora');
Expand All @@ -23,49 +23,77 @@ util.inspect.defaultOptions.maxArrayLength = null;
const loopInterval: number = +process.env.LOOP_INTERVAL;
const trackingUserPk: number = +process.env.TRACKING_USER_PK;
const postSecondsOld: number = +process.env.POST_SECONDS_OLD;
const commentsArray = process.env.COMMENTS_ARRAY.split(",").map((item)=>item.trim());
const trackedUsersUsernames: string[] = process.env.TRACKED_USERS_USERNAMES.split(",").map((item)=>item.trim());
const commentsArray = process.env.COMMENTS_ARRAY.split(",").map((item) => item.trim());
const trackedUsersUsernames: string[] = process.env.TRACKED_USERS_USERNAMES.split(",").map((item) => item.trim());
let trackedUsersPks: number[];
let pushbulletNotificationEnabled: boolean = stringToBool(process.env.PUSHBULLET_NOTIFICATION);
let pushbulletAccessToken = process.env.PUSHBULLET_ACCESS_TOKEN;
let pusher;

if (pushbulletNotificationEnabled) {
const PushBullet = require('pushbullet');
pusher = new PushBullet(pushbulletAccessToken);
}

let iac = new InstaAutoComment();


(async () => {
await iac.login().then(async r => {
trackedUsersPks = await iac.usernamesToPks(trackedUsersUsernames);
iac.trackedUsersUsernames = trackedUsersUsernames;
trackedUsersUsernames.forEach(trackedUsersUsername => {
console.log(chalk.blue("Tracking: ", trackedUsersUsername));
});
console.log('\n');
// you received a notification
iac.ig.fbns.push$.subscribe(
(push) => {
if (push.pushCategory === "post" && trackedUsersPks.includes(Number(push.sourceUserId))) {
iac.commentOnPostWithRandomComment(push.actionParams["id"], commentsArray);
try {
await iac.login().then(async r => {
trackedUsersPks = await iac.usernamesToPks(trackedUsersUsernames);
iac.trackedUsersUsernames = trackedUsersUsernames;
console.log('\n');
// you received a notification
iac.ig.fbns.push$.subscribe(
(push) => {
if (push.pushCategory === "post" && trackedUsersPks.includes(Number(push.sourceUserId))) {
iac.commentOnPostWithRandomComment(push.actionParams["id"], commentsArray);
}
}
}
);
);

// the client received auth data
// the listener has to be added before connecting
iac.ig.fbns.auth$.subscribe(async (auth) => {
// logs the auth
// iac.logEvent('auth')(auth);
//saves the auth
await iac.saveState(iac.ig);
});
// the client received auth data
// the listener has to be added before connecting
iac.ig.fbns.auth$.subscribe(async (auth) => {
// logs the auth
// iac.logEvent('auth')(auth);
//saves the auth
await iac.saveState(iac.ig);
});

// 'error' is emitted whenever the client experiences a fatal error
iac.ig.fbns.error$.subscribe(iac.logEvent('error'));
// 'error' is emitted whenever the client experiences a fatal error
iac.ig.fbns.error$.subscribe((fbnsError) => {
iac.logEvent('error', fbnsError);
if (pushbulletAccessToken) pusher.note('', 'InstaAutoComment | Error with MQTT client', fbnsError.message);
});

// 'warning' is emitted whenever the client errors but the connection isn't affected
iac.ig.fbns.warning$.subscribe(iac.logEvent('warning'));
// 'warning' is emitted whenever the client errors but the connection isn't affected
iac.ig.fbns.warning$.subscribe((fbnsWarning) => {
iac.logEvent('warning', fbnsWarning);
});

// 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 iac.ig.fbns.connect();
// 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 iac.ig.fbns.connect();

const spinner = ora('Waiting for notification').start();
});
trackedUsersUsernames.forEach(trackedUsersUsername => {
console.log(chalk.blue("Tracking: ", trackedUsersUsername));
});

const spinner = ora('Waiting for a notification').start();
});
} catch (e) {
if (pushbulletAccessToken) pusher.note('', 'InstaAutoComment | Error with program', e.message);
}
})();

function stringToBool(stringBool: string) {
if (stringBool.toLowerCase() === "true") {
return true;
} else if (stringBool.toLowerCase() === "false") {
return false;
} else {
return false;
}
}
63 changes: 63 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"instagram-private-api": "^1.39.2",
"instagram_mqtt": "^0.2.16",
"ora": "^5.0.0",
"pushbullet": "^2.4.0",
"tsc": "^1.20150623.0"
},
"scripts": {
Expand Down

0 comments on commit b421724

Please sign in to comment.