Skip to content

Full Yeoman Generator to create Slack Bot with travis, coveralls and test

License

Notifications You must be signed in to change notification settings

eromano/generator-slack-bot

Repository files navigation

Slack bot Yeoman Generator

yeoman logo slack logo

Build Status Coverage Status license downloads stats npm version blog

About Slack bot Yeoman Generator

Full Yeoman Generator to create Slack Bot with travis, coveralls and test.

Installation

First, install Yeoman and generator-slack-bot using npm (we assume you have pre-installed node.js).

npm install -g yo
npm install -g generator-slack-bot

Then generate your new Bot:

yo slack-bot

generator slack bot shell

Which will generate the following project structure, if for example your bot name is Mazinga:

├── travis.yml
├── gitignore
├── jscsrc
├── LICENSE
├── README.md
├── app.json
├── package.json
├── Gruntfile.js
├── configBot.json
├── grunt/coveralls.js
├── grunt/jscs.js
├── grunt/jshint.js
├── grunt/mocha_istanbul.js
├── grunt/mochaTest.js
├── src/slackMessageAnalyzer.js
├── src/main.js
├── src/botMazinga.js
├── test/botMazinga.spec.js
└── test/mockoBjects/channel.js

And run npm install for you to fetch all dependencies.

Getting Started with the bot

In order to make the bot works you need a TOKEN_SLACK. Your Slack bot integration token is obtainable at https://my.slack.com/services/new/bot. After you have obtained your TOKEN_SLACK copy it in the configBot.json

Main chunks of codes to understand in the generated bot

slackMessageAnalyzer.js

In this class you are going to find all the utility in order to analyzer the slack message

Class Method Parameters Description
slackMessageAnalyzer.js createSlackMessageLink(titleLink, link) @param {String} titleLink text to show instead of the pure URL
@param {String} link to redirect
Create a slack link format message
slackMessageAnalyzer.js createSlackMessageLink(titleLink, link) @param {String} textMessage message to analyze
@param {String} textToSearch text to search in the message
Create a slack link format message isTextContainedInMessage

yourBotName.js

In this class there are the listeners for the messages

Class Method Parameters Description
yourBotName.js _listenerMessage(condition, callback) @param {Boolean} condition to meet to call the callback
@param {Function} callback to call if the condition is satisfied
Call a callback in the case a message from slack meets the condition

Code Example

This pice of code below will activate a listner on the hello message and will answer in the general chat wit a mssage 'Hello Answer'

   _listenerHelloMessage() {
    this._listenerMessage(this.isHelloMessage, (function(message) {
      var message = 'Hello answer';
      var fallBack = 'Hello answer';
      var color = 'warning';
      var title = 'Hello Answer ' + slackMessageAnalyzer.createSlackMessageLink('made by slack bot generator','https://github.com/eromano/generator-slack-bot');

      this.postSlackMessage(message, fallBack, color, null, title, '', 'general');
    }).bind(this));
  }
  
  isHelloMessage(textMessage) {
    return slackMessageAnalyzer.isTextContainedInMessage(textMessage, 'hello');
  }

History

For detailed changelog, see Releases.

Contributors

Contributor GitHub profile Twitter profile
Eugenio Romano (contributor) Eugenio Romano @RomanoEugenio

All contributors contributors.

License

MIT

About

Full Yeoman Generator to create Slack Bot with travis, coveralls and test

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published