Skip to content

Instantly share code, notes, and snippets.

@ishan-marikar
Created July 8, 2018 08:39
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 ishan-marikar/20d1947670e81447f0bb93647c2cab4e to your computer and use it in GitHub Desktop.
Save ishan-marikar/20d1947670e81447f0bb93647c2cab4e to your computer and use it in GitHub Desktop.
const Facebook = require("facebook-chat-api");
const FileSystem = require("fs");
const BIRTHDAY_WISHES = require("./birthday-wishes.json");
const { FACEBOOK_USERNAME, FACEBOOK_PASSWORD, FORCE_LOGIN=true } = process.env;
const getRandomWish = () => {
let randomWish =
BIRTHDAY_WISHES[Math.floor(Math.random() * BIRTHDAY_WISHES.length)];
return randomWish;
};
const getFriendsWithBirthdays = friends => {
let friendsWithBirthdays = friends.filter(friend => {
return friend.isBirthday === true && friend.userID !== "0";
});
return friendsWithBirthdays;
};
const getCredentials = () => {
const stateFileExists = FileSystem.existsSync("appstate.json");
const forceLogin = FORCE_LOGIN === "true" ? true : false;
if (stateFileExists && !forceLogin) {
const appState = FileSystem.readFileSync("appstate.json");
return { appState };
} else {
return {
email: FACEBOOK_USERNAME,
password: FACEBOOK_PASSWORD
};
}
};
let wishFriendsOnTheirBirthday = () => {
Facebook(getCredentials(), (error, instance) => {
if (error) throw error;
const sendWishes = friend => {
let { firstName, userID } = friend;
let body = `Hello ${firstName}, ${getRandomWish()}.`;
instance.sendMessage({ body }, userID, (error, { timestamp }) => {
if (error) throw error;
console.log(
`Sent Birthday Wishes to ${firstName} on ${new Date(
timestamp
).toDateString()}.`
);
});
};
FileSystem.writeFileSync(
"appstate.json",
JSON.stringify(instance.getAppState())
);
instance.getFriendsList((error, internetFriends) => {
if (error) throw error;
let friendsWithBirthdays = getFriendsWithBirthdays(internetFriends);
friendsWithBirthdays.forEach(friend => {
sendWishes(friend);
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment