Commit 25541485 authored by Killian Kemps's avatar Killian Kemps

Send FB message to last sender through Websocket

parent 1d33ed5c
var fbUsers = {};
var fbThreads = {};
exports.getUserInfo = function(api, userID) {
return new Promise(function (resolve, reject) {
if (fbUsers[userID]) return resolve(fbUsers[userID])
api.getUserInfo(userID, function(err, ret) {
if(err) return console.error(err);
// Browse the ret array
for(var prop in ret) {
fbUsers[prop] = ret[prop].name;
resolve(fbUsers[prop]);
}
});
});
}
exports.getThreadInfo = function(api, threadID) {
return new Promise(function (resolve, reject) {
if (fbThreads[threadID]) resolve(fbThreads[threadID])
api.getThreadInfo(threadID, function(err, ret) {
if(err) return console.error(err);
fbThreads[threadID] = ret.name;
resolve(fbThreads[threadID]);
});
});
}
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const fs = require('fs');
var login = require("facebook-chat-api");
var facebookMessengerService = require('./facebookMessengerService');
fs.exists('appstate.json', function(exists) {
if (exists) {
console.log('Login from saved appstate');
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, function callback (err, api) {
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
var lastThreadID = '';
var fbApi = {};
io.on('connection', function(socket){
fs.exists('appstate.json', function(exists) {
if (exists) {
console.log('Login from saved appstate');
login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, function callback (err, api) {
if(err) return console.error(err);
console.log('Listening for messages...');
fbApi = api;
api.listen(function callback(err, message) {
lastThreadID = message.threadID;
console.log(message);
var allInfos = [];
console.log(Object.keys(facebookMessengerService));
allInfos.push(facebookMessengerService.getUserInfo(api, message.senderID));
allInfos.push(facebookMessengerService.getThreadInfo(api, message.threadID));
Promise.all(allInfos).then(function(data) {
console.log(data);
io.emit('chat message', data[0] + ' [thread: ' + data[1] + ']: ' + message.body);
});
// Here you can use the api
});
}
else {
console.log('Login from user credentials');
login({email: "FB_EMAIL", password: "FB_PASSWORD"}, function callback (err, api) {
if(err) return console.error(err);
});
});
}
else {
console.log('Login from user credentials');
login({email: "FB_EMAIL", password: "FB_PASSWORD"}, function callback (err, api) {
if(err) return console.error(err);
fs.writeFileSync('appstate.json', JSON.stringify(api.getAppState()));
});
}
});
socket.on('chat message', function(msg){
if (lastThreadID != '') {
fbApi.sendMessage(msg, lastThreadID);
}
io.emit('chat message', msg);
});
});
fs.writeFileSync('appstate.json', JSON.stringify(api.getAppState()));
});
}
http.listen(3000, function(){
console.log('listening on *:3000');
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment