Skip to content

Commit

Permalink
Merge pull request #7 from brunojdo/refact-singleton
Browse files Browse the repository at this point in the history
Refact the bot object creation
  • Loading branch information
brunojdo authored Aug 22, 2017
2 parents 6598c3b + 257961e commit 56a7472
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ If you prefer, you may also clone this project and run `npm`. On the `./app` fol

### Enable Webhooks

In your Gitlab project select **Settings -> Integrations** and put your service address. For example, on: `http://localhost:8008/webhook` select which hooks you want use. For more information [click here.](https://docs.gitlab.com/ce/user/project/integrations/webhooks.html)
In your Gitlab project select **Settings -> Integrations** and put your service address. For example, on: `http://localhost:8080/webhook` select which hooks you want use. For more information [click here.](https://docs.gitlab.com/ce/user/project/integrations/webhooks.html)

### Create a SlackBot

Expand Down
2 changes: 1 addition & 1 deletion app/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
system:
name: Slack-Gitlab Notifier
port: 8080
port:
lang_selector: lang/en_US.yml
slack:
bot:
Expand Down
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"type": "git"
},
"scripts": {
"prestart": "yarn",
"start": "nodemon express.js -e yml,js,json --exec babel-node",
"build": "babel *.js -d dist",
"serve": "node dist/express.js"
Expand Down
47 changes: 27 additions & 20 deletions app/slackNotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,36 @@ function buildMessageByAction(action, submitter){
};
}

/**
* Create a Singleton bot that open a RTM connection with Slack Service
* @return {object - Slack session object}
*/
var Bot = (function () {
var object;
return {
getInstance: function () {
if (!object) {
object = new SlackBot({
name: conf.slack.bot.name,
token: conf.slack.bot.token
});
}
return object;
}
};
})();

/**
* Notify a slack channel.
*
* @param {Message to be send to slack} message
* @param {Another parameters to send on the message} params
*/
function sendChannelMessage(ch, message, params) {
// Create a bot - To add a bot https://my.slack.com/services/new/bot
var bot = new SlackBot({
name: conf.slack.bot.name,
token: conf.slack.bot.token
});
bot.on('start', function() {
console.log("Send message to: " + ch)
bot.postMessageToChannel(ch, message, params).fail(function(data){
console.err(data);
});
var bot = Bot.getInstance();
console.log("Send message to: " + ch);
bot.postMessageToChannel(ch, message, params).fail(function(data){
console.err(data);
});
};

Expand All @@ -132,15 +145,9 @@ function sendChannelMessage(ch, message, params) {
* @param {Another parameters to send on the message} params
*/
function sendDMMessage(user, message, params) {
// Create a bot - To add a bot https://my.slack.com/services/new/bot
var bot = new SlackBot({
name: conf.slack.bot.name,
token: conf.slack.bot.token
});
bot.on('start', function(){
console.log("Send message to: " + user)
bot.postMessageToUser(user, message, params).fail(function(data){
console.err(data);
});
var bot = Bot.getInstance();
console.log("Send message to: " + user);
bot.postMessageToUser(user, message, params).fail(function(data){
console.err(data);
});
};

0 comments on commit 56a7472

Please sign in to comment.