Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active July 13, 2017 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save primaryobjects/e1a182c7ef2f8d33731e to your computer and use it in GitHub Desktop.
Save primaryobjects/e1a182c7ef2f8d33731e to your computer and use it in GitHub Desktop.
Example Slack chatbot running chatskills.
var chatskills = require('chatskills');
var SlackBot = require('slackbots');
var bot = new SlackBot({ token: 'your-slack-token', name: 'awesome' });
// Set bot name.
chatskills.name('awesome');
// Create a new skill.
var hello = chatskills.add('hello');
// Create a new intent.
hello.intent('helloWorld', {
'slots': {},
'utterances': [ '{to |}{say|speak|tell me} {hi|hello|howdy|hi there|hiya|hi ya|hey|hay|heya}' ]
},
function(req, res) {
res.say('Hello, World!');
}
);
// Listen to slack messages.
bot.on('message', function(message) {
// Reply to humans.
if (message.type == 'message' && message.text && message.subtype != 'bot_message') {
var author = getUserById(message.user);
var channel = getChannelById(message.channel);
// Respond to input, use author.name as the session id.
chatskills.respond(message.text, author.name, function(response) {
if (channel) {
// Public channel message.
bot.postMessageToChannel(channel.name, response);
}
else {
// Private message.
bot.postMessageToUser(author.name, response);
}
});
}
});
function getUserById(id) {
return bot.users.filter(function (user) {
return user.id == id;
})[0];
}
function getChannelById(id) {
return bot.channels.filter(function (channel) {
return channel.id == id;
})[0];
}
@techonomics69
Copy link

hi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment