├── .gitignore ├── index.js ├── lib ├── matchers.js └── bot.js ├── package.json ├── README.md └── LICENSE.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Bot = require('./lib/bot'); 4 | 5 | exports.Bot = Bot; 6 | -------------------------------------------------------------------------------- /lib/matchers.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function isMessage(ev, s) { 4 | return (ev.type === 'message' && ev.text === s); 5 | } 6 | 7 | function beginsWith(ev, s) { 8 | return (ev.type === 'message' && ev.text.substring(0, s.length) === s); 9 | } 10 | 11 | function isJoining(ev) { 12 | return ev.type === 'channel_joined'; 13 | } 14 | 15 | module.exports = { 16 | isMessage: isMessage, 17 | beginsWith: beginsWith, 18 | isJoining: isJoining 19 | } 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "botstation", 3 | "version": "0.0.1", 4 | "description": "Tiny framework to build your own chatbot", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+ssh://git@github.com/uniquid/botstation.git" 12 | }, 13 | "keywords": [ 14 | "bot", 15 | "slack", 16 | "facebook", 17 | "messenger", 18 | "chatops", 19 | "chatbot", 20 | "automation" 21 | ], 22 | "author": "Alessio Biancalana", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/uniquid/botstation/issues" 26 | }, 27 | "homepage": "https://github.com/uniquid/botstation#readme", 28 | "dependencies": { 29 | "slackbots": "^1.0.2" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Botstation 2 | Botstation is a tiny framework made to build your own chatbot fast. 3 | 4 | ## Installation 5 | ``` 6 | npm install --save botstation 7 | ``` 8 | 9 | ## Usage 10 | ```js 11 | var Botstation = require('botstation'); 12 | var bot = new Botstation.Bot(); 13 | 14 | bot.config({ 15 | key: 'xoxb-157105193143-eJdSjJRttSCU2DAd3IdAtBoi', 16 | name: 'Dat Bot' 17 | }); 18 | 19 | bot.task({ 20 | name: "Help", 21 | matcher: "isMessage", 22 | content: "!help", 23 | exec: (bot, data, params) => { 24 | bot.postMessage(data.channel, 'Do you need help?', params); 25 | } 26 | }); 27 | 28 | bot.start(() => { 29 | console.log('Bot started!'); 30 | }); 31 | ``` 32 | 33 | ## Matchers 34 | You can configure tasks to match in several ways with incoming messages: 35 | 36 | - `isMessage`: Checks if the incoming message is strictly equal to the specified content 37 | - `beginsWith`: Checks if an incoming message begins with the specified content string 38 | - `isJoining`: Looks for channel joining events in incoming messages, so the bot can advertise itself 39 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Uniquid Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/bot.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const SlackBot = require('slackbots'); 4 | const matchers = require('./matchers'); 5 | 6 | function match(data, set) { 7 | return new Promise(function(resolve, reject) { 8 | var task = set.find(t => matchers[t.matcher](data, t.content)); 9 | 10 | if (!task) { return reject({error: "Task not found"}); } 11 | resolve(task); 12 | }); 13 | } 14 | 15 | function parse(data, ctx) { 16 | match(data, ctx.taskSet).then(function(task) { 17 | task.exec(ctx.bot.instance, data, ctx.bot.params); 18 | }).catch(function() { 19 | return; 20 | }); 21 | } 22 | 23 | function storeConfig(configObj) { 24 | Object.assign(this.configStore, configObj) 25 | } 26 | 27 | function task(t) { 28 | this.taskSet.push(t); 29 | } 30 | 31 | function start(cb) { 32 | var slackbot = new SlackBot({ 33 | token: this.configStore.key, 34 | name: this.configStore.name 35 | }); 36 | 37 | var ctx = this; 38 | 39 | this.bot.instance = slackbot; 40 | 41 | slackbot.on('message', function(data) { 42 | parse(data, ctx) 43 | }); 44 | 45 | cb() 46 | } 47 | 48 | function Bot() { 49 | this.configStore = { 50 | key: "", 51 | name: "A Simple Bot" 52 | }; 53 | this.taskSet = []; 54 | this.bot = { 55 | instance: {}, 56 | params: {as_user: true} 57 | } 58 | this.config = storeConfig; 59 | this.task = task; 60 | this.start = start; 61 | this.parse = parse; 62 | this.match = match; 63 | } 64 | 65 | module.exports = Bot 66 | --------------------------------------------------------------------------------