├── Procfile ├── README.md ├── index.js ├── package.json ├── .gitignore ├── ScrabbleDictionary └── index.js ├── TrainTimetable └── index.js └── EchoBot └── index.js /Procfile: -------------------------------------------------------------------------------- 1 | web: node . 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bottr-Examples 2 | 3 | Examples for the Bottr Framework. 4 | 5 | Hosted at [bottr.co/examples](http://bottr.co/examples) 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Bottr = require('bottr') 2 | 3 | var server = new Bottr.Server() 4 | 5 | // Start EchoBot Example 6 | server.use('/echobot', require('./EchoBot')) 7 | 8 | // Start Train Timetable Example 9 | server.use('/train-timetable', require('./TrainTimetable')) 10 | 11 | // Start Scrabble Dictionary Example 12 | server.use('/scrabble-dictionary', require('./ScrabbleDictionary')) 13 | 14 | var port = process.env.PORT || 3000 15 | console.log('Examples listening on port ' + port) 16 | server.listen(port) 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "examples", 3 | "version": "1.0.0", 4 | "description": "Examples for the Botkit.js Framework", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/Bottr-js/Bottr-Examples.git" 12 | }, 13 | "author": "jcampbell_05", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/Bottr-js/Bottr-Examples/issues" 17 | }, 18 | "homepage": "https://github.com/Bottr-js/Bottr-Examples#readme", 19 | "dependencies": { 20 | "bottr": "^0.1.0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | .hubot_history 39 | 40 | # Heroku Enviroments 41 | .env 42 | 43 | # Bot Data 44 | data/ 45 | -------------------------------------------------------------------------------- /ScrabbleDictionary/index.js: -------------------------------------------------------------------------------- 1 | var Bottr = require('bottr') 2 | var fs = require('fs'); 3 | var bot = new Bottr.Bot(); 4 | 5 | var contents = fs.readFileSync(__dirname + '/words.csv', 'utf8'); 6 | var dictionary = contents.split('\n').filter(function(word){ 7 | return word.length > 1 && /^[a-zA-Z]+$/.test(word) 8 | }) 9 | 10 | bot.on('message_received', function(message, session) { 11 | 12 | var letters = message.text.split("").filter(function(letter){ 13 | return letter != " " 14 | }) 15 | 16 | var words = dictionary.filter(function(word) { 17 | 18 | var detectedLetters = letters.filter(function(letter){ 19 | return word.indexOf(letter) >= 0 20 | }) 21 | 22 | return detectedLetters.length >= word.length 23 | }) 24 | 25 | words = words.slice(0, Math.min(30, words.length - 1)) 26 | 27 | session.send("You can make these words with the letters: " + letters.join(" ")) 28 | session.send(words.join(", ")) 29 | }); 30 | 31 | module.exports = bot 32 | -------------------------------------------------------------------------------- /TrainTimetable/index.js: -------------------------------------------------------------------------------- 1 | var Bottr = require('bottr') 2 | var bot = new Bottr.Bot('TrainTimesBot'); 3 | 4 | var timetable = [ 5 | { 6 | time: "7:00", 7 | destination: "London" 8 | }, 9 | { 10 | time: "8:00", 11 | destination: "Swindon" 12 | }, 13 | { 14 | time: "9:00", 15 | destination: "London" 16 | }, 17 | { 18 | time: "10:00", 19 | destination: "Swindon" 20 | }, 21 | { 22 | time: "11:00", 23 | destination: "London" 24 | }, 25 | { 26 | time: "12:00", 27 | destination: "Swindon" 28 | } 29 | ] 30 | 31 | bot.hears(/timetable/i, function(message, session) { 32 | 33 | var rows = timetable.map(function(item){ 34 | return item.time + " " + item.destination 35 | }) 36 | 37 | session.send("The timetable for today is: \n" + rows.join("\n")) 38 | }); 39 | 40 | bot.hears([/thanks/i, /cheers/i, /thank you/i, /ta/i, /thx/i, /ty/i], function(message, session) { 41 | session.send("No problem!") 42 | }); 43 | 44 | module.exports = bot 45 | -------------------------------------------------------------------------------- /EchoBot/index.js: -------------------------------------------------------------------------------- 1 | var Bottr = require('bottr') 2 | var fs = require('fs'); 3 | var bot = new Bottr.Bot(); 4 | 5 | bot.use(new Bottr.FacebookMessengerClient()) 6 | bot.use(new Bottr.MemoryStorage()) 7 | 8 | bot.on('message_received', function(message, session, next) { 9 | 10 | // Get existing context or use defaults if values don't exist 11 | var context = session.getUserContext({ 12 | messageCount: 0, 13 | wordCount: 0, 14 | }) 15 | 16 | // Calculate new statistics 17 | context.messageCount ++ 18 | 19 | var words = message.text.split(" ") 20 | context.wordCount += words.length 21 | 22 | //Store new values into context 23 | session.updateUserContext(context) 24 | 25 | //Tell bot to let other handlers process the message 26 | next() 27 | }); 28 | 29 | bot.hears(/\/stats/i, function(message, session) { 30 | 31 | var context = session.getUserContext() 32 | 33 | // Send the total number of messages to the 34 | // user 35 | session.send("Total Message Count: " + context.messageCount) 36 | 37 | // Send the total number of words to the 38 | // user 39 | session.send("Total Word Count: " + context.wordCount) 40 | }); 41 | 42 | bot.hears([/.+/], function(message, session) { 43 | // Repeat what the user sent us 44 | session.send(message.text) 45 | }); 46 | 47 | module.exports = bot 48 | --------------------------------------------------------------------------------