Skip to content

Instantly share code, notes, and snippets.

@valiafetisov
Last active April 24, 2018 15:12
Show Gist options
  • Save valiafetisov/191220be1c9b74d62a75969e69f30fca to your computer and use it in GitHub Desktop.
Save valiafetisov/191220be1c9b74d62a75969e69f30fca to your computer and use it in GitHub Desktop.
getAnswer from otvet.mail.ru as a chat-bot
{
"password": "your_password",
"login": "your_login"
}
const request = require('request')
const striptags = require('striptags')
const getAnswer = function (text, callback) {
const url = 'https://go.mail.ru/answer_json?q='
request(url + encodeURIComponent(text), function (error, status, body) {
if (error) return console.error('error', error)
const json = JSON.parse(body)
console.log('json', json)
if (json.results[0] == null) return
const answer = striptags(json.results[0].answer)
if (callback) callback(answer)
})
}
module.exports = getAnswer
var login = require('facebook-chat-api')
var getAnswer = require('./getAnswer.js')
var config = require('./config.json')
login({email: config.login, password: config.password}, function (err, bot) {
if(err) return console.error(err)
bot.listen((err, message) => {
if (err || message.body == null) return
const isQuestion = message.body.lastIndexOf('?') === message.body.length - 1
if (isQuestion) {
getAnswer(message.body, function (answer) {
bot.sendMessage({body: answer}, message.threadID)
})
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment