Skip to content

Instantly share code, notes, and snippets.

@ErinsMatthew
Created December 13, 2016 21:35
Show Gist options
  • Save ErinsMatthew/8e3f55ba48e363530eaed13040a9fe06 to your computer and use it in GitHub Desktop.
Save ErinsMatthew/8e3f55ba48e363530eaed13040a9fe06 to your computer and use it in GitHub Desktop.
Dynamically Load `hears` from External JSON File with Botkit
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;
}
[
{
"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