Last active
July 13, 2017 16:18
-
-
Save primaryobjects/e1a182c7ef2f8d33731e to your computer and use it in GitHub Desktop.
Example Slack chatbot running chatskills.
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
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]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi