Created
December 22, 2017 18:00
-
-
Save brycedorn/53d69b0c358c755623b2d392dd2d6640 to your computer and use it in GitHub Desktop.
chatbort
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
// yarn init | |
// yarn add -D schmavery/facebook-chat-api pos dotenv | |
// node chatbort.js | |
const fs = require('fs'); | |
const login = require('facebook-chat-api'); | |
const pos = require('pos'); | |
const env = require('dotenv').config() | |
const firebaseConfig = { | |
apiKey: env.apiKey, | |
authDomain: env.authDomain, | |
databaseURL: env.databaseURL, | |
projectId: env.projectId, | |
messagingSenderId: env.messagingSenderId | |
}; | |
const facebookCredentials = { | |
email: env.facebookEmail, | |
password: env.facebookPassword | |
}; | |
// Simple echo bot. Pushes chat messages to Firebase | |
login(facebookCredentials, (err, api) => { | |
if(err) return console.error(err); | |
api.setOptions({listenEvents: true}); | |
const tagger = new pos.Tagger(); | |
const tagsToIgnore = ['CC', 'DT', 'EX', 'IN', 'MD', 'PDT', 'PP$', 'PRP', 'SYM', 'TO', 'WDT', 'WP', 'WP$', 'WRB', ',', '.', ':', '$', '#', '"', '(', ')']; | |
let words = []; | |
let taggedWords = []; | |
let frequency = {}; | |
var listener = api.listen((err, event) => { | |
if(err) return console.error(err); | |
api.markAsRead(event.threadID, (err) => { | |
if(err) console.error(err); | |
}); | |
switch(event.type) { | |
case 'message': | |
if(event.body === '/showcounts') { | |
api.sendMessage(JSON.stringify(frequency), event.threadID); | |
break; | |
} | |
words = new pos.Lexer().lex(event.body); | |
taggedWords = tagger.tag(words); | |
let text; | |
frequency = taggedWords.reduce(function(curr, word) { | |
text = word[0].toLowerCase(); | |
if (word.length && !tagsToIgnore.includes(word[1])) { | |
curr[text] = (curr[text] || 0) + 1; | |
} | |
return curr; | |
}, frequency); | |
break; | |
default: | |
console.log(event); | |
break; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment