├── chatterbox-tests.js ├── .versions ├── package.js ├── chatterbox.es6.js └── README.md /chatterbox-tests.js: -------------------------------------------------------------------------------- 1 | // Write your tests here! 2 | // Here is an example. 3 | Tinytest.add('example', function (test) { 4 | test.equal(true, true); 5 | }); 6 | -------------------------------------------------------------------------------- /.versions: -------------------------------------------------------------------------------- 1 | base64@1.0.3 2 | binary-heap@1.0.3 3 | callback-hook@1.0.3 4 | check@1.0.5 5 | ddp@1.1.0 6 | ejson@1.0.6 7 | geojson-utils@1.0.3 8 | grigio:babel@0.1.8 9 | id-map@1.0.3 10 | json@1.0.3 11 | logging@1.0.7 12 | matb33:collection-hooks@0.7.13 13 | meteor@1.1.6 14 | minimongo@1.0.8 15 | mongo@1.1.0 16 | ordered-dict@1.0.3 17 | poetic:chatterbox@0.0.2 18 | random@1.0.3 19 | retry@1.0.3 20 | tracker@1.0.7 21 | underscore@1.0.3 22 | -------------------------------------------------------------------------------- /package.js: -------------------------------------------------------------------------------- 1 | Package.describe({ 2 | name: 'poetic:chatterbox', 3 | version: '0.0.3', 4 | summary: 'chat bot for meteor apps', 5 | git: 'https://github.com/poetic/chatterbox', 6 | documentation: 'README.md' 7 | }) 8 | 9 | Package.onUse(function(api) { 10 | api.versionsFrom('METEOR@1.2'); 11 | 12 | api.use('matb33:collection-hooks@0.7.13') 13 | api.use('grigio:babel@0.1.8') 14 | 15 | api.imply('matb33:collection-hooks@0.7.13') 16 | api.addFiles('chatterbox.es6.js', 'server') 17 | api.export('Chatterbox') 18 | }) 19 | -------------------------------------------------------------------------------- /chatterbox.es6.js: -------------------------------------------------------------------------------- 1 | Chatterbox = class Chatterbox { 2 | constructor (name){ 3 | check(name, String) 4 | 5 | if (name) { 6 | this.name = name 7 | 8 | } else { 9 | throw new Error('Chatterbox must be initialized with a name.') 10 | } 11 | } 12 | 13 | listen (collection, params, callback){ 14 | check(params, { 15 | listenTo: String, listenFor: RegExp, chatAs: String 16 | }) 17 | check(callback, Function) 18 | 19 | collection.after.insert((userId, doc) => { 20 | // make sure the bot does not reply to itself 21 | if (doc[params.chatAs] === this.name) { return } 22 | 23 | if (params.listenFor.test(doc[params.listenTo])) { 24 | callback(userId, doc) 25 | } 26 | }) 27 | } 28 | 29 | chat (collection, params){ 30 | check(params, {chatAs: String, message: Object}) 31 | 32 | params.message[params.chatAs] = this.name 33 | return collection.insert(params.message) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Chatterbox 2 | 3 | # Installation 4 | 5 | `meteor add poetic:chatterbox` 6 | 7 | # Usage 8 | 9 | First initialize your chatbot with the `Chatterbox` constructor, passing a name as the only parameter. 10 | 11 | ``` 12 | Chatbot = new Chatterbox('Bot'); 13 | ``` 14 | 15 | Then call `listen` on your new Chatbot. 16 | 17 | ``` 18 | Chatbot.listen(Posts, {listenTo: 'body', listenFor: /testing/, chatAs: 'author'}, (userId, post) => { 19 | // userId and post are the same objects passed from the collection hooks package 20 | }) 21 | ``` 22 | 23 | Your Chatbot will now listen to the `body` property on every new post that is created. When the post's body passes a regex test against your `listenFor` argument the callback will be run. 24 | 25 | If you'd like your Chatbot to get in on the conversation simply call `chat`. 26 | 27 | ``` 28 | Chatbot.chat(Posts, {chatAs: 'author', 29 | message: { 30 | // properties of your Posts collection go here 31 | } 32 | }) 33 | ``` 34 | 35 | Make sure that, whether you `chat` or `listen`, always `chatAs` the same property, or your bot will start talking to itself. The above example will yield a post by the Chatbot that has the name `Bot` attached to the `author` property. 36 | --------------------------------------------------------------------------------