This is an implementaion of a simple Slack bot, based on node.js and the botkit package. The bot is using a json configuration file to match keywords and reply with static messages (can include attachments). It can be used to quickly create a demo bot without actual implementaion of backend logic or integrations.
- Clone from github and run
npm install. - Create/join a Slack team and create bot(s) in that group (see Slack docs). For each bot you want to run with demo-bot-slack, copy the bot API token.
- Write a conversation json configuration file and place it under the
configfolder (see configuraiton file format below) . - Set the following environment variables:
SLACK_BOT_TOKEN- the API token copied from SlackSLACK_BOT_TYPE- the name of the json configuration file to use for this bot execution (without the js extnesion, e.g.SET SLACK_BOT_TYPE=workbenchwill run a bot with the configuraiton fileconfig/workbench.json).
- Run the bot using
node bot.js
demo-bot-slack is using json to match types of messages->keywords->replies.
A first key of default specifies what the bot will answer if no match is found. A second key of all specifies keywords that will be matched on all direct events (mention and message). Specific responses can be place under 'direct_mention' or 'direct_message'. Lastly, the 'ambient' key lists keywords and responses for messages not mentioning the bot at all.
All keyword keys will be matched as javascript regular expressions, and can use any regexp expression format. Reply format is Slack's message format, including attachments.
{
'default': 'a default repsonse',
'all': {
'.*keyword.*1': {'text': 'reply text'},
'.*keyword.*2': {'text': 'some reply text',
'attachments': [{
'title': 'attachment title',
'text': 'attachment text',
'color': "#7CD197",
'mrkdwn_in': ['text', 'title']
}]}
},
'direct_mention': {
'.*keyword.*3': {'text': 'reply text'}
},
'direct_message': {
},
'ambient': {
}
}
The bot will match keywords by the following order, for messages referencing the bot:
- Keywords under the
allcategory, from top to bottom - Keywords in subsequent categories (top to bottom for both categories and keywords within)
- If no match was found, the default reply For messages not referencing the bot, but on a channel with the bot, the only lookup is only for keywords listed under 'ambient'.
If a match is found, the reply is sent and the lookup is done (i.e. you can't match more than one reply to an event).
MIT License
- Inbar Shani - @inbarshani