-
-
Save ishan-marikar/20d1947670e81447f0bb93647c2cab4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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