Last active
April 24, 2018 15:12
-
-
Save valiafetisov/191220be1c9b74d62a75969e69f30fca to your computer and use it in GitHub Desktop.
getAnswer from otvet.mail.ru as a chat-bot
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
{ | |
"password": "your_password", | |
"login": "your_login" | |
} |
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
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 |
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
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