Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const login = require('facebook-chat-api');
- const peshkata = 'GROUP ID HERE';
- let email = 'FB USERNAME HERE';
- let password = 'FB PASSWORD HERE';
- login({ email, password }, (err, api) => {
- if (err) return console.error(err);
- // Function which adds a user to the group.
- let reAdd = (user, threadId, time) => {
- return api.addUserToGroup(user, threadId, (err) => {
- if (err) {
- // If the request errored try again in an increment from now to a second.
- setTimeout(() => reAdd(user, threadId, time + 1000), time + 1000);
- return console.error(err);
- }
- });
- };
- // Listen to events and not only messages. This is to capture leave events.
- api.setOptions({ listenEvents: true });
- // Set listen callback.
- api.listen((err, message) => {
- // Check for errors.
- if (err) return console.error(err);
- // Check for Secret Peshka.
- if (message.threadID !== peshkata) return;
- // Check for leave event.
- if (message.type === 'event' && message.logMessageType === 'log:unsubscribe') {
- // Send message.
- api.sendMessage('Ne.', message.threadID);
- // Check if we have the id of the person who left - type safety.
- if (message.logMessageData && message.logMessageData.leftParticipantFbId) {
- // Queue a re-add.
- setTimeout(() => reAdd(message.logMessageData.leftParticipantFbId, message.threadID, 1000), 1000);
- return;
- }
- // If we don't have the id of the person who left - this case is impossible.
- return api.sendMessage('wtf', message.threadID);
- }
- });
- });
Add Comment
Please, Sign In to add comment