Created
December 13, 2016 21:35
-
-
Save ErinsMatthew/8e3f55ba48e363530eaed13040a9fe06 to your computer and use it in GitHub Desktop.
Dynamically Load `hears` from External JSON File with Botkit
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 Botkit = require( 'botkit' ); | |
const Q = require( 'q' ); | |
const _ = require( 'lodash' ); | |
var controller = Botkit.slackbot(); | |
var bot = controller.spawn( { | |
token: process.env.token | |
} ).startRTM( ( err, bot, payload ) => { | |
loadHears() | |
.then( ( hears ) => { | |
if ( hears && _.isArray( hears ) ) { | |
hears.forEach( ( h ) => { | |
controller.hears( h.patterns, h.events, ( bot, message ) => { | |
bot.reply( message, h.response ); | |
} ); | |
} ); | |
} | |
} ) | |
.fail( ( err ) => { | |
console.error( err ); | |
} ); | |
} ); | |
function loadHears() { | |
var deferred = Q.defer(); | |
fs.readFile( 'hears.json', 'utf8', ( err, data ) => { | |
if ( err ) { | |
deferred.reject( err ); | |
return err; | |
} | |
deferred.resolve( JSON.parse( data ) ); | |
} ); | |
return deferred.promise; | |
} |
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
[ | |
{ | |
"patterns": [ "hi", "hello" ], | |
"events": [ "ambient", "direct_message", "direct_mention" ], | |
"response": "Hello!" | |
}, | |
{ | |
"patterns": [ "help", "assistance" ], | |
"events": [ "ambient", "direct_message", "direct_mention" ], | |
"response": "For help, go to XYZ." | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment