Skip to content

Instantly share code, notes, and snippets.

@valiafetisov
Last active April 24, 2018 11:52
Show Gist options
  • Save valiafetisov/7b5426a6dbb1405bda1ad85549b54301 to your computer and use it in GitHub Desktop.
Save valiafetisov/7b5426a6dbb1405bda1ad85549b54301 to your computer and use it in GitHub Desktop.
facebook chatbots
var login = require('facebook-chat-api')
var config = require('./config.json')
var util = require('util')
var loginPromise = util.promisify(login)
var setup = async function () {
var bot1 = await loginPromise({email: config.bot1.login, password: config.bot1.password})
var bot2 = await loginPromise({email: config.bot2.login, password: config.bot2.password1})
bot1.listen((err, message) => {
console.log('message', message)
bot1.sendMessage(message.body, message.threadID)
})
bot2.listen((err, message) => {
console.log('message', message)
bot2.sendMessage(message.body, message.threadID)
})
}
setup()
var login = require('facebook-chat-api')
var config = require('./config.json')
login({email: config.login1, password: config.password1}, function (err, bot1) {
if(err) return console.error(err)
login({email: config.login2, password: config.password2}, function (err, bot2) {
if(err) return console.error(err)
bot1.listen((err, message) => {
console.log('message', message)
bot1.sendMessage(message.body, message.threadID)
})
bot2.listen((err, message) => {
console.log('message', message)
bot2.sendMessage(message.body, message.threadID)
})
})
})
var login = require('facebook-chat-api')
var config = require('./config.json')
var poetry = require('./poetry.json')
var util = require('util')
var setTimeoutPromise = util.promisify(setTimeout)
const ourThread = '1776874859000345'
login({email: config.login, password: config.password}, async function (err, bot) {
if(err) return console.error(err)
bot.sendMessage = util.promisify(bot.sendMessage)
for (var line of poetry.lines) {
await setTimeoutPromise(line.delay)
await bot.sendMessage(line.text, ourThread)
}
})
{
"lines": [
{"who": "bot1", "delay": 0, "text": "сижу у окна"},
{"who": "bot1", "delay": 10000, "text": "солнце светит"},
{"who": "bot1", "delay": 20000, "text": "хорошо"}
]
}
var login = require('facebook-chat-api')
var config = require('./config.json')
const ourThread = '1776874859000345'
login({email: config.login, password: config.password}, function (err, bot) {
if(err) return console.error(err)
bot.sendMessage('привет!', ourThread)
bot.sendMessage('как дела?', ourThread)
})
var login = require('facebook-chat-api')
var config = require('./config.json')
var poetry = require('./poetry.json')
var util = require('util')
var fs = require('fs')
var setTimeoutPromise = util.promisify(setTimeout)
const picturesPath = '/Users/user/Pictures'
const files = fs.readdirSync(picturesPath)
const pictures = files.filter(function (file) {
return file.indexOf('.jpg') > 0
})
const getRandomPicture = function () {
const randomFilename = pictures[Math.floor(pictures.length * Math.random())]
return picturesPath + '/' + randomFilename
}
const ourThread = '1776874859000345'
const sendPoetry = async function (bot, threadID) {
for (var line of poetry.lines) {
await setTimeoutPromise(line.delay)
await bot.sendMessage(line.text, threadID)
}
}
const sendPicture = function (bot, threadID) {
bot.sendMessage({
attachment: fs.createReadStream(getRandomPicture())
}, threadID)
}
login({email: config.login, password: config.password}, function (err, bot) {
if(err) return console.error(err)
bot.sendMessage = util.promisify(bot.sendMessage)
bot.listen((err, message) => {
if (message.body.toLowerCase() === 'расскажи стишок') {
sendPoetry(bot, message.threadID)
}
if (message.body.toLowerCase() === 'отправь фотографию') {
sendPicture(bot, message.threadID)
}
})
})
var login = require('facebook-chat-api')
var config = require('./config.json')
// вместо FB_EMAIL и FB_PASSWORD нужно ввести
// свои реальные логин и пароль от фейсбука
login({email: config.login, password: config.password}, function (err, bot) {
// это строка, при наличии ошибки логина, выведет ее в консоль
if(err) return console.error(err)
// эта функция вызовется при получении нового сообщения
bot.listen((err, message) => {
console.log('message', message)
// эта функция отправит обратно то же сообщение
bot.sendMessage(message.body, message.threadID)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment