From 1486f58ffdd563f5594231a435e2b7ef618b6000 Mon Sep 17 00:00:00 2001 From: Anton Toropanov Date: Fri, 6 Apr 2018 17:48:23 +0300 Subject: [PATCH 01/41] WIP --- facebookbot.js | 11 ++++------- start.sh | 3 --- 2 files changed, 4 insertions(+), 10 deletions(-) delete mode 100755 start.sh diff --git a/facebookbot.js b/facebookbot.js index d8b0d84..1c6a5d0 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -2,7 +2,7 @@ var fs = require('fs'); var Bot = require('node-telegram-bot'); var login = require("facebook-chat-api"); -if (!process.env.TELEGRAM_USER || !process.env.APP_TOKEN) +if (!process.env.TELEGRAM_USER_ID || !process.env.TELEGRAM_TOKEN) return console.log("Please define this env variables -- TELEGRAM_USER - APP_TOKEN"); var owner = {username: process.env.TELEGRAM_USER, chat_id: process.env.CHAT_ID || undefined}; @@ -22,11 +22,8 @@ var friends = {}; var threadListTmp; var currentThreadId; -var config; -config = JSON.parse(fs.readFileSync('config.json', 'utf-8')); - -login({email: config.email, password: config.password}, async function (err, api) { +login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASSWORD}, async function (err, api) { if (err) return console.error(err); await retrieveFriendsFromFacebook(api); @@ -94,7 +91,7 @@ login({email: config.email, password: config.password}, async function (err, api if (message.photo != undefined) { bot.getFile({ file_id: message.photo[message.photo.length - 1].file_id, - dir: config.dir + dir: '/' }, function callback(err, arr) { api.sendMessage({attachment: fs.createReadStream(arr.destination)}, currentThreadId, function (err, api) { if (err) return console.error(err); @@ -224,4 +221,4 @@ const sendAttachmentsToTelegram = function (bot, senderName, message) { function reset() { currentThreadId = undefined; threadListTmp = undefined; -} \ No newline at end of file +} diff --git a/start.sh b/start.sh deleted file mode 100755 index a21388a..0000000 --- a/start.sh +++ /dev/null @@ -1,3 +0,0 @@ -TELEGRAM_USER="your telegram login" -APP_TOKEN="the token of your bot that BotFather gave you" -node facebookbot.js From ecafdecc77d25044a825547f70701dc3e62e66d5 Mon Sep 17 00:00:00 2001 From: Kreastr Date: Mon, 16 Apr 2018 13:09:05 +0300 Subject: [PATCH 02/41] Start converting bot to telegrem-bot-api --- facebookbot.js | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index 1c6a5d0..bc4075e 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -1,5 +1,5 @@ var fs = require('fs'); -var Bot = require('node-telegram-bot'); +var Bot = require('node-telegram-bot-api'); var login = require("facebook-chat-api"); if (!process.env.TELEGRAM_USER_ID || !process.env.TELEGRAM_TOKEN) @@ -23,13 +23,38 @@ var threadListTmp; var currentThreadId; -login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASSWORD}, async function (err, api) { - if (err) return console.error(err); +// Start Telegram Bot +var bot = new TelegramBot(login, {polling: true}); - await retrieveFriendsFromFacebook(api); +//listen telegram message - //listen telegram message - var bot = new Bot({ +function doAuth(msg) +{ + if (msg.from.id == owner.username) + return true; + bot.sendMessage( msg.chat.id, "I don't know you, "+msg.from.id); +} + +function initListeners() +{ + bot.onText(/\/echo (.+)/, (msg, match) => { + const chatId = msg.chat.id; + const resp = match[1]; // the captured "whatever" + doAuth(msg) + // send back the matched "whatever" to the chat + bot.sendMessage(chatId, resp); + }); + + bot.onText(/\/echo (.+)/, (msg, match) => { + const chatId = msg.chat.id; + const resp = match[1]; // the captured "whatever" + doAuth(msg) + // send back the matched "whatever" to the chat + bot.sendMessage(chatId, resp); + }); +} +/* +var bot = new Bot({ token: process.env.APP_TOKEN }).on('message', async function (message) { if (message.from.username != owner.username) @@ -147,6 +172,12 @@ login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASS } }).start(); +*/ + +login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASSWORD}, async function (err, api) { + if (err) return console.error(err); + + await retrieveFriendsFromFacebook(api); //listen message from FB and forward to telegram From f0d20d630478114f7f7d8e33b5b8f4631b205ba0 Mon Sep 17 00:00:00 2001 From: Kreastr Date: Mon, 16 Apr 2018 13:15:54 +0300 Subject: [PATCH 03/41] fix dep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2b539e0..23aaaf6 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "homepage": "https://github.com/Liryna/FacebookBot", "dependencies": { "facebook-chat-api": "git+https://github.com/Schmavery/facebook-chat-api.git", - "node-telegram-bot": "^0.1.5" + "node-telegram-bot-api": "git+https://github.com/yagop/node-telegram-bot-api.git" }, "scripts": {} } From d28c5f66b77a19feb41e2e1b29dcc0e6385b9720 Mon Sep 17 00:00:00 2001 From: Kreastr Date: Mon, 16 Apr 2018 13:20:32 +0300 Subject: [PATCH 04/41] Fixes, correct varname --- facebookbot.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index bc4075e..19a068d 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -3,9 +3,9 @@ var Bot = require('node-telegram-bot-api'); var login = require("facebook-chat-api"); if (!process.env.TELEGRAM_USER_ID || !process.env.TELEGRAM_TOKEN) - return console.log("Please define this env variables -- TELEGRAM_USER - APP_TOKEN"); + return console.log("Please define this env variables -- TELEGRAM_USER_ID - TELEGRAM_TOKEN"); -var owner = {username: process.env.TELEGRAM_USER, chat_id: process.env.CHAT_ID || undefined}; +var owner = {username: process.env.TELEGRAM_USER_ID, chat_id: process.env.CHAT_ID || undefined}; var maxThreadNb = 10; function getUsage() { @@ -14,7 +14,7 @@ function getUsage() { + "Available commands:\n" + "/threadlist - List the latest conversations you had with your friends.\n" + "/cancel - Cancel the current command.\n" - + "\n\nMore Informations: https://github.com/Liryna/FacebookBot"; + + "\n\nMore Informations: https://github.com/Kreastr/FacebookBot"; } var chat = new Array(); @@ -24,7 +24,7 @@ var currentThreadId; // Start Telegram Bot -var bot = new TelegramBot(login, {polling: true}); +var bot = new Bot(login, {polling: true}); //listen telegram message From ec8e6248eb470f727df8be453fa04150f78940fa Mon Sep 17 00:00:00 2001 From: Kreastr Date: Mon, 16 Apr 2018 13:22:57 +0300 Subject: [PATCH 05/41] Fix token --- facebookbot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index 19a068d..b9c124d 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -24,7 +24,7 @@ var currentThreadId; // Start Telegram Bot -var bot = new Bot(login, {polling: true}); +var bot = new Bot(process.env.TELEGRAM_TOKEN, {polling: true}); //listen telegram message From 1ebe0041c0eb1a64c607dcf88c6db10ba05e9e36 Mon Sep 17 00:00:00 2001 From: Kreastr Date: Mon, 16 Apr 2018 13:32:00 +0300 Subject: [PATCH 06/41] debug --- facebookbot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index b9c124d..af13b6c 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -173,7 +173,7 @@ var bot = new Bot({ }).start(); */ - +/* login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASSWORD}, async function (err, api) { if (err) return console.error(err); @@ -252,4 +252,4 @@ const sendAttachmentsToTelegram = function (bot, senderName, message) { function reset() { currentThreadId = undefined; threadListTmp = undefined; -} +}*/ From 5ee10b7d4a092b739660d88a860dc26f0f67b66c Mon Sep 17 00:00:00 2001 From: Kreastr Date: Mon, 16 Apr 2018 13:57:57 +0300 Subject: [PATCH 07/41] dev --- facebookbot.js | 146 ++++++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 75 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index af13b6c..27c05ce 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -31,8 +31,11 @@ var bot = new Bot(process.env.TELEGRAM_TOKEN, {polling: true}); function doAuth(msg) { if (msg.from.id == owner.username) + if (owner.chat_id == undefined) + owner.chat_id = msg.chat.id; return true; bot.sendMessage( msg.chat.id, "I don't know you, "+msg.from.id); + return false; } function initListeners() @@ -40,44 +43,16 @@ function initListeners() bot.onText(/\/echo (.+)/, (msg, match) => { const chatId = msg.chat.id; const resp = match[1]; // the captured "whatever" - doAuth(msg) + if (!doAuth(msg)) return; // send back the matched "whatever" to the chat bot.sendMessage(chatId, resp); }); - bot.onText(/\/echo (.+)/, (msg, match) => { + bot.onText(/\/threadlist/, (msg, match) => { const chatId = msg.chat.id; - const resp = match[1]; // the captured "whatever" - doAuth(msg) - // send back the matched "whatever" to the chat - bot.sendMessage(chatId, resp); - }); -} -/* -var bot = new Bot({ - token: process.env.APP_TOKEN - }).on('message', async function (message) { - if (message.from.username != owner.username) - bot.sendMessage({ - chat_id: message.chat.id, - text: "You are not my owner! Go away ! \n" - + "- https://github.com/Liryna/FacebookBot" - }); - else { - if (owner.chat_id == undefined) - owner.chat_id = message.chat.id; //save owner chat id - TODO save in config file - - if (!!message.reply_to_message - && !!chat[message.reply_to_message.message_id]) { //it is a reply message from FB - - api.sendMessage(message.text, - chat[message.reply_to_message.message_id], function (err, api) { - if (err) return console.error(err); - }); - } else { - - if (message.text == "/threadlist") { - api.getThreadList(0, maxThreadNb, function callback(err, arr) { + if (!doAuth(msg)) return; + + api.getThreadList(0, maxThreadNb, function callback(err, arr) { var ft = require('./lib/findThread'); var fbids = ft.getParticipantsIds(arr); @@ -88,31 +63,36 @@ var bot = new Bot({ ft.createThreads(arr, ret, function (conversatioNames, newThreadListTmp) { threadListTmp = newThreadListTmp; - bot.sendMessage({ - chat_id: message.chat.id, - text: "Who is the recipient ?", + bot.sendMessage(msg.chat.id, "Who is the recipient ?",{ reply_markup: { keyboard: conversatioNames } - }, function (err, ret) { - if (err) return console.error(err); }) }); }); }); - } else if (message.text == "/cancel") { - reset(); - bot.sendMessage({ - chat_id: message.chat.id, - text: "Command canceled.", + + }); + } + + bot.onText(/\/cancel/, (msg, match) => { + const chatId = msg.chat.id; + if (!doAuth(msg)) return; + reset(); + bot.sendMessage( + msg.chat.id, + "Command canceled.",{ reply_markup: { hide_keyboard: true } - }, - function (err, ret) { - if (err) return console.error(err); }); - } else if (currentThreadId != undefined) { + } + + bot.on('message', (msg) => { + const chatId = msg.chat.id; + if (!doAuth(msg)) return; + + if (currentThreadId != undefined) { if (message.photo != undefined) { bot.getFile({ file_id: message.photo[message.photo.length - 1].file_id, @@ -132,7 +112,7 @@ var bot = new Bot({ if (err) return console.error(err); }); } - } else if (threadListTmp != undefined) { //Check if owner have send a good recipient name + } else if (threadListTmp != undefined) { //Check if owner have send a good recipient name currentThreadId = undefined; for (var x = 0; x < threadListTmp.length; x++) { if (threadListTmp[x].name == message.text) @@ -140,40 +120,61 @@ var bot = new Bot({ } if (currentThreadId != undefined) - bot.sendMessage({ - chat_id: message.chat.id, - text: "What is the message for him ?", + bot.sendMessage( + message.chat.id, + "What is the message for him ?",{ reply_markup: { hide_keyboard: true } - }, - function (err, ret) { - if (err) return console.error(err); }); else - bot.sendMessage({ + bot.sendMessage( chat_id: message.chat.id, text: "I do not know him, Please give me a correct name or /cancel." - }, - function (err, ret) { - if (err) return console.error(err); - }); - } else { - bot.sendMessage({ - chat_id: message.chat.id, - text: getUsage(), + ); + } else { + bot.sendMessage(message.chat.id, + getUsage(),{ disable_web_page_preview: true - }, - function (err, ret) { - if (err) return console.error(err); }); } + } +} + +var bot = new Bot({ + token: process.env.APP_TOKEN + }).on('message', async function (message) { + if (message.from.username != owner.username) + bot.sendMessage({ + chat_id: message.chat.id, + text: "You are not my owner! Go away ! \n" + + "- https://github.com/Liryna/FacebookBot" + }); + else { + if (owner.chat_id == undefined) + owner.chat_id = message.chat.id; //save owner chat id - TODO save in config file + + if (!!message.reply_to_message + && !!chat[message.reply_to_message.message_id]) { //it is a reply message from FB + + api.sendMessage(message.text, + chat[message.reply_to_message.message_id], function (err, api) { + if (err) return console.error(err); + }); + } else { + + if (message.text == "/threadlist") { + + } else if (message.text == "/cancel") { + + } else } } }).start(); -*/ -/* + +initListeners(); + login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASSWORD}, async function (err, api) { if (err) return console.error(err); @@ -225,12 +226,7 @@ const sendTextMessageToTelegram = function (bot, senderName, message, text) { forwardmsg = message.threadID + ": " + forwardmsg; } - bot.sendMessage({chat_id: owner.chat_id, text: forwardmsg}, function (err, res) { - if (err) return console.error(err); - - //save message id send and fb thread id for futur reply - chat[res.message_id] = message.threadID; - }) + bot.sendMessage(owner.chat_id, forwardmsg,) }; const sendAttachmentsToTelegram = function (bot, senderName, message) { @@ -252,4 +248,4 @@ const sendAttachmentsToTelegram = function (bot, senderName, message) { function reset() { currentThreadId = undefined; threadListTmp = undefined; -}*/ +} From 1adbf459a77f392e243e026f0c0e5b27d7c8a915 Mon Sep 17 00:00:00 2001 From: Aleksei Date: Mon, 16 Apr 2018 11:07:03 +0000 Subject: [PATCH 08/41] fixes --- facebookbot.js | 43 +++++-------------------------------------- 1 file changed, 5 insertions(+), 38 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index 27c05ce..5a93a6f 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -72,8 +72,7 @@ function initListeners() }); }); - }); - } + }); bot.onText(/\/cancel/, (msg, match) => { const chatId = msg.chat.id; @@ -86,7 +85,7 @@ function initListeners() hide_keyboard: true } }); - } + }); bot.on('message', (msg) => { const chatId = msg.chat.id; @@ -129,8 +128,8 @@ function initListeners() }); else bot.sendMessage( - chat_id: message.chat.id, - text: "I do not know him, Please give me a correct name or /cancel." + message.chat.id, + "I do not know him, Please give me a correct name or /cancel." ); } else { bot.sendMessage(message.chat.id, @@ -138,41 +137,9 @@ function initListeners() disable_web_page_preview: true }); } - } + }); } -var bot = new Bot({ - token: process.env.APP_TOKEN - }).on('message', async function (message) { - if (message.from.username != owner.username) - bot.sendMessage({ - chat_id: message.chat.id, - text: "You are not my owner! Go away ! \n" - + "- https://github.com/Liryna/FacebookBot" - }); - else { - if (owner.chat_id == undefined) - owner.chat_id = message.chat.id; //save owner chat id - TODO save in config file - - if (!!message.reply_to_message - && !!chat[message.reply_to_message.message_id]) { //it is a reply message from FB - - api.sendMessage(message.text, - chat[message.reply_to_message.message_id], function (err, api) { - if (err) return console.error(err); - }); - } else { - - if (message.text == "/threadlist") { - - } else if (message.text == "/cancel") { - - } else - } - } - }).start(); - - initListeners(); login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASSWORD}, async function (err, api) { From fdf2cdd604c65e51413e0015a141fa6af3d8ad63 Mon Sep 17 00:00:00 2001 From: Kreastr Date: Mon, 16 Apr 2018 14:09:31 +0300 Subject: [PATCH 09/41] debug info --- facebookbot.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/facebookbot.js b/facebookbot.js index 5a93a6f..8386a75 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -30,6 +30,8 @@ var bot = new Bot(process.env.TELEGRAM_TOKEN, {polling: true}); function doAuth(msg) { + console.log("New message in Telegram") + console.log(msg) if (msg.from.id == owner.username) if (owner.chat_id == undefined) owner.chat_id = msg.chat.id; From f567544cc04be7b9162efccbcfd22ebc4b4fefdd Mon Sep 17 00:00:00 2001 From: Kreastr Date: Mon, 16 Apr 2018 14:14:28 +0300 Subject: [PATCH 10/41] fixes --- facebookbot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index 8386a75..cc6de82 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -32,11 +32,11 @@ function doAuth(msg) { console.log("New message in Telegram") console.log(msg) - if (msg.from.id == owner.username) + if (msg.from.username== owner.username) if (owner.chat_id == undefined) owner.chat_id = msg.chat.id; return true; - bot.sendMessage( msg.chat.id, "I don't know you, "+msg.from.id); + bot.sendMessage( msg.chat.id, "I don't know you, "+msg.from.username); return false; } From 4b81102a887d415b8339877f06e6f2ceeb62833c Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 17:54:19 +0300 Subject: [PATCH 11/41] fixes --- facebookbot.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index cc6de82..44ec49b 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -34,9 +34,9 @@ function doAuth(msg) console.log(msg) if (msg.from.username== owner.username) if (owner.chat_id == undefined) - owner.chat_id = msg.chat.id; - return true; - bot.sendMessage( msg.chat.id, "I don't know you, "+msg.from.username); + owner.chat_id = msg.chat.id; + return true; + bot.sendMessage( ""+msg.chat.id, "I don't know you, "+msg.from.username); return false; } @@ -94,21 +94,19 @@ function initListeners() if (!doAuth(msg)) return; if (currentThreadId != undefined) { - if (message.photo != undefined) { - bot.getFile({ - file_id: message.photo[message.photo.length - 1].file_id, + if (msg.photo != undefined) { + bot.getFile(msg.photo[msg.photo.length - 1].file_id,{ dir: '/' - }, function callback(err, arr) { + }).then( function(arr) { api.sendMessage({attachment: fs.createReadStream(arr.destination)}, currentThreadId, function (err, api) { - if (err) return console.error(err); fs.unlink(arr.destination, function (err) { if (err) throw err; }); }); - + }); }); } else { - api.sendMessage(message.text, + api.sendMessage(msg.text, currentThreadId, function (err, api) { if (err) return console.error(err); }); @@ -116,13 +114,13 @@ function initListeners() } else if (threadListTmp != undefined) { //Check if owner have send a good recipient name currentThreadId = undefined; for (var x = 0; x < threadListTmp.length; x++) { - if (threadListTmp[x].name == message.text) + if (threadListTmp[x].name == msg.text) currentThreadId = threadListTmp[x].threadID; } if (currentThreadId != undefined) bot.sendMessage( - message.chat.id, + msg.chat.id, "What is the message for him ?",{ reply_markup: { hide_keyboard: true @@ -130,15 +128,16 @@ function initListeners() }); else bot.sendMessage( - message.chat.id, + msg.chat.id, "I do not know him, Please give me a correct name or /cancel." ); } else { - bot.sendMessage(message.chat.id, + bot.sendMessage(msg.chat.id, getUsage(),{ disable_web_page_preview: true }); } + }); } From 2a76dc73778025727b355d57bfb2e2990b5b5583 Mon Sep 17 00:00:00 2001 From: Aleksei Date: Mon, 16 Apr 2018 15:04:20 +0000 Subject: [PATCH 12/41] fixes --- facebookbot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index 44ec49b..26afde1 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -69,7 +69,7 @@ function initListeners() reply_markup: { keyboard: conversatioNames } - }) + }); }); }); }); @@ -104,7 +104,7 @@ function initListeners() }); }); }); - }); + } else { api.sendMessage(msg.text, currentThreadId, function (err, api) { From 6e3cb690178a23d6e7c0b92818d225d457849ff7 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 18:06:26 +0300 Subject: [PATCH 13/41] fix --- facebookbot.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index 26afde1..a0df141 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -55,7 +55,8 @@ function initListeners() if (!doAuth(msg)) return; api.getThreadList(0, maxThreadNb, function callback(err, arr) { - + if (err) + return console.error(err); var ft = require('./lib/findThread'); var fbids = ft.getParticipantsIds(arr); currentThreadId = undefined; //reset current thread From 205b52f7b87a317a1013be48a528ddd5c99f94b1 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 18:18:25 +0300 Subject: [PATCH 14/41] fix --- facebookbot.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index a0df141..86f1eeb 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -94,6 +94,15 @@ function initListeners() const chatId = msg.chat.id; if (!doAuth(msg)) return; + if (!!msg.reply_to_message + && !!chat[msg.reply_to_message.message_id]) { //it is a reply message from FB + + api.sendMessage(msg.text, + chat[msg.reply_to_message.message_id], function (err, api) { + if (err) return console.error(err); + }); + } + if (currentThreadId != undefined) { if (msg.photo != undefined) { bot.getFile(msg.photo[msg.photo.length - 1].file_id,{ @@ -195,7 +204,8 @@ const sendTextMessageToTelegram = function (bot, senderName, message, text) { forwardmsg = message.threadID + ": " + forwardmsg; } - bot.sendMessage(owner.chat_id, forwardmsg,) + bot.sendMessage(owner.chat_id, forwardmsg) + chat[res.message_id] = message.threadID; }; const sendAttachmentsToTelegram = function (bot, senderName, message) { From 71b5646f7748556080cfa9db3b63ddcf37252e86 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 18:26:46 +0300 Subject: [PATCH 15/41] debug info --- facebookbot.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/facebookbot.js b/facebookbot.js index 86f1eeb..c145138 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -168,6 +168,8 @@ login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASS console.error("Errors on facebook listening", err); } else if (message) { + console.log("Got FB message") + sonsole.log(message) // gets the fb user name given his id const senderName = friends[message.senderID] || message.senderID; From 96b920cfb38a877fe438cf5e4777236f97b64f46 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 18:32:18 +0300 Subject: [PATCH 16/41] logic fix --- facebookbot.js | 63 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index c145138..119cfa2 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -26,16 +26,53 @@ var currentThreadId; // Start Telegram Bot var bot = new Bot(process.env.TELEGRAM_TOKEN, {polling: true}); + + +var api = undefined; +var fbReady = false; + +function initFBlistener(){ + api.listen(function callback(err, message) { + if (err) { + console.error("Errors on facebook listening", err); + } + else if (message) { + console.log("Got FB message") + sonsole.log(message) + // gets the fb user name given his id + const senderName = friends[message.senderID] || message.senderID; + + if (message.attachments.length > 0) { + sendAttachmentsToTelegram(bot, senderName, message); + } else { + sendTextMessageToTelegram(bot, senderName, message, message.body); + } + } else { + console.log("no message from facebook"); + } + + }); +} + + + //listen telegram message + function doAuth(msg) { console.log("New message in Telegram") console.log(msg) if (msg.from.username== owner.username) + { if (owner.chat_id == undefined) + { owner.chat_id = msg.chat.id; - return true; + if (fbReady) + initFBlistener(); + } + return true; + } bot.sendMessage( ""+msg.chat.id, "I don't know you, "+msg.from.username); return false; } @@ -153,6 +190,8 @@ function initListeners() initListeners(); + + login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASSWORD}, async function (err, api) { if (err) return console.error(err); @@ -162,27 +201,9 @@ login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASS if (!owner.chat_id) { console.error("No chat id found."); + fbReady = true; } else { - api.listen(function callback(err, message) { - if (err) { - console.error("Errors on facebook listening", err); - } - else if (message) { - console.log("Got FB message") - sonsole.log(message) - // gets the fb user name given his id - const senderName = friends[message.senderID] || message.senderID; - - if (message.attachments.length > 0) { - sendAttachmentsToTelegram(bot, senderName, message); - } else { - sendTextMessageToTelegram(bot, senderName, message, message.body); - } - } else { - console.log("no message from facebook"); - } - - }); + initFBlistener(); } }); From 2c14fd1e5d2c93e371a076d5935ef127f70a7465 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 18:35:58 +0300 Subject: [PATCH 17/41] dev --- facebookbot.js | 1 + 1 file changed, 1 insertion(+) diff --git a/facebookbot.js b/facebookbot.js index 119cfa2..a39042e 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -68,6 +68,7 @@ function doAuth(msg) if (owner.chat_id == undefined) { owner.chat_id = msg.chat.id; + console.log("Late start for FB listener") if (fbReady) initFBlistener(); } From 0c132492cd5b9feefe951c9a34c037376cba2c4d Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 18:40:13 +0300 Subject: [PATCH 18/41] fix variable scope --- facebookbot.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index a39042e..f2980cb 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -193,11 +193,11 @@ initListeners(); -login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASSWORD}, async function (err, api) { +login({email: process.env.FACEBOOK_USERNAME, password: process.env.FACEBOOK_PASSWORD}, async function (err, lapi) { if (err) return console.error(err); - await retrieveFriendsFromFacebook(api); - + await retrieveFriendsFromFacebook(lapi); + api = lapi; //listen message from FB and forward to telegram if (!owner.chat_id) { From 1454d7fb9ddf7cb03626ed9b9082c0e06a5486f7 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 18:49:54 +0300 Subject: [PATCH 19/41] Appropriate location for usage info --- facebookbot.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index f2980cb..d3a6bf1 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -127,7 +127,20 @@ function initListeners() } }); }); - + + + bot.onText(/\/start/, (msg, match) => { + const chatId = msg.chat.id; + if (!doAuth(msg)) return; + + bot.sendMessage(msg.chat.id, + getUsage(),{ + disable_web_page_preview: true + }); + }); + + + bot.on('message', (msg) => { const chatId = msg.chat.id; if (!doAuth(msg)) return; @@ -179,12 +192,7 @@ function initListeners() msg.chat.id, "I do not know him, Please give me a correct name or /cancel." ); - } else { - bot.sendMessage(msg.chat.id, - getUsage(),{ - disable_web_page_preview: true - }); - } + } }); } From f8a3ee9ab4250f33d4532beea4e386a435ccc667 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 18:53:49 +0300 Subject: [PATCH 20/41] debug info --- facebookbot.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/facebookbot.js b/facebookbot.js index d3a6bf1..7f43d73 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -95,6 +95,8 @@ function initListeners() api.getThreadList(0, maxThreadNb, function callback(err, arr) { if (err) return console.error(err); + console.log("got FB Threads List") + console.log(arr) var ft = require('./lib/findThread'); var fbids = ft.getParticipantsIds(arr); currentThreadId = undefined; //reset current thread From b662bfc26eb2d25942b2c13bef7e74d72a00fb84 Mon Sep 17 00:00:00 2001 From: Aleksei Date: Mon, 16 Apr 2018 16:04:18 +0000 Subject: [PATCH 21/41] switch to dev version of facebook api lib --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 23aaaf6..4cf5df6 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "homepage": "https://github.com/Liryna/FacebookBot", "dependencies": { - "facebook-chat-api": "git+https://github.com/Schmavery/facebook-chat-api.git", + "facebook-chat-api": "git+https://github.com/ravkr/facebook-chat-api.git", "node-telegram-bot-api": "git+https://github.com/yagop/node-telegram-bot-api.git" }, "scripts": {} From 9b1bd8defcfb9734e62d1ded2e5b00d64a49adae Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 19:08:26 +0300 Subject: [PATCH 22/41] new parameters for getThreadList --- facebookbot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index 7f43d73..743d1ec 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -92,7 +92,7 @@ function initListeners() const chatId = msg.chat.id; if (!doAuth(msg)) return; - api.getThreadList(0, maxThreadNb, function callback(err, arr) { + api.getThreadList(maxThreadNb, new Date('2000-01-01T00:00:00Z'), [],function callback(err, arr) { if (err) return console.error(err); console.log("got FB Threads List") From 1547391bbe62e00bf22709a70bc1edf1f0de1f17 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 19:10:50 +0300 Subject: [PATCH 23/41] Timestamp default value --- facebookbot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index 743d1ec..48ceaaa 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -92,7 +92,7 @@ function initListeners() const chatId = msg.chat.id; if (!doAuth(msg)) return; - api.getThreadList(maxThreadNb, new Date('2000-01-01T00:00:00Z'), [],function callback(err, arr) { + api.getThreadList(maxThreadNb, 0, [],function callback(err, arr) { if (err) return console.error(err); console.log("got FB Threads List") From 5bc84414907634f24d7a05bbdcf297610956fa2d Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 19:20:03 +0300 Subject: [PATCH 24/41] null means before NOW --- facebookbot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index 48ceaaa..b45f6c8 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -92,7 +92,7 @@ function initListeners() const chatId = msg.chat.id; if (!doAuth(msg)) return; - api.getThreadList(maxThreadNb, 0, [],function callback(err, arr) { + api.getThreadList(maxThreadNb, null, [],function callback(err, arr) { if (err) return console.error(err); console.log("got FB Threads List") From 4594a7aa13d53c96f1fc0b357ccb837cb4bbc21e Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 19:26:56 +0300 Subject: [PATCH 25/41] Replacement for seemingly broken call --- facebookbot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index b45f6c8..c687f66 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -98,7 +98,7 @@ function initListeners() console.log("got FB Threads List") console.log(arr) var ft = require('./lib/findThread'); - var fbids = ft.getParticipantsIds(arr); + var fbids = [].concat.apply([], arr.map(el => el.participantIDs)); //Extract IDs and merge them into a single array currentThreadId = undefined; //reset current thread api.getUserInfo(fbids, function (err, ret) { From e2b8094908ab47002ea8d6633b6830b6dda40fd8 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 19:42:32 +0300 Subject: [PATCH 26/41] Conversation fixes --- facebookbot.js | 2 +- lib/findThread.js | 17 ++++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index c687f66..82021dd 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -98,7 +98,7 @@ function initListeners() console.log("got FB Threads List") console.log(arr) var ft = require('./lib/findThread'); - var fbids = [].concat.apply([], arr.map(el => el.participantIDs)); //Extract IDs and merge them into a single array + var fbids = arr.map(el => el.participantIDs); //Extract IDs currentThreadId = undefined; //reset current thread api.getUserInfo(fbids, function (err, ret) { diff --git a/lib/findThread.js b/lib/findThread.js index e5c7e18..15f810f 100644 --- a/lib/findThread.js +++ b/lib/findThread.js @@ -47,25 +47,20 @@ module.exports = { }, - createThreads : function(arr, ret, cb) { + createThreads : function(arr, cb) { var threadListTmp = new Array(); - var userIds = new Array(); - - for(var prop in ret) { - userIds[prop] = ret[prop].name; - } for (var i = 0; i < arr.length; i++) { if (!arr[i].name) { var conversatioName = ""; - for (var j = 0; j < arr[i].participants.length; j++) { - fbid = arr[i].participants[j].replace('fbid:',''); + for (var j = 0; j < arr[i].participantIDs.length; j++) { + fbid = arr[i].participantIDs[j]; + uname = arr[i].participants[j].name; - if (userIds[fbid]) { if (conversatioName) conversatioName += ","; - conversatioName += userIds[fbid]; - } + conversatioName += uname; + } threadListTmp.push({threadID: arr[i].threadID, name: conversatioName}); From fab777d33999e277620d21a2a00f93bb5146ba12 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 22:26:34 +0300 Subject: [PATCH 27/41] fix getUserInfo --- facebookbot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index 82021dd..c687f66 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -98,7 +98,7 @@ function initListeners() console.log("got FB Threads List") console.log(arr) var ft = require('./lib/findThread'); - var fbids = arr.map(el => el.participantIDs); //Extract IDs + var fbids = [].concat.apply([], arr.map(el => el.participantIDs)); //Extract IDs and merge them into a single array currentThreadId = undefined; //reset current thread api.getUserInfo(fbids, function (err, ret) { From 8a59dc632031eea2b40b411cda5295da6e35aba5 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 22:33:25 +0300 Subject: [PATCH 28/41] fix --- facebookbot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index c687f66..8ba6c21 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -104,7 +104,7 @@ function initListeners() api.getUserInfo(fbids, function (err, ret) { if (err) return console.error(err); - ft.createThreads(arr, ret, function (conversatioNames, newThreadListTmp) { + ft.createThreads(arr, function (conversatioNames, newThreadListTmp) { threadListTmp = newThreadListTmp; bot.sendMessage(msg.chat.id, "Who is the recipient ?",{ reply_markup: { From d10560d633a3e82c1a54519e3962c465e0f65f81 Mon Sep 17 00:00:00 2001 From: Aleksei Romanenko Date: Mon, 16 Apr 2018 22:50:02 +0300 Subject: [PATCH 29/41] more fixes --- facebookbot.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index 8ba6c21..4d91d8e 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -38,7 +38,7 @@ function initFBlistener(){ } else if (message) { console.log("Got FB message") - sonsole.log(message) + console.log(message) // gets the fb user name given his id const senderName = friends[message.senderID] || message.senderID; @@ -238,8 +238,13 @@ const sendTextMessageToTelegram = function (bot, senderName, message, text) { forwardmsg = message.threadID + ": " + forwardmsg; } - bot.sendMessage(owner.chat_id, forwardmsg) + bot.sendMessage(owner.chat_id, forwardmsg, function(err, res){ + if (err) { + return console.error(err); + } chat[res.message_id] = message.threadID; + + }); }; const sendAttachmentsToTelegram = function (bot, senderName, message) { From 10338c957622b8e40db76ac0ff00d31a4a144ffa Mon Sep 17 00:00:00 2001 From: Kreastr Date: Tue, 17 Apr 2018 08:10:11 +0300 Subject: [PATCH 30/41] Sticker support --- facebookbot.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index 4d91d8e..8dd7ca1 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -168,7 +168,18 @@ function initListeners() }); }); - } else { + } else if (msg.sticker != undefined) { + bot.getFile(msg.sticker[msg.sticker.length - 1].file_id,{ + dir: '/' + }).then( function(arr) { + api.sendMessage({attachment: fs.createReadStream(arr.destination)}, currentThreadId, function (err, api) { + fs.unlink(arr.destination, function (err) { + if (err) throw err; + }); + }); + }); + + } else{ api.sendMessage(msg.text, currentThreadId, function (err, api) { if (err) return console.error(err); From 160474dd1f39a7a7d56425a60fb50c3d3f719fca Mon Sep 17 00:00:00 2001 From: Kreastr Date: Tue, 17 Apr 2018 08:28:55 +0300 Subject: [PATCH 31/41] Correct API for file downloading --- facebookbot.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index 8dd7ca1..ccf30db 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -158,9 +158,7 @@ function initListeners() if (currentThreadId != undefined) { if (msg.photo != undefined) { - bot.getFile(msg.photo[msg.photo.length - 1].file_id,{ - dir: '/' - }).then( function(arr) { + bot.downloadFile(msg.photo[msg.photo.length - 1].file_id,'/app/').then( function(arr) { api.sendMessage({attachment: fs.createReadStream(arr.destination)}, currentThreadId, function (err, api) { fs.unlink(arr.destination, function (err) { if (err) throw err; @@ -169,9 +167,7 @@ function initListeners() }); } else if (msg.sticker != undefined) { - bot.getFile(msg.sticker[msg.sticker.length - 1].file_id,{ - dir: '/' - }).then( function(arr) { + bot.downloadFile(msg.sticker[msg.sticker.length - 1].file_id,'/app/').then( function(arr) { api.sendMessage({attachment: fs.createReadStream(arr.destination)}, currentThreadId, function (err, api) { fs.unlink(arr.destination, function (err) { if (err) throw err; From 590f4b9754fe5be3022ecafa48e49c70e2fede03 Mon Sep 17 00:00:00 2001 From: Kreastr Date: Tue, 17 Apr 2018 08:44:00 +0300 Subject: [PATCH 32/41] Fixes --- facebookbot.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index ccf30db..f319099 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -158,18 +158,22 @@ function initListeners() if (currentThreadId != undefined) { if (msg.photo != undefined) { - bot.downloadFile(msg.photo[msg.photo.length - 1].file_id,'/app/').then( function(arr) { - api.sendMessage({attachment: fs.createReadStream(arr.destination)}, currentThreadId, function (err, api) { - fs.unlink(arr.destination, function (err) { - if (err) throw err; - }); - }); - }); + for each (ph in msg.photo){ + bot.downloadFile(ph.file_id,'/app/').then( function(arr) { + api.sendMessage({attachment: fs.createReadStream(arr)}, currentThreadId, function (err, api) { + fs.unlink(arr, function (err) { + if (err) throw err; + }); + }); + }); + } } else if (msg.sticker != undefined) { - bot.downloadFile(msg.sticker[msg.sticker.length - 1].file_id,'/app/').then( function(arr) { - api.sendMessage({attachment: fs.createReadStream(arr.destination)}, currentThreadId, function (err, api) { - fs.unlink(arr.destination, function (err) { + bot.downloadFile(msg.sticker.file_id,'/app/').then( function(arr) { + console.log("Got path") + console.log(arr) + api.sendMessage({attachment: fs.createReadStream(arr)}, currentThreadId, function (err, api) { + fs.unlink(arr, function (err) { if (err) throw err; }); }); From a32e158e21e0bc72fcfe4bca55b2987a7de4897e Mon Sep 17 00:00:00 2001 From: Kreastr Date: Tue, 17 Apr 2018 08:48:56 +0300 Subject: [PATCH 33/41] old standards --- facebookbot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index f319099..b573deb 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -158,7 +158,7 @@ function initListeners() if (currentThreadId != undefined) { if (msg.photo != undefined) { - for each (ph in msg.photo){ + for (var ph in msg.photo){ bot.downloadFile(ph.file_id,'/app/').then( function(arr) { api.sendMessage({attachment: fs.createReadStream(arr)}, currentThreadId, function (err, api) { fs.unlink(arr, function (err) { From 7e6f40bf9765ac464539b4ef5bcc348b9d13b411 Mon Sep 17 00:00:00 2001 From: Kreastr Date: Tue, 17 Apr 2018 08:52:10 +0300 Subject: [PATCH 34/41] Reverting to correct logic error --- facebookbot.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index b573deb..28ac895 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -158,15 +158,14 @@ function initListeners() if (currentThreadId != undefined) { if (msg.photo != undefined) { - for (var ph in msg.photo){ - bot.downloadFile(ph.file_id,'/app/').then( function(arr) { + bot.downloadFile(msg.photo[msg.photo.length - 1].file_id,'/app/').then( function(arr) { api.sendMessage({attachment: fs.createReadStream(arr)}, currentThreadId, function (err, api) { fs.unlink(arr, function (err) { if (err) throw err; }); }); }); - } + } else if (msg.sticker != undefined) { bot.downloadFile(msg.sticker.file_id,'/app/').then( function(arr) { From 8a0f1794efb6963cc67cf0b46733dce72b7d0b7f Mon Sep 17 00:00:00 2001 From: Kreastr Date: Tue, 17 Apr 2018 08:56:25 +0300 Subject: [PATCH 35/41] Filter commands from chat --- facebookbot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index 28ac895..ba0d561 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -146,7 +146,7 @@ function initListeners() bot.on('message', (msg) => { const chatId = msg.chat.id; if (!doAuth(msg)) return; - + if (msg.text[0] == '/') return; if (!!msg.reply_to_message && !!chat[msg.reply_to_message.message_id]) { //it is a reply message from FB From 630bea0a00e0f5c5c587153b5ae30c40b3b81827 Mon Sep 17 00:00:00 2001 From: Kreastr Date: Tue, 17 Apr 2018 09:18:56 +0300 Subject: [PATCH 36/41] debug info --- facebookbot.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index ba0d561..70210bd 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -247,7 +247,8 @@ const sendTextMessageToTelegram = function (bot, senderName, message, text) { if (message.isGroup) { forwardmsg = message.threadID + ": " + forwardmsg; } - + console.log("Forwarding message to TG: "); + console.log(" - " + forwardmsg); bot.sendMessage(owner.chat_id, forwardmsg, function(err, res){ if (err) { return console.error(err); From 402be67c5c6d46987054f9efe9b8806649196b36 Mon Sep 17 00:00:00 2001 From: Kreastr Date: Wed, 18 Apr 2018 12:58:41 +0300 Subject: [PATCH 37/41] correct promise --- facebookbot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index 70210bd..5ebe9b1 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -249,7 +249,7 @@ const sendTextMessageToTelegram = function (bot, senderName, message, text) { } console.log("Forwarding message to TG: "); console.log(" - " + forwardmsg); - bot.sendMessage(owner.chat_id, forwardmsg, function(err, res){ + bot.sendMessage(owner.chat_id, forwardmsg).then(function(err, res){ if (err) { return console.error(err); } From 44b39488f18c3617cae83183e9d1c267c0d5d255 Mon Sep 17 00:00:00 2001 From: Kreastr Date: Thu, 19 Apr 2018 10:14:39 +0300 Subject: [PATCH 38/41] fixes and debug info --- facebookbot.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/facebookbot.js b/facebookbot.js index 5ebe9b1..8b46f8a 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -176,6 +176,11 @@ function initListeners() if (err) throw err; }); }); + }).catch(function(arr) { + api.sendMessage(msg.sticker.emoji + " ("+msg.sticker.set_name+")", + currentThreadId, function (err, api) { + if (err) return console.error(err); + }); }); } else{ @@ -254,7 +259,7 @@ const sendTextMessageToTelegram = function (bot, senderName, message, text) { return console.error(err); } chat[res.message_id] = message.threadID; - + console.log(chat); }); }; From 763f9aea4cef2f029a88a12f44a94d75e01ef24d Mon Sep 17 00:00:00 2001 From: Vucodil Date: Tue, 29 May 2018 21:38:13 +0200 Subject: [PATCH 39/41] update README.md with the rigth dependencies --- README.md | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index dd3a6fe..28de746 100644 --- a/README.md +++ b/README.md @@ -6,29 +6,24 @@ A Facebook bot for Telegram. ## Install - Create your Telegram bot, follow the instruction [here](https://core.telegram.org/bots#3-how-do-i-create-a-bot). -- Clone this repository and install nodejs package. +- Clone this repository. +- Install nodejs package. ``` -git clone https://github.com/Liryna/FacebookBot.git cd FacebookBot npm install ``` -- Create the configuration file `config.json`. -``` -{ - "email": "YOUR EMAIL", - "password": "YOUR PASSWORD", - "dir": "FacebookBot DIRECTORY" -} -``` + ##Usage -- This bot require some additional information in the your environment variables. +- This bot requires some additional information in your environment variables. ``` -TELEGRAM_USER="your telegram login" -APP_TOKEN="the token of your bot that BotFather gave you" +TELEGRAM_USER_ID="your telegram user id" +TELEGRAM_TOKEN="the token of your bot that BotFather gave you" +FACEBOOK_USERNAME="your facebook username" +FACEBOOK_PASSWORD="your facebook password" ``` - Run it. ``` -TELEGRAM_USER="Mario" APP_TOKEN="TOKEN" node facebookbot.js +node facebookbot.js ``` - The bot cannot establish a conversation to you directly, you need to write him first. Use your Telegram Client to say him "Hello". - The Available commands to send to your bot: @@ -39,4 +34,4 @@ TELEGRAM_USER="Mario" APP_TOKEN="TOKEN" node facebookbot.js ## Dependencies -This bot use [Schmavery/facebook-chat-api](https://github.com/Schmavery/facebook-chat-api) and [depoio/node-telegram-bot](https://github.com/depoio/node-telegram-bot). +This bot use [ravkr/facebook-chat-api](https://github.com/ravkr/facebook-chat-api) and [yagop/node-telegram-bot-api](https://github.com/yagop/node-telegram-bot-api). From 56595ee2859234c3d99d9c5507ecb628943e775d Mon Sep 17 00:00:00 2001 From: goldenapple Date: Fri, 29 Jun 2018 18:20:54 +0300 Subject: [PATCH 40/41] Fix attachments --- facebookbot.js | 78 +++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index 8b46f8a..7fa7df7 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -17,7 +17,9 @@ function getUsage() { + "\n\nMore Informations: https://github.com/Kreastr/FacebookBot"; } -var chat = new Array(); +//when a Facebook message is fowarded to user: +//key = TG message id, value = FB thread id +var chat = new Map(); var friends = {}; var threadListTmp; var currentThreadId; @@ -146,46 +148,34 @@ function initListeners() bot.on('message', (msg) => { const chatId = msg.chat.id; if (!doAuth(msg)) return; - if (msg.text[0] == '/') return; + if (msg.text != undefined && msg.text[0] == '/') return; if (!!msg.reply_to_message - && !!chat[msg.reply_to_message.message_id]) { //it is a reply message from FB + && !!chat.get(msg.reply_to_message.message_id)) { //it is a reply message from FB api.sendMessage(msg.text, - chat[msg.reply_to_message.message_id], function (err, api) { + chat.get(msg.reply_to_message.message_id), function (err, messageInfo) { if (err) return console.error(err); }); } if (currentThreadId != undefined) { if (msg.photo != undefined) { - bot.downloadFile(msg.photo[msg.photo.length - 1].file_id,'/app/').then( function(arr) { - api.sendMessage({attachment: fs.createReadStream(arr)}, currentThreadId, function (err, api) { - fs.unlink(arr, function (err) { - if (err) throw err; - }); - }); - }); - - + sendAttachmentToFacebook(currentThreadId, msg.chat.id, msg.photo[msg.photo.length - 1].file_id, msg.caption); } else if (msg.sticker != undefined) { - bot.downloadFile(msg.sticker.file_id,'/app/').then( function(arr) { - console.log("Got path") - console.log(arr) - api.sendMessage({attachment: fs.createReadStream(arr)}, currentThreadId, function (err, api) { - fs.unlink(arr, function (err) { - if (err) throw err; - }); - }); - }).catch(function(arr) { - api.sendMessage(msg.sticker.emoji + " ("+msg.sticker.set_name+")", - currentThreadId, function (err, api) { - if (err) return console.error(err); - }); - }); - - } else{ + sendAttachmentToFacebook(currentThreadId, msg.chat.id, msg.sticker.file_id); + } else if (msg.audio != undefined) { + sendAttachmentToFacebook(currentThreadId, msg.chat.id, msg.audio.file_id, msg.caption); + } else if (msg.voice != undefined) { + sendAttachmentToFacebook(currentThreadId, msg.chat.id, msg.voice.file_id, msg.caption); + } else if (msg.video != undefined) { + sendAttachmentToFacebook(currentThreadId, msg.chat.id, msg.video.file_id, msg.caption); + } else if (msg.video_note != undefined) { + sendAttachmentToFacebook(currentThreadId, msg.chat.id, msg.video_note.file_id); + } else if (msg.document != undefined) { + sendAttachmentToFacebook(currentThreadId, msg.chat.id, msg.document.file_id, msg.caption); + } else { api.sendMessage(msg.text, - currentThreadId, function (err, api) { + currentThreadId, function (err, messageInfo) { if (err) return console.error(err); }); } @@ -254,12 +244,9 @@ const sendTextMessageToTelegram = function (bot, senderName, message, text) { } console.log("Forwarding message to TG: "); console.log(" - " + forwardmsg); - bot.sendMessage(owner.chat_id, forwardmsg).then(function(err, res){ - if (err) { - return console.error(err); - } - chat[res.message_id] = message.threadID; - console.log(chat); + bot.sendMessage(owner.chat_id, forwardmsg).then(function(err, tgMessage) { + if(err) throw err; + chat.set(tgMessage.message_id, message.thread_id); }); }; @@ -279,6 +266,25 @@ const sendAttachmentsToTelegram = function (bot, senderName, message) { } } +const sendAttachmentToFacebook = function (fbThreadID, tgChatID, file_id, caption) { + if(!process.env.FACEBOOK_BOT_DIR) { + bot.sendMessage(tgChatID, "Bot cache directory (FACEBOOK_BOT_DIR environment var) is not defiend, cannot send attachments."); + return; + } + //would be cool if this worked, can't bother to fix though + // api.sendMessage({body: caption, attachment: bot.getFileStream(file_id)}, fbThreadID, function (err, messageInfo) { + // if(err) throw err; + // }); + + return bot.downloadFile(file_id, process.env.FACEBOOK_BOT_DIR).then( function(arr) { + api.sendMessage({body: caption, attachment: fs.createReadStream(arr)}, fbThreadID, function (err, messageInfo) { + fs.unlink(arr, function (err) { + if (err) throw err; + }); + }); + }) +} + function reset() { currentThreadId = undefined; threadListTmp = undefined; From a181ee34be35106176d3a88c67b6ec57c3829192 Mon Sep 17 00:00:00 2001 From: goldenapple Date: Sat, 30 Jun 2018 15:20:09 +0300 Subject: [PATCH 41/41] No longer neccessary to specify working directory. --- facebookbot.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/facebookbot.js b/facebookbot.js index 7fa7df7..7ebeab8 100644 --- a/facebookbot.js +++ b/facebookbot.js @@ -17,6 +17,8 @@ function getUsage() { + "\n\nMore Informations: https://github.com/Kreastr/FacebookBot"; } +var botWorkingDir = process.env.FACEBOOK_BOT_DIR || (__dirname + "/data"); + //when a Facebook message is fowarded to user: //key = TG message id, value = FB thread id var chat = new Map(); @@ -204,6 +206,14 @@ function initListeners() }); } + +if (!fs.existsSync(botWorkingDir)) { + fs.mkdirSync(botWorkingDir); +} +if (!fs.existsSync(botWorkingDir + "/temp")){ + fs.mkdirSync(botWorkingDir + "/temp"); +} +console.log("Data directory: " + botWorkingDir); initListeners(); @@ -267,16 +277,12 @@ const sendAttachmentsToTelegram = function (bot, senderName, message) { } const sendAttachmentToFacebook = function (fbThreadID, tgChatID, file_id, caption) { - if(!process.env.FACEBOOK_BOT_DIR) { - bot.sendMessage(tgChatID, "Bot cache directory (FACEBOOK_BOT_DIR environment var) is not defiend, cannot send attachments."); - return; - } //would be cool if this worked, can't bother to fix though // api.sendMessage({body: caption, attachment: bot.getFileStream(file_id)}, fbThreadID, function (err, messageInfo) { // if(err) throw err; // }); - return bot.downloadFile(file_id, process.env.FACEBOOK_BOT_DIR).then( function(arr) { + return bot.downloadFile(file_id, botWorkingDir + "/temp").then( function(arr) { api.sendMessage({body: caption, attachment: fs.createReadStream(arr)}, fbThreadID, function (err, messageInfo) { fs.unlink(arr, function (err) { if (err) throw err;