Last active
August 7, 2017 17:26
-
-
Save fr34kyn01535/538ce430514c35b376aff840088f4588 to your computer and use it in GitHub Desktop.
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
Twitter App erstellen | |
https://apps.twitter.com/ | |
nodejs LTS installieren | |
cd in den Ordner | |
npm install | |
zum starten node index.js | |
Momentan lauscht der Bot auf #fr34kyn01535.answerbot und antwortet mit einem Hi |
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
"use strict"; | |
let questions = require('./questions.json'); | |
function getAnswer(questionText){ | |
let answers = []; | |
let words = questionText.split(" "); | |
words = words.map(n => n.toLowerCase()); | |
words = words.filter(n => n != ""); | |
for(let question of questions){ | |
let weight = 0; | |
for(let indicator of question.indicators){ | |
if(words.includes(indicator.word.toLowerCase())) weight+=indicator.weight; | |
} | |
answers.push({answer:question.answer,totalWeight:weight}); | |
} | |
let answer = answers.sort((a,b) => b.totalWeight-a.totalWeight)[0]; | |
let answerText = answer.answer; | |
if(answer.totalWeight < 200){ | |
answerText = "Entschuldigung ich verstehe deine Frage nicht. Kannst du sie anders formulieren?"; | |
} | |
return answerText; | |
} | |
let credentials = { | |
consumer_key: '', | |
consumer_secret: '', | |
access_token_key: '', | |
access_token_secret: '' | |
}; | |
let userId = credentials.access_token_key.split("-")[0]; | |
let Twitter = new require('twitter')(credentials); | |
Twitter.stream('user', { | |
stringify_friend_ids: true | |
},function(stream){ | |
stream.on('data', function (data) { | |
if(data.direct_message && data.direct_message.sender.id != userId){ | |
Twitter.post('direct_messages/new', {user_id:data.direct_message.sender.id_str,text: getAnswer(data.direct_message.text)}, function(error, message, response) { | |
if (error) { | |
console.log(message); | |
} | |
}); | |
} | |
}); | |
}); | |
Twitter.stream('statuses/filter', { | |
track: '@fr34kyn01535' | |
}, function(stream) { | |
stream.on('data', function(tweet) { | |
Twitter.post('statuses/update', { | |
status: getAnswer(tweet.text), | |
in_reply_to_status_id: tweet.id_str | |
}, function(error, tweetReply, response) { | |
if (error) { | |
console.log(error); | |
} | |
}); | |
}); | |
stream.on('error', function(error) { | |
console.log(error); | |
}); | |
}); |
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
{ | |
"name": "answerbot", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"twitter": "^1.7.1" | |
} | |
} |
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
[ | |
{ | |
"indicators": [ | |
{ "word":"wie","weight":100 }, | |
{ "word":"alt","weight":100 }, | |
{ "word":"geboren","weight":100 }, | |
{ "word":"geburtag","weight":100 }, | |
{ "word":"alter","weight":100 }, | |
{ "word":"bist","weight":100 }, | |
{ "word":"du","weight":100 }, | |
{ "word":"mein","weight":-200 }, | |
{ "word":"dein","weight":100 } | |
] | |
,"answer": "Ich bin 1 Jahr alt" | |
}, | |
{ | |
"indicators": [ | |
{ "word":"wie","weight":100 }, | |
{ "word":"ist","weight":50 }, | |
{ "word":"dein","weight":50 }, | |
{ "word":"name","weight":100 }, | |
{ "word":"mein","weight":-200 }, | |
{ "word":"namen","weight":100 } | |
] | |
,"answer": "Ich heiße Olaf" | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment