|
| 1 | +var Slack = require('slack-node'); |
| 2 | + |
| 3 | +module.exports = function () { |
| 4 | + |
| 5 | + |
| 6 | + return { |
| 7 | + integrationNotification: function (appId, document) { |
| 8 | + var integration_api = ["slack"]; |
| 9 | + global.appService.getApp(appId).then(function (application) { |
| 10 | + var appName = application.name; |
| 11 | + global.appService.getAllSettings(appId).then(function (settings) { |
| 12 | + var integrationSettings; |
| 13 | + settings.forEach(function (element) { |
| 14 | + if (element.category == "integrations") { |
| 15 | + integrationSettings = element.settings; |
| 16 | + } |
| 17 | + }, this); |
| 18 | + if (integrationSettings) { |
| 19 | + for (var i = 0; i < integration_api.length; i++) { |
| 20 | + switch (integration_api[i]) { |
| 21 | + case "slack": |
| 22 | + if (integrationSettings.slack.enabled) { |
| 23 | + notifyOnSlack(integrationSettings.slack, document, appName); |
| 24 | + } |
| 25 | + break; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + }); |
| 31 | + }); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +function notifyOnSlack(integrationSettings, document, appName) { |
| 38 | + var slack = new Slack(); |
| 39 | + var timeStamp = Math.floor(Date.now() / 1000); |
| 40 | + |
| 41 | + //req data |
| 42 | + var event_type = document.name; |
| 43 | + var user = document.data.username; |
| 44 | + var user_email = document.data.email; |
| 45 | + |
| 46 | + slack.setWebhook(integrationSettings.webhook_url); |
| 47 | + var text, image, title, color; |
| 48 | + switch (event_type) { |
| 49 | + case "Login": |
| 50 | + if (integrationSettings.loginNotify === true) { |
| 51 | + title = "Login"; |
| 52 | + text = "A user just logged in to " + appName + " application" |
| 53 | + color = "#36a64f" |
| 54 | + } |
| 55 | + break; |
| 56 | + case "Signup": |
| 57 | + if (integrationSettings.signUpNotify === true) { |
| 58 | + title = "Sign Up"; |
| 59 | + text = "A new user just signed up for your " + appName + " application" |
| 60 | + color = "#5CACEE"; |
| 61 | + } |
| 62 | + break; |
| 63 | + default: |
| 64 | + title = event_type; |
| 65 | + color = "#9932CC"; |
| 66 | + } |
| 67 | + if (title) { |
| 68 | + slack.webhook({ |
| 69 | + channel: "#general", |
| 70 | + username: "CloudBoost", |
| 71 | + attachments: [ |
| 72 | + { |
| 73 | + "fallback": "Whenever a User Triggers any event a notification will appear here", |
| 74 | + "color": color, |
| 75 | + "pretext": text, |
| 76 | + "author_name": "Event Notifications", |
| 77 | + "author_link": "https://www.cloudboost.io/", |
| 78 | + "author_icon": "https://d1qb2nb5cznatu.cloudfront.net/startups/i/490103-917cc2864d0246e313e9521971422f09-medium_jpg.jpg?buster=1430997518", |
| 79 | + "title": "User " + event_type + " Notification", |
| 80 | + "title_link": "https://www.cloudboost.io/", |
| 81 | + // "text": text, |
| 82 | + "fields": [ |
| 83 | + { |
| 84 | + "title": "User Name", |
| 85 | + "value": user, |
| 86 | + "short": false |
| 87 | + }, |
| 88 | + { |
| 89 | + "title": "User Email", |
| 90 | + "value": user_email, |
| 91 | + "short": false |
| 92 | + } |
| 93 | + ], |
| 94 | + "footer": "CloudBoost", |
| 95 | + "footer_icon": "https://d1qb2nb5cznatu.cloudfront.net/startups/i/490103-917cc2864d0246e313e9521971422f09-medium_jpg.jpg?buster=1430997518", |
| 96 | + "ts": timeStamp |
| 97 | + } |
| 98 | + ], |
| 99 | + icon_emoji: "https://d1qb2nb5cznatu.cloudfront.net/startups/i/490103-917cc2864d0246e313e9521971422f09-medium_jpg.jpg?buster=1430997518" |
| 100 | + }, function (err, response) { |
| 101 | + if (!err) { |
| 102 | + console.log(response); |
| 103 | + } else { |
| 104 | + console.log(err); |
| 105 | + } |
| 106 | + }); |
| 107 | + return true; |
| 108 | + } |
| 109 | + return false; |
| 110 | +} |
0 commit comments