Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refact the bot object creation #7

Merged
merged 4 commits into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
};